From 5aa37ea07b31a6408e244f99d9440fba202f76ff Mon Sep 17 00:00:00 2001 From: Qimiao Chen Date: Tue, 4 Feb 2020 21:10:59 +0800 Subject: [PATCH] Let BFAwareGeneratorStrategy extend ClassLoaderAwareGeneratorStrategy This commit updates BeanFactoryAwareGeneratorStrategy to extend ClassLoaderAwareGeneratorStrategy in order to avoid duplication of the common generate() implementation. Closes gh-24396 --- .../ConfigurationClassEnhancer.java | 40 ++----------------- 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java index a27f1d436ce..17079522c7f 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java @@ -35,8 +35,8 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.support.SimpleInstantiationStrategy; import org.springframework.cglib.core.ClassGenerator; +import org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy; import org.springframework.cglib.core.Constants; -import org.springframework.cglib.core.DefaultGeneratorStrategy; import org.springframework.cglib.core.SpringNamingPolicy; import org.springframework.cglib.proxy.Callback; import org.springframework.cglib.proxy.CallbackFilter; @@ -207,13 +207,11 @@ class ConfigurationClassEnhancer { * Also exposes the application ClassLoader as thread context ClassLoader for the time of * class generation (in order for ASM to pick it up when doing common superclass resolution). */ - private static class BeanFactoryAwareGeneratorStrategy extends DefaultGeneratorStrategy { - - @Nullable - private final ClassLoader classLoader; + private static class BeanFactoryAwareGeneratorStrategy extends + ClassLoaderAwareGeneratorStrategy { public BeanFactoryAwareGeneratorStrategy(@Nullable ClassLoader classLoader) { - this.classLoader = classLoader; + super(classLoader); } @Override @@ -228,36 +226,6 @@ class ConfigurationClassEnhancer { return new TransformingClassGenerator(cg, transformer); } - @Override - public byte[] generate(ClassGenerator cg) throws Exception { - if (this.classLoader == null) { - return super.generate(cg); - } - - Thread currentThread = Thread.currentThread(); - ClassLoader threadContextClassLoader; - try { - threadContextClassLoader = currentThread.getContextClassLoader(); - } - catch (Throwable ex) { - // Cannot access thread context ClassLoader - falling back... - return super.generate(cg); - } - - boolean overrideClassLoader = !this.classLoader.equals(threadContextClassLoader); - if (overrideClassLoader) { - currentThread.setContextClassLoader(this.classLoader); - } - try { - return super.generate(cg); - } - finally { - if (overrideClassLoader) { - // Reset original thread context ClassLoader. - currentThread.setContextClassLoader(threadContextClassLoader); - } - } - } }