Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to reflection if building lambda instance fails. #683

Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/nebula-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# test against JDK 8, 17 and 21.
java: [ 8, 17, 21]
# test against may JDKs. Only 8, 17 and 21 are officially supported, fixes for others are on a best effort basis.
java: [ 8, 9, 11, 17, 21]
name: CI with Java ${{ matrix.java }}
steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import javax.inject.Inject;
import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaConversionException;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
Expand Down Expand Up @@ -451,14 +450,21 @@ private static Function<Object, Object> asFunction(MethodHandles.Lookup lookup,
methodHandle,
methodHandle.type());
return (Function<Object, Object>) site.getTarget().invokeExact();
} catch (VerifyError ve) {
// This happens in java 9 and 11 (maybe others, we haven't checked. 8 and 17 onwards are known good)
// The generated bytecode for the CallSite has bad bytecode and can't be loaded as a class.
// For this case, we'll just fall back to calling the method handle. It's slower but works.
return o -> {
try {
return methodHandle.invoke(o);
} catch (Throwable t) {
maybeWrapThenRethrow(t);
return null; // Unreachable, but the compiler can't know
}
};
} catch (Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof Error) {
throw (Error) t;
}
throw new RuntimeException(t);
maybeWrapThenRethrow(t);
return null; // Unreachable, but the compiler can't know
}
}

Expand All @@ -476,14 +482,31 @@ private static BiFunction<Object, Object, Object> asBiFunction(MethodHandles.Loo
methodHandle,
methodHandle.type());
return (BiFunction<Object, Object, Object>) site.getTarget().invokeExact();
} catch (Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof Error) {
throw (Error) t;
} catch (VerifyError ve) {
// This happens in java 9 and 11 (maybe others, we haven't checked. 8 and 17 onwards are known good)
// The generated bytecode for the CallSite has bad bytecode and can't be loaded as a class.
// For this case, we'll just fall back to calling the method handle. It's slower but works.
return (o1, o2) -> {
try {
return methodHandle.invoke(o1, o2);
} catch (Throwable t) {
maybeWrapThenRethrow(t);
return null; // Unreachable, but the compiler can't know
}
throw new RuntimeException(t);
};
} catch (Throwable t) {
maybeWrapThenRethrow(t);
return null; // Unreachable, but the compiler can't know
}
}

private static void maybeWrapThenRethrow(Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof Error) {
throw (Error) t;
}
throw new RuntimeException(t);
}
}