From de1b938e2ed95104c532afc708e4accca7461e76 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 13 Jul 2022 11:10:00 +0200 Subject: [PATCH] Improve diagnostics for CGLIB ClassLoader mismatch with --add-opens hint Closes gh-28747 --- .../org/springframework/cglib/core/ReflectUtils.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 b765ce12c0f..9467b935507 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 @@ -490,7 +490,7 @@ public class ReflectUtils { return defineClass(className, b, loader, protectionDomain, null); } - @SuppressWarnings("deprecation") // on JDK 9 + @SuppressWarnings({"deprecation", "serial"}) public static Class defineClass(String className, byte[] b, ClassLoader loader, ProtectionDomain protectionDomain, Class contextClass) throws Exception { @@ -578,6 +578,16 @@ public class ReflectUtils { catch (InvocationTargetException ex) { throw new CodeGenerationException(ex.getTargetException()); } + catch (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(); + } + }; + } catch (Throwable ex) { throw new CodeGenerationException(ex); }