HHH-10245 - [enhancer] Do not expose ClassPool
This commit is contained in:
parent
d95cecd91b
commit
86727515f4
|
@ -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,17 +43,12 @@ 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() );
|
||||
FieldWriter.addField( managedCtClass, compositeCtType, EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME );
|
||||
final CtClass compositeCtType = loadCtClassFromClass( CompositeOwnerTracker.class );
|
||||
FieldWriter.addField( managedCtClass, compositeCtType, EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME );
|
||||
|
||||
createCompositeTrackerMethod( managedCtClass );
|
||||
}
|
||||
catch (NotFoundException nfe) {
|
||||
nfe.printStackTrace();
|
||||
}
|
||||
createCompositeTrackerMethod( managedCtClass );
|
||||
}
|
||||
|
||||
private void createCompositeTrackerMethod(CtClass managedCtClass) {
|
||||
|
|
|
@ -29,6 +29,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;
|
||||
|
||||
|
@ -49,7 +51,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 );
|
||||
|
@ -86,7 +88,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
|
||||
|
@ -95,7 +97,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
|
||||
|
@ -104,7 +106,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
|
||||
|
@ -112,25 +114,20 @@ 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() ),
|
||||
EnhancerConstants.TRACKER_FIELD_NAME
|
||||
);
|
||||
FieldWriter.addField(
|
||||
managedCtClass,
|
||||
classPool.get( CollectionTracker.class.getName() ),
|
||||
EnhancerConstants.TRACKER_COLLECTION_NAME
|
||||
);
|
||||
FieldWriter.addField(
|
||||
managedCtClass,
|
||||
loadCtClassFromClass( DirtyTracker.class ),
|
||||
EnhancerConstants.TRACKER_FIELD_NAME
|
||||
);
|
||||
FieldWriter.addField(
|
||||
managedCtClass,
|
||||
loadCtClassFromClass( CollectionTracker.class ),
|
||||
EnhancerConstants.TRACKER_COLLECTION_NAME
|
||||
);
|
||||
|
||||
createDirtyTrackerMethods( managedCtClass );
|
||||
}
|
||||
catch (NotFoundException nfe) {
|
||||
nfe.printStackTrace();
|
||||
}
|
||||
createDirtyTrackerMethods( managedCtClass );
|
||||
}
|
||||
|
||||
private void createDirtyTrackerMethods(CtClass managedCtClass) {
|
||||
|
|
|
@ -19,6 +19,7 @@ import javax.persistence.OneToMany;
|
|||
import javax.persistence.OneToOne;
|
||||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtField;
|
||||
import javassist.CtMethod;
|
||||
|
@ -487,6 +488,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;
|
||||
|
@ -572,6 +574,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;
|
||||
|
@ -592,7 +595,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;
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
this.enhancementContext = enhancementContext;
|
||||
this.classPool = buildClassPool( enhancementContext );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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 );
|
||||
|
|
Loading…
Reference in New Issue