Browse Source

Let BFAwareGeneratorStrategy extend ClassLoaderAwareGeneratorStrategy

This commit updates BeanFactoryAwareGeneratorStrategy to extend 
ClassLoaderAwareGeneratorStrategy in order to avoid duplication of the
common generate() implementation.

Closes gh-24396
pull/24069/head
Qimiao Chen 6 years ago committed by GitHub
parent
commit
5aa37ea07b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

40
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

@ -35,8 +35,8 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor; @@ -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 { @@ -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 { @@ -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);
}
}
}
}

Loading…
Cancel
Save