Skip to content

Commit

Permalink
Improve diagnostics for LinkageError in case of ClassLoader mismatch
Browse files Browse the repository at this point in the history
Closes gh-25940
  • Loading branch information
jhoeller committed Jul 11, 2023
1 parent c375fb1 commit e2b24f3
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.beans.PropertyDescriptor;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -508,14 +509,14 @@ public static Class defineClass(String className, byte[] b, ClassLoader loader,
catch (InvocationTargetException ex) {
throw new CodeGenerationException(ex.getTargetException());
}
catch (Throwable ex) {
// Fall through if setAccessible fails with InaccessibleObjectException on JDK 9+
// (on the module path and/or with a JVM bootstrapped with --illegal-access=deny)
if (!ex.getClass().getName().endsWith("InaccessibleObjectException")) {
throw new CodeGenerationException(ex);
}
catch (InaccessibleObjectException ex) {
// setAccessible failed with JDK 9+ InaccessibleObjectException -> fall through
// Avoid through JVM startup with --add-opens=java.base/java.lang=ALL-UNNAMED
t = ex;
}
catch (Throwable ex) {
throw new CodeGenerationException(ex);
}
}
}

Expand All @@ -525,13 +526,14 @@ public static Class defineClass(String className, byte[] b, ClassLoader loader,
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(contextClass, MethodHandles.lookup());
c = lookup.defineClass(b);
}
catch (IllegalAccessException ex) {
catch (LinkageError | IllegalAccessException ex) {
throw new CodeGenerationException(ex) {
@Override
public String getMessage() {
return "ClassLoader mismatch for [" + contextClass.getName() +
"]: JVM should be started with --add-opens=java.base/java.lang=ALL-UNNAMED " +
"for ClassLoader.defineClass to be accessible on " + loader.getClass().getName();
"for ClassLoader.defineClass to be accessible on " + loader.getClass().getName() +
"; consider co-locating the affected class in that target ClassLoader instead.";
}
};
}
Expand Down

0 comments on commit e2b24f3

Please sign in to comment.