Defensively handle fast class generation failure for individual methods

Includes rethrowing of last actual defineClass exception encountered.

Closes gh-27490
This commit is contained in:
Juergen Hoeller
2021-09-30 18:00:10 +02:00
parent 137bca129f
commit edf664c1cf
2 changed files with 33 additions and 12 deletions

View File

@@ -499,6 +499,7 @@ public class ReflectUtils {
ProtectionDomain protectionDomain, Class<?> contextClass) throws Exception {
Class c = null;
Throwable t = THROWABLE;
// Preferred option: JDK 9+ Lookup.defineClass API if ClassLoader matches
if (contextClass != null && contextClass.getClassLoader() == loader &&
@@ -516,6 +517,7 @@ public class ReflectUtils {
// in case of plain LinkageError (class already defined)
// or IllegalArgumentException (class in different package):
// fall through to traditional ClassLoader.defineClass below
t = target;
}
catch (Throwable ex) {
throw new CodeGenerationException(ex);
@@ -543,6 +545,7 @@ public class ReflectUtils {
if (!ex.getClass().getName().endsWith("InaccessibleObjectException")) {
throw new CodeGenerationException(ex);
}
t = ex;
}
}
@@ -564,7 +567,7 @@ public class ReflectUtils {
// No defineClass variant available at all?
if (c == null) {
throw new CodeGenerationException(THROWABLE);
throw new CodeGenerationException(t);
}
// Force static initializers to run.