Skip to content

Commit 2212495

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Migrate more code to use UndeclaredThrowableException.
And generally make our handling more consistent (`throwIfUnchecked` + `UndeclaredThrowableException` with appropriate comments). This is mostly a no-op (assuming that the undeclared throwables are in fact impossible :)), but I note that the old `ChecksumHashFunction` code appears to be wrapping any _`RuntimeException`_ in an `AssertionError`, which would be bad if there were a practical chance of such a thing (though hopefully there is not). Compare cl/686064398. I suspect that we could find yet more similar code. RELNOTES=n/a PiperOrigin-RevId: 686883397
1 parent 1337405 commit 2212495

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

guava/src/com/google/common/hash/ChecksumHashFunction.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
19+
import static com.google.common.base.Throwables.throwIfUnchecked;
1920

2021
import com.google.errorprone.annotations.Immutable;
2122
import com.google.j2objc.annotations.J2ObjCIncompatible;
2223
import java.io.Serializable;
2324
import java.lang.invoke.MethodHandle;
2425
import java.lang.invoke.MethodHandles;
2526
import java.lang.invoke.MethodType;
27+
import java.lang.reflect.UndeclaredThrowableException;
2628
import java.nio.ByteBuffer;
2729
import java.util.zip.Checksum;
2830
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -114,10 +116,10 @@ static boolean updateByteBuffer(Checksum cs, ByteBuffer bb) {
114116
if (UPDATE_BB != null) {
115117
try {
116118
UPDATE_BB.invokeExact(cs, bb);
117-
} catch (Error t) {
118-
throw t;
119-
} catch (Throwable t) {
120-
throw new AssertionError(t);
119+
} catch (Throwable e) {
120+
throwIfUnchecked(e);
121+
// This should be impossible, since `update` has no `throws` clause.
122+
throw new UndeclaredThrowableException(e);
121123
}
122124
return true;
123125
} else {

guava/src/com/google/common/hash/Hashing.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.j2objc.annotations.J2ObjCIncompatible;
2424
import java.lang.invoke.MethodHandle;
2525
import java.lang.invoke.MethodHandles;
26+
import java.lang.reflect.UndeclaredThrowableException;
2627
import java.security.Key;
2728
import java.util.ArrayList;
2829
import java.util.Arrays;
@@ -520,8 +521,8 @@ static Checksum newCrc32c() {
520521
return (Checksum) CONSTRUCTOR.invokeExact();
521522
} catch (Throwable e) {
522523
throwIfUnchecked(e);
523-
// That constructor has no `throws` clause.
524-
throw newLinkageError(e);
524+
// This should be impossible, since the constructor has no `throws` clause.
525+
throw new UndeclaredThrowableException(e);
525526
}
526527
}
527528

0 commit comments

Comments
 (0)