HHH-7667 - Investigate expanding bytecode enhancement support

(cherry picked from commit 5506a48bea)
This commit is contained in:
Steve Ebersole 2012-10-23 08:28:16 -05:00
parent 889405b3f5
commit 60836cda1b
6 changed files with 270 additions and 46 deletions

View File

@ -23,12 +23,40 @@
*/ */
package org.hibernate.bytecode.enhance.spi; package org.hibernate.bytecode.enhance.spi;
import java.security.ProtectionDomain; import javassist.CtField;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface EnhancementContext { public interface EnhancementContext {
/**
* Does the given class name represent a entity class?
*
* @param className The name of the class to check.
*
* @return {@code true} if the class is an entity; {@code false} otherwise.
*/
public boolean isEntityClass(String className); public boolean isEntityClass(String className);
/**
* Does the given class name represent an embeddable/component class?
*
* @param className The name of the class to check.
*
* @return {@code true} if the class is an embeddable/component; {@code false} otherwise.
*/
public boolean isCompositeClass(String className); public boolean isCompositeClass(String className);
/**
* Does the field represent persistent state? Persistent fields will be "enhanced".
* <p/>
* todo : not sure its a great idea to expose Javassist classes this way.
// may be better to perform basic checks in the caller (non-static, etc) and call out with just the
// Class name and field name...
* @param ctField The field reference.
*
* @return {@code true} if the field is ; {@code false} otherwise.
*/
public boolean isPersistentField(CtField ctField);
} }

View File

