HHH-12857 Reuse the TypePool created in EnhancerImpl

This commit is contained in:
Guillaume Smet 2018-07-26 13:14:09 +02:00
parent dff202ded9
commit 6a3ed33627
3 changed files with 26 additions and 9 deletions

View File

@ -69,7 +69,7 @@ public class EnhancerImpl implements Enhancer {
protected final ByteBuddyEnhancementContext enhancementContext;
private final ByteBuddyState byteBuddyState;
private final TypePool classPool;
private final TypePool typePool;
/**
* Constructs the Enhancer, using the given context.
@ -81,7 +81,7 @@ public class EnhancerImpl implements Enhancer {
public EnhancerImpl(final EnhancementContext enhancementContext, final ByteBuddyState byteBuddyState) {
this.enhancementContext = new ByteBuddyEnhancementContext( enhancementContext );
this.byteBuddyState = byteBuddyState;
this.classPool = buildClassPool( this.enhancementContext );
this.typePool = buildTypePool( this.enhancementContext );
}
/**
@ -100,9 +100,9 @@ public class EnhancerImpl implements Enhancer {
//Classpool#describe does not accept '/' in the description name as it expects a class name. See HHH-12545
final String safeClassName = className.replace( '/', '.' );
try {
final TypeDescription typeDescription = classPool.describe( safeClassName ).resolve();
final TypeDescription typeDescription = typePool.describe( safeClassName ).resolve();
return byteBuddyState.rewrite( safeClassName, originalBytes, byteBuddy -> doEnhance(
return byteBuddyState.rewrite( typePool, safeClassName, originalBytes, byteBuddy -> doEnhance(
byteBuddy.ignore( isDefaultFinalizer() ).redefine( typeDescription, ClassFileLocator.Simple.of( safeClassName, originalBytes ) ),
typeDescription
) );
@ -112,7 +112,7 @@ public class EnhancerImpl implements Enhancer {
}
}
private TypePool buildClassPool(final ByteBuddyEnhancementContext enhancementContext) {
private TypePool buildTypePool(final ByteBuddyEnhancementContext enhancementContext) {
return TypePool.Default.WithLazyResolution.of( enhancementContext.getLoadingClassLoader() );
}
@ -128,7 +128,7 @@ public class EnhancerImpl implements Enhancer {
return null;
}
PersistentAttributeTransformer transformer = PersistentAttributeTransformer.collectPersistentFields( managedCtClass, enhancementContext, classPool );
PersistentAttributeTransformer transformer = PersistentAttributeTransformer.collectPersistentFields( managedCtClass, enhancementContext, typePool );
if ( enhancementContext.isEntityClass( managedCtClass ) ) {
log.infof( "Enhancing [%s] as Entity", managedCtClass.getName() );

View File

@ -32,6 +32,7 @@ import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.dynamic.scaffold.TypeValidation;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.pool.TypePool;
/**
* A utility to hold all ByteBuddy related state, as in the current version of
@ -118,19 +119,20 @@ public final class ByteBuddyState {
/**
* Rewrite a class, used by the enhancer.
*
* @param typePool the ByteBuddy TypePool
* @param className The original class name.
* @param originalBytes The original content of the class.
* @param rewriteClassFunction The function used to rewrite the class.
* @return The rewritten content of the class.
*/
public byte[] rewrite(String className, byte[] originalBytes,
public byte[] rewrite(TypePool typePool, String className, byte[] originalBytes,
Function<ByteBuddy, DynamicType.Builder<?>> rewriteClassFunction) {
DynamicType.Builder<?> builder = rewriteClassFunction.apply( byteBuddy );
if ( builder == null ) {
return originalBytes;
}
return make( builder ).getBytes();
return make( typePool, builder ).getBytes();
}
/**
@ -158,12 +160,23 @@ public final class ByteBuddyState {
}
private Unloaded<?> make(DynamicType.Builder<?> builder) {
return make( null, builder );
}
private Unloaded<?> make(TypePool typePool, DynamicType.Builder<?> builder) {
if ( System.getSecurityManager() != null ) {
builder = builder.visit( getDeclaredMethodMemberSubstitution );
builder = builder.visit( getMethodMemberSubstitution );
}
Unloaded<?> unloadedClass = builder.make();
Unloaded<?> unloadedClass;
if ( typePool != null ) {
unloadedClass = builder.make( typePool );
}
else {
unloadedClass = builder.make();
}
if ( DEBUG ) {
try {
unloadedClass.saveIn( new File( System.getProperty( "java.io.tmpdir" ) + "/bytebuddy/" ) );

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.bytecode.internal.bytebuddy;
import java.util.regex.Pattern;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
@ -15,6 +17,8 @@ import javax.persistence.Id;
@Entity(name = "SimpleEntity")
public class SimpleEntity {
private static final Pattern PATTERN = Pattern.compile( "whatever" );
@Id
@GeneratedValue
private Long id;