Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ static <T, E extends Throwable> T invokeExact(final ThrowableCallable<T> methodH
if (rethrow.isInstance(e)) {
throw rethrow.cast(e);
}
// Unreachable: the looked-up method declares no other exceptions.
if (e instanceof Error) {
// A JVM error (OutOfMemoryError, ...) must keep its type; only exceptions are wrapped.
throw (Error) e;
}
// The looked-up method declares no checked exceptions besides rethrow's type, so this wraps runtime exceptions only.
throw new IllegalStateException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -54,9 +55,20 @@ void findStaticReturnsNullForMissingMethod() {

@Test
void invokeExactRethrowsDeclaredException() {
assertThrows(FactoryConfigurationError.class, () -> MethodHandleFactory.invokeExact(() -> {
throw new FactoryConfigurationError("boom");
final FactoryConfigurationError declared = new FactoryConfigurationError("boom");
final FactoryConfigurationError thrown = assertThrows(FactoryConfigurationError.class, () -> MethodHandleFactory.invokeExact(() -> {
throw declared;
}, FactoryConfigurationError.class), "an exception of the declared type must be rethrown");
assertSame(declared, thrown, "the declared exception must propagate unchanged");
}

@Test
void invokeExactRethrowsUndeclaredError() {
final OutOfMemoryError error = new OutOfMemoryError("boom");
final OutOfMemoryError thrown = assertThrows(OutOfMemoryError.class, () -> MethodHandleFactory.invokeExact(() -> {
throw error;
}, FactoryConfigurationError.class), "a JVM error must keep its type");
assertSame(error, thrown, "the error must propagate unchanged");
}

@Test
Expand Down
Loading