HHH-12471 Avoid using a TypeCache in the ByteBuddy BytecodeProvider

This commit is contained in:
Sanne Grinovero 2018-04-10 18:05:28 +01:00
parent 69000ddc16
commit ec2f8e9ba5
1 changed files with 21 additions and 37 deletions

View File

@ -11,7 +11,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.concurrent.Callable;
import net.bytebuddy.TypeCache;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
@ -39,8 +38,7 @@ import net.bytebuddy.matcher.ElementMatchers;
public class BytecodeProviderImpl implements BytecodeProvider {
private final TypeCache<String> FAST_CLASSES = new TypeCache.WithInlineExpunction<String>(TypeCache.Sort.SOFT);
private final TypeCache<String> BULK_ACCESSORS = new TypeCache.WithInlineExpunction<String>(TypeCache.Sort.SOFT);
private final ByteBuddy buddy = new ByteBuddy().with( TypeValidation.DISABLED );
@Override
public ProxyFactoryFactory getProxyFactoryFactory() {
@ -58,41 +56,27 @@ public class BytecodeProviderImpl implements BytecodeProvider {
findAccessors( clazz, getterNames, setterNames, types, getters, setters );
final Constructor<?> constructor = findConstructor( clazz );
Class fastClass = FAST_CLASSES.findOrInsert( clazz.getClassLoader(), clazz.getName(), new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return new ByteBuddy()
.with(TypeValidation.DISABLED)
.with(new NamingStrategy.SuffixingRandom("HibernateInstantiator"))
.subclass(ReflectionOptimizer.InstantiationOptimizer.class)
.method(ElementMatchers.named("newInstance"))
.intercept(MethodCall.construct(constructor))
.make()
.load(clazz.getClassLoader())
.getLoaded();
}
}, FAST_CLASSES);
final Class fastClass = buddy
.with( new NamingStrategy.SuffixingRandom( "HibernateInstantiator" ) )
.subclass( ReflectionOptimizer.InstantiationOptimizer.class )
.method( ElementMatchers.named( "newInstance" ) )
.intercept( MethodCall.construct( constructor ) )
.make()
.load( clazz.getClassLoader() )
.getLoaded();
fastClass = FAST_CLASSES.insert( clazz.getClassLoader(), clazz.getName(), fastClass );
Class bulkAccessor = BULK_ACCESSORS.findOrInsert( clazz.getClassLoader(), clazz.getName(), new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return new ByteBuddy()
.with(TypeValidation.DISABLED)
.with(new NamingStrategy.SuffixingRandom("HibernateAccessOptimizer"))
.subclass(ReflectionOptimizer.AccessOptimizer.class)
.method(ElementMatchers.named("getPropertyValues"))
.intercept(new Implementation.Simple(new GetPropertyValues(clazz, getters)))
.method(ElementMatchers.named("setPropertyValues"))
.intercept(new Implementation.Simple(new SetPropertyValues(clazz, setters)))
.method(ElementMatchers.named("getPropertyNames"))
.intercept(MethodCall.call(new CloningPropertyCall(getterNames)))
.make()
.load(clazz.getClassLoader())
.getLoaded();
}
}, BULK_ACCESSORS);
final Class bulkAccessor = buddy
.with( new NamingStrategy.SuffixingRandom( "HibernateAccessOptimizer" ) )
.subclass( ReflectionOptimizer.AccessOptimizer.class )
.method( ElementMatchers.named( "getPropertyValues" ) )
.intercept( new Implementation.Simple( new GetPropertyValues( clazz, getters ) ) )
.method( ElementMatchers.named( "setPropertyValues" ) )
.intercept( new Implementation.Simple( new SetPropertyValues( clazz, setters ) ) )
.method( ElementMatchers.named( "getPropertyNames" ) )
.intercept( MethodCall.call( new CloningPropertyCall( getterNames ) ) )
.make()
.load( clazz.getClassLoader() )
.getLoaded();
try {
return new ReflectionOptimizerImpl(