Introduce setLoadedClassHandler method on CGLIB ReflectUtils

See gh-29295
This commit is contained in:
Juergen Hoeller
2022-10-10 16:49:55 +02:00
parent b2b3163fae
commit 26c6a742d9
2 changed files with 21 additions and 1 deletions

View File

@@ -349,7 +349,9 @@ abstract public class AbstractClassGenerator<T> implements ClassGenerator {
if (attemptLoad) {
try {
// SPRING PATCH BEGIN
gen = Class.forName(getClassName(), true, classLoader);
synchronized (classLoader) { // just in case
gen = ReflectUtils.loadClass(getClassName(), classLoader);
}
// SPRING PATCH END
return gen;
}

View File

@@ -35,6 +35,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.springframework.asm.Type;
@@ -63,6 +64,8 @@ public class ReflectUtils {
private static BiConsumer<String, byte[]> generatedClassHandler;
private static Consumer<Class<?>> loadedClassHandler;
// SPRING PATCH BEGIN
static {
// Resolve protected ClassLoader.defineClass method for fallback use
@@ -550,6 +553,21 @@ public class ReflectUtils {
Class.forName(className, true, loader);
return c;
}
public static void setLoadedClassHandler(Consumer<Class<?>> loadedClassHandler) {
ReflectUtils.loadedClassHandler = loadedClassHandler;
}
public static Class<?> loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
// Force static initializers to run.
Class<?> clazz = Class.forName(className, true, classLoader);
Consumer<Class<?>> handlerToUse = loadedClassHandler;
if (handlerToUse != null) {
handlerToUse.accept(clazz);
}
return clazz;
}
// SPRING PATCH END
public static int findPackageProtected(Class[] classes) {