Skip to content

Commit 82b7fcb

Browse files
committed
Try loadClass on LinkageError in case of ClassLoader mismatch
See gh-34677 (cherry picked from commit 7f2c1f4)
1 parent 6717fca commit 82b7fcb

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

Diff for: spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,26 @@ public static Class defineClass(String className, byte[] b, ClassLoader loader,
527527
c = lookup.defineClass(b);
528528
}
529529
catch (LinkageError | IllegalAccessException ex) {
530-
throw new CodeGenerationException(ex) {
531-
@Override
532-
public String getMessage() {
533-
return "ClassLoader mismatch for [" + contextClass.getName() +
534-
"]: JVM should be started with --add-opens=java.base/java.lang=ALL-UNNAMED " +
535-
"for ClassLoader.defineClass to be accessible on " + loader.getClass().getName() +
536-
"; consider co-locating the affected class in that target ClassLoader instead.";
530+
if (ex instanceof LinkageError) {
531+
// Could be a ClassLoader mismatch with the class pre-existing in a
532+
// parent ClassLoader -> try loadClass before giving up completely.
533+
try {
534+
c = contextClass.getClassLoader().loadClass(className);
537535
}
538-
};
536+
catch (ClassNotFoundException cnfe) {
537+
}
538+
}
539+
if (c == null) {
540+
throw new CodeGenerationException(ex) {
541+
@Override
542+
public String getMessage() {
543+
return "ClassLoader mismatch for [" + contextClass.getName() +
544+
"]: JVM should be started with --add-opens=java.base/java.lang=ALL-UNNAMED " +
545+
"for ClassLoader.defineClass to be accessible on " + loader.getClass().getName() +
546+
"; consider co-locating the affected class in that target ClassLoader instead.";
547+
}
548+
};
549+
}
539550
}
540551
catch (Throwable ex) {
541552
throw new CodeGenerationException(ex);

0 commit comments

Comments
 (0)