Skip to content

Commit 6887802

Browse files
committed
Fallback to ClassLoader.defineClass for Lookup.defineClass LinkageError
Issue: SPR-16902
1 parent 3a4b5c2 commit 6887802

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,18 @@ public static Class defineClass(String className, byte[] b, ClassLoader loader,
493493
Class c = null;
494494
if (contextClass != null && privateLookupInMethod != null && lookupDefineClassMethod != null) {
495495
try {
496-
MethodHandles.Lookup lookup =
497-
(MethodHandles.Lookup) privateLookupInMethod.invoke(null, contextClass, MethodHandles.lookup());
496+
MethodHandles.Lookup lookup = (MethodHandles.Lookup)
497+
privateLookupInMethod.invoke(null, contextClass, MethodHandles.lookup());
498498
c = (Class) lookupDefineClassMethod.invoke(lookup, b);
499499
}
500500
catch (InvocationTargetException ex) {
501-
if (!(ex.getTargetException() instanceof IllegalArgumentException)) {
502-
throw new CodeGenerationException(ex.getTargetException());
501+
Throwable target = ex.getTargetException();
502+
if (target.getClass() != LinkageError.class && target.getClass() != IllegalArgumentException.class) {
503+
throw new CodeGenerationException(target);
503504
}
504-
// in case of IllegalArgumentException: fall through to defineClass
505+
// in case of plain LinkageError (class already defined)
506+
// or IllegalArgumentException (class in different package):
507+
// fall through to traditional ClassLoader.defineClass below
505508
}
506509
catch (Throwable ex) {
507510
throw new CodeGenerationException(ex);

0 commit comments

Comments
 (0)