@ -28,11 +28,13 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.IdentityHashMap;
import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.ClassPool; import javassist.ClassPool;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField; import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod; import javassist.CtNewMethod;
import javassist.Modifier; import javassist.Modifier;
import javassist.bytecode.AnnotationsAttribute; import javassist.bytecode.AnnotationsAttribute;
@ -44,7 +46,7 @@ import org.jboss.logging.Logger;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.EnhancementException; import org.hibernate.bytecode.enhance.EnhancementException;
import org.hibernate.bytecode.internal.javassist.FieldHandled; import org.hibernate.engine.spi.Managed;
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.EntityEntry; import org.hibernate.engine.spi.EntityEntry;
@ -57,6 +59,9 @@ import org.hibernate.internal.CoreMessageLogger;
public class Enhancer { public class Enhancer {
private static final CoreMessageLogger log = Logger.getMessageLogger( CoreMessageLogger.class, Enhancer.class.getName() ); private static final CoreMessageLogger log = Logger.getMessageLogger( CoreMessageLogger.class, Enhancer.class.getName() );
private static final String PERSISTENT_FIELD_READER_PREFIX = "$hibernate_read_";
private static final String PERSISTENT_FIELD_WRITER_PREFIX = "$hibernate_write_";
public static final String ENTITY_INSTANCE_GETTER_NAME = "hibernate_getEntityInstance"; public static final String ENTITY_INSTANCE_GETTER_NAME = "hibernate_getEntityInstance";
public static final String ENTITY_ENTRY_FIELD_NAME = "$hibernate_entityEntryHolder"; public static final String ENTITY_ENTRY_FIELD_NAME = "$hibernate_entityEntryHolder";
@ -168,7 +173,7 @@ public class Enhancer {
final String[] interfaceNames = managedCtClass.getClassFile2().getInterfaces(); final String[] interfaceNames = managedCtClass.getClassFile2().getInterfaces();
for ( String interfaceName : interfaceNames ) { for ( String interfaceName : interfaceNames ) {
if ( FieldHandled.class.getName().equals( interfaceName ) ) { if ( Managed.class.getName().equals( interfaceName ) ) {
log.debug( "skipping enhancement : already enhanced" ); log.debug( "skipping enhancement : already enhanced" );
return; return;
} }
@ -191,13 +196,18 @@ public class Enhancer {
// add the ManagedEntity interface // add the ManagedEntity interface
managedCtClass.addInterface( managedEntityCtClass ); managedCtClass.addInterface( managedEntityCtClass );
addEntityInstanceHandling( managedCtClass, constPool ); // enhancePersistentAttributes( managedCtClass );
addEntityEntryHandling( managedCtClass, constPool );
addLinkedPreviousHandling( managedCtClass, constPool ); addEntityInstanceHandling( managedCtClass );
addLinkedNextHandling( managedCtClass, constPool ); addEntityEntryHandling( managedCtClass );
addLinkedPreviousHandling( managedCtClass );
addLinkedNextHandling( managedCtClass );
} }
private void addEntityInstanceHandling(CtClass managedCtClass, ConstPool constPool) { private void enhanceAsComposite(CtClass classFile) {
}
private void addEntityInstanceHandling(CtClass managedCtClass) {
// add the ManagedEntity#hibernate_getEntityInstance method // add the ManagedEntity#hibernate_getEntityInstance method
try { try {
managedCtClass.addMethod( managedCtClass.addMethod(
@ -224,7 +234,9 @@ public class Enhancer {
// essentially add `return this;` // essentially add `return this;`
} }
private void addEntityEntryHandling(CtClass managedCtClass, ConstPool constPool) { private void addEntityEntryHandling(CtClass managedCtClass) {
final ConstPool constPool = managedCtClass.getClassFile().getConstPool();
// add field to hold EntityEntry // add field to hold EntityEntry
final CtField entityEntryField; final CtField entityEntryField;
try { try {
@ -243,6 +255,7 @@ public class Enhancer {
// make that new field transient and @Transient // make that new field transient and @Transient
entityEntryField.setModifiers( entityEntryField.getModifiers() | Modifier.TRANSIENT ); entityEntryField.setModifiers( entityEntryField.getModifiers() | Modifier.TRANSIENT );
entityEntryField.setModifiers( Modifier.setPrivate( entityEntryField.getModifiers() ) );
AnnotationsAttribute annotationsAttribute = getVisibleAnnotations( entityEntryField.getFieldInfo() ); AnnotationsAttribute annotationsAttribute = getVisibleAnnotations( entityEntryField.getFieldInfo() );
annotationsAttribute.addAnnotation( new Annotation( Transient.class.getName(), constPool ) ); annotationsAttribute.addAnnotation( new Annotation( Transient.class.getName(), constPool ) );
@ -279,11 +292,13 @@ public class Enhancer {
} }
} }
private void addLinkedPreviousHandling(CtClass managedCtClass, ConstPool constPool) { private void addLinkedPreviousHandling(CtClass managedCtClass) {
final ConstPool constPool = managedCtClass.getClassFile().getConstPool();
// add field to hold "previous" ManagedEntity // add field to hold "previous" ManagedEntity
final CtField previousField; final CtField previousField;
try { try {
previousField = new CtField( managedCtClass, PREVIOUS_FIELD_NAME, managedCtClass ); previousField = new CtField( managedEntityCtClass, PREVIOUS_FIELD_NAME, managedCtClass );
managedCtClass.addField( previousField ); managedCtClass.addField( previousField );
} }
catch (CannotCompileException e) { catch (CannotCompileException e) {
@ -298,6 +313,7 @@ public class Enhancer {
// make that new field transient and @Transient // make that new field transient and @Transient
previousField.setModifiers( previousField.getModifiers() | Modifier.TRANSIENT ); previousField.setModifiers( previousField.getModifiers() | Modifier.TRANSIENT );
previousField.setModifiers( Modifier.setPrivate( previousField.getModifiers() ) );
AnnotationsAttribute annotationsAttribute = getVisibleAnnotations( previousField.getFieldInfo() ); AnnotationsAttribute annotationsAttribute = getVisibleAnnotations( previousField.getFieldInfo() );
annotationsAttribute.addAnnotation( new Annotation( Transient.class.getName(), constPool ) ); annotationsAttribute.addAnnotation( new Annotation( Transient.class.getName(), constPool ) );
@ -330,11 +346,13 @@ public class Enhancer {
} }
} }
private void addLinkedNextHandling(CtClass managedCtClass, ConstPool constPool) { private void addLinkedNextHandling(CtClass managedCtClass) {
final ConstPool constPool = managedCtClass.getClassFile().getConstPool();
// add field to hold "next" ManagedEntity // add field to hold "next" ManagedEntity
final CtField nextField; final CtField nextField;
try { try {
nextField = new CtField( managedCtClass, NEXT_FIELD_NAME, managedCtClass ); nextField = new CtField( managedEntityCtClass, NEXT_FIELD_NAME, managedCtClass );
managedCtClass.addField( nextField ); managedCtClass.addField( nextField );
} }
catch (CannotCompileException e) { catch (CannotCompileException e) {
@ -347,8 +365,9 @@ public class Enhancer {
); );
} }
// make that new field transient and @Transient // make that new field (1) private, (2) transient and (3) @Transient
nextField.setModifiers( nextField.getModifiers() | Modifier.TRANSIENT ); nextField.setModifiers( nextField.getModifiers() | Modifier.TRANSIENT );
nextField.setModifiers( Modifier.setPrivate( nextField.getModifiers() ) );
AnnotationsAttribute annotationsAttribute = getVisibleAnnotations( nextField.getFieldInfo() ); AnnotationsAttribute annotationsAttribute = getVisibleAnnotations( nextField.getFieldInfo() );
annotationsAttribute.addAnnotation( new Annotation( Transient.class.getName(), constPool ) ); annotationsAttribute.addAnnotation( new Annotation( Transient.class.getName(), constPool ) );
@ -390,8 +409,84 @@ public class Enhancer {
return annotationsAttribute; return annotationsAttribute;
} }
private void enhanceAsComposite(CtClass classFile) { private void enhancePersistentAttributes(CtClass managedCtClass) {
final IdentityHashMap<CtField,FieldVirtualReadWritePair> fieldToMethodsXref = new IdentityHashMap<CtField, FieldVirtualReadWritePair>();
for ( CtField ctField : managedCtClass.getFields() ) {
if ( ! enhancementContext.isPersistentField( ctField ) ) {
continue;
}
final FieldVirtualReadWritePair methodPair = addReadAndWriteMethod( managedCtClass, ctField );
fieldToMethodsXref.put( ctField, methodPair );
}
transformFieldAccessesIntoReadsAndWrites( managedCtClass, fieldToMethodsXref );
} }
private FieldVirtualReadWritePair addReadAndWriteMethod(CtClass managedCtClass, CtField persistentField) {
// add the "reader"
final CtMethod reader = generateFieldReader( managedCtClass, persistentField );
// add the "writer"
final CtMethod writer = generateFieldWriter( managedCtClass, persistentField );
return new FieldVirtualReadWritePair( reader, writer );
}
private CtMethod generateFieldReader(CtClass managedCtClass, CtField persistentField) {
// todo : temporary; still need to add hooks into lazy-loading
try {
final String name = PERSISTENT_FIELD_READER_PREFIX + persistentField.getName();
CtMethod reader = CtNewMethod.getter( name, persistentField );
managedCtClass.addMethod( reader );
return reader;
}
catch (CannotCompileException e) {
throw new EnhancementException(
String.format(
"Could not enhance entity class [%s] to add virtual reader method for field [%s]",
managedCtClass.getName(),
persistentField.getName()
),
e
);
}
}
private CtMethod generateFieldWriter(CtClass managedCtClass, CtField persistentField) {
// todo : temporary; still need to add hooks into lazy-loading and dirtying
try {
final String name = PERSISTENT_FIELD_WRITER_PREFIX + persistentField.getName();
CtMethod writer = CtNewMethod.setter( name, persistentField );
managedCtClass.addMethod( writer );
return writer;
}
catch (CannotCompileException e) {
throw new EnhancementException(
String.format(
"Could not enhance entity class [%s] to add virtual writer method for field [%s]",
managedCtClass.getName(),
persistentField.getName()
),
e
);
}
}
private void transformFieldAccessesIntoReadsAndWrites(
CtClass managedCtClass,
IdentityHashMap<CtField, FieldVirtualReadWritePair> fieldToMethodsXref) {
}
private static class FieldVirtualReadWritePair {
private final CtMethod readMethod;
private final CtMethod writeMethod;
private FieldVirtualReadWritePair(CtMethod readMethod, CtMethod writeMethod) {
this.readMethod = readMethod;
this.writeMethod = writeMethod;
}
}
} }

View File

@ -24,6 +24,7 @@
package org.hibernate.tool.enhance; package org.hibernate.tool.enhance;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Transient;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -33,7 +34,10 @@ import java.util.List;
import javassist.ClassPool; import javassist.ClassPool;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField;
import javassist.NotFoundException; import javassist.NotFoundException;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.annotation.Annotation;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
@ -74,6 +78,12 @@ public class EnhancementTask extends Task {
public boolean isCompositeClass(String className) { public boolean isCompositeClass(String className) {
return false; return false;
} }
@Override
public boolean isPersistentField(CtField ctField) {
// current check is to look for @Transient
return ! ctField.hasAnnotation( Transient.class );
}
}; };
// we use the CtClass stuff here just as a simple vehicle for obtaining low level information about // we use the CtClass stuff here just as a simple vehicle for obtaining low level information about

View File

@ -36,6 +36,7 @@ import java.util.Arrays;
import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.ClassPool; import javassist.ClassPool;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField;
import javassist.LoaderClassPath; import javassist.LoaderClassPath;
import javassist.NotFoundException; import javassist.NotFoundException;
@ -44,68 +45,92 @@ import org.hibernate.LockMode;
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.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.Status; import org.hibernate.engine.spi.Status;
import org.junit.Test; import org.junit.Test;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.testing.junit4.ExtraAssertions;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class EnhancerTest extends BaseUnitTestCase { public class EnhancerTest extends BaseUnitTestCase {
@Test private static EnhancementContext enhancementContext = new EnhancementContext() {
public void testEnhancement() @Override
throws IOException, CannotCompileException, NotFoundException, public boolean isEntityClass(String className) {
NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { return true;
EnhancementContext enhancementContext = new EnhancementContext() { }
@Override
public boolean isEntityClass(String className) {
return true;
}
@Override @Override
public boolean isCompositeClass(String className) { public boolean isCompositeClass(String className) {
return false; return false;
} }
};
@Override
public boolean isPersistentField(CtField ctField) {
return true;
}
};
@Test
public void testEnhancement() throws Exception {
testFor( SimpleEntity.class );
testFor( SubEntity.class );
}
private void testFor(Class entityClassToEnhance) throws Exception {
Enhancer enhancer = new Enhancer( enhancementContext ); Enhancer enhancer = new Enhancer( enhancementContext );
CtClass anEntityCtClass = generateCtClassForAnEntity(); CtClass entityCtClass = generateCtClassForAnEntity( entityClassToEnhance );
byte[] original = anEntityCtClass.toBytecode(); byte[] original = entityCtClass.toBytecode();
byte[] enhanced = enhancer.enhance( anEntityCtClass.getName(), original ); byte[] enhanced = enhancer.enhance( entityCtClass.getName(), original );
assertFalse( "entity was not enhanced", Arrays.equals( original, enhanced ) ); assertFalse( "entity was not enhanced", Arrays.equals( original, enhanced ) );
ClassLoader cl = new ClassLoader() { }; ClassLoader cl = new ClassLoader() { };
ClassPool cp2 = new ClassPool( false ); ClassPool cp2 = new ClassPool( false );
cp2.appendClassPath( new LoaderClassPath( cl ) ); cp2.appendClassPath( new LoaderClassPath( cl ) );
CtClass enhancedCtClass = cp2.makeClass( new ByteArrayInputStream( enhanced ) ); CtClass enhancedCtClass = cp2.makeClass( new ByteArrayInputStream( enhanced ) );
Class simpleEntityClass = enhancedCtClass.toClass( cl, this.getClass().getProtectionDomain() ); Class entityClass = enhancedCtClass.toClass( cl, this.getClass().getProtectionDomain() );
Object simpleEntityInstance = simpleEntityClass.newInstance(); Object entityInstance = entityClass.newInstance();
assertTyping( ManagedEntity.class, entityInstance );
// call the new methods // call the new methods
Method setter = simpleEntityClass.getMethod( Enhancer.ENTITY_ENTRY_SETTER_NAME, EntityEntry.class ); //
Method getter = simpleEntityClass.getMethod( Enhancer.ENTITY_ENTRY_GETTER_NAME ); Method setter = entityClass.getMethod( Enhancer.ENTITY_ENTRY_SETTER_NAME, EntityEntry.class );
assertNull( getter.invoke( simpleEntityInstance ) ); Method getter = entityClass.getMethod( Enhancer.ENTITY_ENTRY_GETTER_NAME );
setter.invoke( simpleEntityInstance, makeEntityEntry() ); assertNull( getter.invoke( entityInstance ) );
assertNotNull( getter.invoke( simpleEntityInstance ) ); setter.invoke( entityInstance, makeEntityEntry() );
setter.invoke( simpleEntityInstance, new Object[] { null } ); assertNotNull( getter.invoke( entityInstance ) );
assertNull( getter.invoke( simpleEntityInstance ) ); setter.invoke( entityInstance, new Object[] {null} );
assertNull( getter.invoke( entityInstance ) );
Method entityInstanceGetter = simpleEntityClass.getMethod( Enhancer.ENTITY_INSTANCE_GETTER_NAME ); Method entityInstanceGetter = entityClass.getMethod( Enhancer.ENTITY_INSTANCE_GETTER_NAME );
assertSame( simpleEntityInstance, entityInstanceGetter.invoke( simpleEntityInstance ) ); assertSame( entityInstance, entityInstanceGetter.invoke( entityInstance ) );
Method previousGetter = entityClass.getMethod( Enhancer.PREVIOUS_GETTER_NAME );
Method previousSetter = entityClass.getMethod( Enhancer.PREVIOUS_SETTER_NAME, ManagedEntity.class );
previousSetter.invoke( entityInstance, entityInstance );
assertSame( entityInstance, previousGetter.invoke( entityInstance ) );
Method nextGetter = entityClass.getMethod( Enhancer.PREVIOUS_GETTER_NAME );
Method nextSetter = entityClass.getMethod( Enhancer.PREVIOUS_SETTER_NAME, ManagedEntity.class );
nextSetter.invoke( entityInstance, entityInstance );
assertSame( entityInstance, nextGetter.invoke( entityInstance ) );
} }
private CtClass generateCtClassForAnEntity() throws IOException, NotFoundException { private CtClass generateCtClassForAnEntity(Class entityClassToEnhance) throws Exception {
ClassPool cp = new ClassPool( false ); ClassPool cp = new ClassPool( false );
return cp.makeClass( return cp.makeClass(
getClass().getClassLoader().getResourceAsStream( getClass().getClassLoader().getResourceAsStream(
SimpleEntity.class.getName().replace( '.', '/' ) + ".class" entityClassToEnhance.getName().replace( '.', '/' ) + ".class"
) )
); );
} }

View File

@ -0,0 +1,33 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.bytecode.enhancement;
import javax.persistence.Entity;
/**
* @author Steve Ebersole
*/
@Entity
public class SubEntity extends SuperEntity {
}

View File

@ -0,0 +1,33 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.bytecode.enhancement;
import javax.persistence.Entity;
/**
* @author Steve Ebersole
*/
@Entity
public class SuperEntity {
}