Try loadClass on LinkageError in case of ClassLoader mismatch

See gh-34677
This commit is contained in:
Juergen Hoeller 2025-04-10 18:30:45 +02:00
parent 26869b0e4c
commit 7f2c1f447f
1 changed files with 19 additions and 8 deletions

View File

@ -527,15 +527,26 @@ public class ReflectUtils {
c = lookup.defineClass(b);
}
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() +
"; consider co-locating the affected class in that target ClassLoader instead.";
if (ex instanceof LinkageError) {
// Could be a ClassLoader mismatch with the class pre-existing in a
// parent ClassLoader -> try loadClass before giving up completely.
try {
c = contextClass.getClassLoader().loadClass(className);
}
};
catch (ClassNotFoundException cnfe) {
}
}
if (c == null) {
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() +
"; consider co-locating the affected class in that target ClassLoader instead.";
}
};
}
}
catch (Throwable ex) {
throw new CodeGenerationException(ex);