HHH-10245 - [enhancer] Do not expose ClassPool

(cherry picked from commit 86727515f4)
This commit is contained in:
barreiro 2016-03-31 06:15:15 +01:00 committed by Gail Badner
parent c43ba563da
commit 3c6ae4112e
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.CtClass;
import javassist.NotFoundException;
import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.engine.spi.CompositeOwner;
import org.hibernate.engine.spi.CompositeTracker;
import org.hibernate.engine.spi.ManagedComposite;
/**
* enhancer for composite (embeddable) entities
@ -29,7 +29,7 @@ public class CompositeEnhancer extends Enhancer {
public void enhance(CtClass managedCtClass) {
// add the ManagedComposite interface
managedCtClass.addInterface( managedCompositeCtClass );
managedCtClass.addInterface( loadCtClassFromClass( ManagedComposite.class ) );
addInterceptorHandling( managedCtClass );
@ -43,18 +43,13 @@ public class CompositeEnhancer extends Enhancer {
/* --- */
private void addInLineDirtyHandling(CtClass managedCtClass) {
try {
managedCtClass.addInterface( classPool.get( CompositeTracker.class.getName() ) );
managedCtClass.addInterface( loadCtClassFromClass( CompositeTracker.class ) );
final CtClass compositeCtType = classPool.get( CompositeOwnerTracker.class.getName() );
final CtClass compositeCtType = loadCtClassFromClass( CompositeOwnerTracker.class );
FieldWriter.addField( managedCtClass, compositeCtType, EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME );
createCompositeTrackerMethod( managedCtClass );
}
catch (NotFoundException nfe) {
nfe.printStackTrace();
}
}
private void createCompositeTrackerMethod(CtClass managedCtClass) {
try {

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

View File

@ -18,6 +18,7 @@ import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
@ -476,6 +477,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
CtClass managedCtClass,
IdentityHashMap<String, PersistentAttributeAccessMethods> attributeDescriptorMap) {
final ConstPool constPool = managedCtClass.getClassFile().getConstPool();
final ClassPool classPool = managedCtClass.getClassPool();
for ( Object oMethod : managedCtClass.getClassFile().getMethods() ) {
final MethodInfo methodInfo = (MethodInfo) oMethod;
@ -561,6 +563,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
*/
public void extendedEnhancement(CtClass aCtClass) {
final ConstPool constPool = aCtClass.getClassFile().getConstPool();
final ClassPool classPool = aCtClass.getClassPool();
for ( Object oMethod : aCtClass.getClassFile().getMethods() ) {
final MethodInfo methodInfo = (MethodInfo) oMethod;
@ -581,7 +584,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
}
String fieldName = constPool.getFieldrefName( 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 ) ) {
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.FieldWriter;
import org.hibernate.bytecode.enhance.internal.PersistentAttributesEnhancer;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.ManagedComposite;
import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
@ -39,13 +38,7 @@ public class Enhancer {
private static final CoreMessageLogger log = CoreLogging.messageLogger( Enhancer.class );
protected final EnhancementContext enhancementContext;
protected final ClassPool classPool;
protected final CtClass managedEntityCtClass;
protected final CtClass managedCompositeCtClass;
protected final CtClass attributeInterceptorCtClass;
protected final CtClass attributeInterceptableCtClass;
protected final CtClass entityEntryCtClass;
private final ClassPool classPool;
/**
* Constructs the Enhancer, using the given context.
@ -54,20 +47,8 @@ public class Enhancer {
* to contextual/environmental information.
*/
public Enhancer(EnhancementContext enhancementContext) {
try {
this.enhancementContext = 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;
}
private CtClass loadCtClassFromClass(ClassPool cp, Class<?> aClass) throws IOException {
protected CtClass loadCtClassFromClass(Class<?> aClass) {
String resourceName = aClass.getName().replace( '.', '/' ) + ".class";
InputStream resourceAsStream = aClass.getClassLoader().getResourceAsStream( resourceName );
try {
return cp.makeClass( resourceAsStream );
return classPool.makeClass( resourceAsStream );
}
catch (IOException e) {
throw new EnhancementException( "Could not prepare Javassist ClassPool", e );
}
finally {
try {
@ -182,9 +166,9 @@ public class Enhancer {
}
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_GETTER_NAME,
EnhancerConstants.INTERCEPTOR_SETTER_NAME );