From c1bf09952b5f5f70b7f83aedf870daa18fdb9d65 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 11 Jul 2023 18:36:12 +0200 Subject: [PATCH] Improve diagnostics for LinkageError in case of ClassLoader mismatch Closes gh-25940 --- .../org/springframework/cglib/core/ReflectUtils.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java b/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java index 9467b9355077..cbf9ae8b7624 100644 --- a/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java +++ b/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java @@ -576,15 +576,17 @@ public static Class defineClass(String className, byte[] b, ClassLoader loader, c = (Class) lookupDefineClassMethod.invoke(lookup, b); } catch (InvocationTargetException ex) { - throw new CodeGenerationException(ex.getTargetException()); - } - catch (IllegalAccessException ex) { - throw new CodeGenerationException(ex) { + Throwable target = ex.getTargetException(); + if (target.getClass() != LinkageError.class && target.getClass() != IllegalAccessException.class) { + throw new CodeGenerationException(target); + } + throw new CodeGenerationException(target) { @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."; } }; }