HHH-10245 - [enhancer] Do not expose ClassPool

This commit is contained in:
barreiro 2016-03-31 06:15:15 +01:00 committed by Gail Badner
parent ac9748adeb
commit 8dd70236cf
4 changed files with 38 additions and 59 deletions

View File

@ -8,13 +8,13 @@ package org.hibernate.bytecode.enhance.internal;
import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.CtClass; import javassist.CtClass;
import javassist.NotFoundException;
import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker; import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker;
import org.hibernate.bytecode.enhance.spi.EnhancementContext; import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.bytecode.enhance.spi.Enhancer; import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.engine.spi.CompositeOwner; import org.hibernate.engine.spi.CompositeOwner;
import org.hibernate.engine.spi.CompositeTracker; import org.hibernate.engine.spi.CompositeTracker;
import org.hibernate.engine.spi.ManagedComposite;
/** /**
* enhancer for composite (embeddable) entities * enhancer for composite (embeddable) entities
@ -29,7 +29,7 @@ public class CompositeEnhancer extends Enhancer {
public void enhance(CtClass managedCtClass) { public void enhance(CtClass managedCtClass) {
// add the ManagedComposite interface // add the ManagedComposite interface
managedCtClass.addInterface( managedCompositeCtClass ); managedCtClass.addInterface( loadCtClassFromClass( ManagedComposite.class ) );
addInterceptorHandling( managedCtClass ); addInterceptorHandling( managedCtClass );
@ -43,17 +43,12 @@ public class CompositeEnhancer extends Enhancer {
/* --- */ /* --- */
private void addInLineDirtyHandling(CtClass managedCtClass) { private void addInLineDirtyHandling(CtClass managedCtClass) {
try { managedCtClass.addInterface( loadCtClassFromClass( CompositeTracker.class ) );
managedCtClass.addInterface( classPool.get( CompositeTracker.class.getName() ) );
final CtClass compositeCtType = classPool.get( CompositeOwnerTracker.class.getName() ); final CtClass compositeCtType = loadCtClassFromClass( CompositeOwnerTracker.class );
FieldWriter.addField( managedCtClass, compositeCtType, EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME ); FieldWriter.addField( managedCtClass, compositeCtType, EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME );
createCompositeTrackerMethod( managedCtClass ); createCompositeTrackerMethod( managedCtClass );
}
catch (NotFoundException nfe) {
nfe.printStackTrace();
}
} }
private void createCompositeTrackerMethod(CtClass managedCtClass) { private void createCompositeTrackerMethod(CtClass managedCtClass) {

View File

@ -26,6 +26,8 @@ import org.hibernate.bytecode.enhance.spi.EnhancementException;
import org.hibernate.bytecode.enhance.spi.Enhancer; import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoader; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoader;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.SelfDirtinessTracker; import org.hibernate.engine.spi.SelfDirtinessTracker;
@ -46,7 +48,7 @@ public class EntityEnhancer extends Enhancer {
public void enhance(CtClass managedCtClass) { public void enhance(CtClass managedCtClass) {
// add the ManagedEntity interface // add the ManagedEntity interface
managedCtClass.addInterface( managedEntityCtClass ); managedCtClass.addInterface( loadCtClassFromClass( ManagedEntity.class ) );
addEntityInstanceHandling( managedCtClass ); addEntityInstanceHandling( managedCtClass );
addEntityEntryHandling( managedCtClass ); addEntityEntryHandling( managedCtClass );
@ -83,7 +85,7 @@ public class EntityEnhancer extends Enhancer {
private void addEntityEntryHandling(CtClass managedCtClass) { private void addEntityEntryHandling(CtClass managedCtClass) {
FieldWriter.addFieldWithGetterAndSetter( FieldWriter.addFieldWithGetterAndSetter(
managedCtClass, entityEntryCtClass, managedCtClass, loadCtClassFromClass( EntityEntry.class ),
EnhancerConstants.ENTITY_ENTRY_FIELD_NAME, EnhancerConstants.ENTITY_ENTRY_FIELD_NAME,
EnhancerConstants.ENTITY_ENTRY_GETTER_NAME, EnhancerConstants.ENTITY_ENTRY_GETTER_NAME,
EnhancerConstants.ENTITY_ENTRY_SETTER_NAME EnhancerConstants.ENTITY_ENTRY_SETTER_NAME
@ -92,7 +94,7 @@ public class EntityEnhancer extends Enhancer {
private void addLinkedPreviousHandling(CtClass managedCtClass) { private void addLinkedPreviousHandling(CtClass managedCtClass) {
FieldWriter.addFieldWithGetterAndSetter( FieldWriter.addFieldWithGetterAndSetter(
managedCtClass, managedEntityCtClass, managedCtClass, loadCtClassFromClass( ManagedEntity.class ),
EnhancerConstants.PREVIOUS_FIELD_NAME, EnhancerConstants.PREVIOUS_FIELD_NAME,
EnhancerConstants.PREVIOUS_GETTER_NAME, EnhancerConstants.PREVIOUS_GETTER_NAME,
EnhancerConstants.PREVIOUS_SETTER_NAME EnhancerConstants.PREVIOUS_SETTER_NAME
@ -101,7 +103,7 @@ public class EntityEnhancer extends Enhancer {
private void addLinkedNextHandling(CtClass managedCtClass) { private void addLinkedNextHandling(CtClass managedCtClass) {
FieldWriter.addFieldWithGetterAndSetter( FieldWriter.addFieldWithGetterAndSetter(
managedCtClass, managedEntityCtClass, managedCtClass, loadCtClassFromClass( ManagedEntity.class ),
EnhancerConstants.NEXT_FIELD_NAME, EnhancerConstants.NEXT_FIELD_NAME,
EnhancerConstants.NEXT_GETTER_NAME, EnhancerConstants.NEXT_GETTER_NAME,
EnhancerConstants.NEXT_SETTER_NAME EnhancerConstants.NEXT_SETTER_NAME
@ -109,25 +111,20 @@ public class EntityEnhancer extends Enhancer {
} }
private void addInLineDirtyHandling(CtClass managedCtClass) { private void addInLineDirtyHandling(CtClass managedCtClass) {
try { managedCtClass.addInterface( loadCtClassFromClass( SelfDirtinessTracker.class ) );
managedCtClass.addInterface( classPool.get( SelfDirtinessTracker.class.getName() ) );
FieldWriter.addField( FieldWriter.addField(
managedCtClass, managedCtClass,
classPool.get( DirtyTracker.class.getName() ), loadCtClassFromClass( DirtyTracker.class ),
EnhancerConstants.TRACKER_FIELD_NAME EnhancerConstants.TRACKER_FIELD_NAME
); );
FieldWriter.addField( FieldWriter.addField(
managedCtClass, managedCtClass,
classPool.get( CollectionTracker.class.getName() ), loadCtClassFromClass( CollectionTracker.class ),
EnhancerConstants.TRACKER_COLLECTION_NAME EnhancerConstants.TRACKER_COLLECTION_NAME
); );
createDirtyTrackerMethods( managedCtClass ); createDirtyTrackerMethods( managedCtClass );
}
catch (NotFoundException nfe) {
nfe.printStackTrace();
}
} }
private void createDirtyTrackerMethods(CtClass managedCtClass) { private void createDirtyTrackerMethods(CtClass managedCtClass) {

View File

@ -18,6 +18,7 @@ import javax.persistence.OneToMany;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField; import javassist.CtField;
import javassist.CtMethod; import javassist.CtMethod;
@ -476,6 +477,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
CtClass managedCtClass, CtClass managedCtClass,
IdentityHashMap<String, PersistentAttributeAccessMethods> attributeDescriptorMap) { IdentityHashMap<String, PersistentAttributeAccessMethods> attributeDescriptorMap) {
final ConstPool constPool = managedCtClass.getClassFile().getConstPool(); final ConstPool constPool = managedCtClass.getClassFile().getConstPool();
final ClassPool classPool = managedCtClass.getClassPool();
for ( Object oMethod : managedCtClass.getClassFile().getMethods() ) { for ( Object oMethod : managedCtClass.getClassFile().getMethods() ) {
final MethodInfo methodInfo = (MethodInfo) oMethod; final MethodInfo methodInfo = (MethodInfo) oMethod;
@ -561,6 +563,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
*/ */
public void extendedEnhancement(CtClass aCtClass) { public void extendedEnhancement(CtClass aCtClass) {
final ConstPool constPool = aCtClass.getClassFile().getConstPool(); final ConstPool constPool = aCtClass.getClassFile().getConstPool();
final ClassPool classPool = aCtClass.getClassPool();
for ( Object oMethod : aCtClass.getClassFile().getMethods() ) { for ( Object oMethod : aCtClass.getClassFile().getMethods() ) {
final MethodInfo methodInfo = (MethodInfo) oMethod; final MethodInfo methodInfo = (MethodInfo) oMethod;
@ -581,7 +584,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
} }
String fieldName = constPool.getFieldrefName( itr.u16bitAt( index + 1 ) ); String fieldName = constPool.getFieldrefName( itr.u16bitAt( index + 1 ) );
String fieldClassName = constPool.getClassInfo( constPool.getFieldrefClass( itr.u16bitAt( index + 1 ) ) ); String fieldClassName = constPool.getClassInfo( constPool.getFieldrefClass( itr.u16bitAt( index + 1 ) ) );
CtClass targetCtClass = this.classPool.getCtClass( fieldClassName ); CtClass targetCtClass = classPool.getCtClass( fieldClassName );
if ( !enhancementContext.isEntityClass( targetCtClass ) && !enhancementContext.isCompositeClass( targetCtClass ) ) { if ( !enhancementContext.isEntityClass( targetCtClass ) && !enhancementContext.isCompositeClass( targetCtClass ) ) {
continue; continue;

View File

@ -21,7 +21,6 @@ import org.hibernate.bytecode.enhance.internal.CompositeEnhancer;
import org.hibernate.bytecode.enhance.internal.EntityEnhancer; import org.hibernate.bytecode.enhance.internal.EntityEnhancer;
import org.hibernate.bytecode.enhance.internal.FieldWriter; import org.hibernate.bytecode.enhance.internal.FieldWriter;
import org.hibernate.bytecode.enhance.internal.PersistentAttributesEnhancer; import org.hibernate.bytecode.enhance.internal.PersistentAttributesEnhancer;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.ManagedComposite; import org.hibernate.engine.spi.ManagedComposite;
import org.hibernate.engine.spi.ManagedEntity; import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptable;
@ -39,13 +38,7 @@ public class Enhancer {
private static final CoreMessageLogger log = CoreLogging.messageLogger( Enhancer.class ); private static final CoreMessageLogger log = CoreLogging.messageLogger( Enhancer.class );
protected final EnhancementContext enhancementContext; protected final EnhancementContext enhancementContext;
private final ClassPool classPool;
protected final ClassPool classPool;
protected final CtClass managedEntityCtClass;
protected final CtClass managedCompositeCtClass;
protected final CtClass attributeInterceptorCtClass;
protected final CtClass attributeInterceptableCtClass;
protected final CtClass entityEntryCtClass;
/** /**
* Constructs the Enhancer, using the given context. * Constructs the Enhancer, using the given context.
@ -54,20 +47,8 @@ public class Enhancer {
* to contextual/environmental information. * to contextual/environmental information.
*/ */
public Enhancer(EnhancementContext enhancementContext) { public Enhancer(EnhancementContext enhancementContext) {
try { this.enhancementContext = enhancementContext;
this.enhancementContext = enhancementContext; this.classPool = buildClassPool( enhancementContext );
this.classPool = buildClassPool( enhancementContext );
// pre-load some of the interfaces that are used
this.managedEntityCtClass = loadCtClassFromClass( classPool, ManagedEntity.class );
this.managedCompositeCtClass = loadCtClassFromClass( classPool, ManagedComposite.class );
this.attributeInterceptableCtClass = loadCtClassFromClass( classPool, PersistentAttributeInterceptable.class );
this.attributeInterceptorCtClass = loadCtClassFromClass( classPool, PersistentAttributeInterceptor.class );
this.entityEntryCtClass = loadCtClassFromClass( classPool, EntityEntry.class );
}
catch (IOException e) {
throw new EnhancementException( "Could not prepare Javassist ClassPool", e );
}
} }
/** /**
@ -108,11 +89,14 @@ public class Enhancer {
return classPool; return classPool;
} }
private CtClass loadCtClassFromClass(ClassPool cp, Class<?> aClass) throws IOException { protected CtClass loadCtClassFromClass(Class<?> aClass) {
String resourceName = aClass.getName().replace( '.', '/' ) + ".class"; String resourceName = aClass.getName().replace( '.', '/' ) + ".class";
InputStream resourceAsStream = aClass.getClassLoader().getResourceAsStream( resourceName ); InputStream resourceAsStream = aClass.getClassLoader().getResourceAsStream( resourceName );
try { try {
return cp.makeClass( resourceAsStream ); return classPool.makeClass( resourceAsStream );
}
catch (IOException e) {
throw new EnhancementException( "Could not prepare Javassist ClassPool", e );
} }
finally { finally {
try { try {
@ -182,9 +166,9 @@ public class Enhancer {
} }
log.debugf( "Weaving in PersistentAttributeInterceptable implementation on [%s]", managedCtClass.getName() ); log.debugf( "Weaving in PersistentAttributeInterceptable implementation on [%s]", managedCtClass.getName() );
managedCtClass.addInterface( attributeInterceptableCtClass ); managedCtClass.addInterface( loadCtClassFromClass( PersistentAttributeInterceptable.class ) );
FieldWriter.addFieldWithGetterAndSetter( managedCtClass, attributeInterceptorCtClass, FieldWriter.addFieldWithGetterAndSetter( managedCtClass, loadCtClassFromClass( PersistentAttributeInterceptor.class ),
EnhancerConstants.INTERCEPTOR_FIELD_NAME, EnhancerConstants.INTERCEPTOR_FIELD_NAME,
EnhancerConstants.INTERCEPTOR_GETTER_NAME, EnhancerConstants.INTERCEPTOR_GETTER_NAME,
EnhancerConstants.INTERCEPTOR_SETTER_NAME ); EnhancerConstants.INTERCEPTOR_SETTER_NAME );