JavassistClassTransformerpublic class JavassistClassTransformer extends org.hibernate.bytecode.AbstractClassTransformerImpl Enhance the classes allowing them to implements InterceptFieldEnabled
This interface is then used by Hibernate for some optimizations. |
Fields Summary |
---|
private static Log | log |
Methods Summary |
---|
private boolean | alreadyInstrumented(javassist.bytecode.ClassFile classfile)
String[] intfs = classfile.getInterfaces();
for ( int i = 0; i < intfs.length; i++ ) {
if ( FieldHandled.class.getName().equals( intfs[i] ) ) {
return true;
}
}
return false;
| protected byte[] | doTransform(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class classBeingRedefined, java.security.ProtectionDomain protectionDomain, byte[] classfileBuffer)
ClassFile classfile;
try {
// WARNING: classfile only
classfile = new ClassFile( new DataInputStream( new ByteArrayInputStream( classfileBuffer ) ) );
}
catch (IOException e) {
log.error( "Unable to build enhancement metamodel for " + className );
return classfileBuffer;
}
FieldTransformer transformer = getFieldTransformer( classfile );
if ( transformer != null ) {
if ( log.isDebugEnabled() ) {
log.debug( "Enhancing " + className );
}
DataOutputStream out = null;
try {
transformer.transform( classfile );
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
out = new DataOutputStream( byteStream );
classfile.write( out );
return byteStream.toByteArray();
}
catch (Exception e) {
log.error( "Unable to transform class", e );
throw new HibernateException( "Unable to transform class: " + e.getMessage() );
}
finally {
try {
if ( out != null ) out.close();
}
catch (IOException e) {
//swallow
}
}
}
return classfileBuffer;
| protected FieldTransformer | getFieldTransformer(javassist.bytecode.ClassFile classfile)
if ( alreadyInstrumented( classfile ) ) {
return null;
}
return new FieldTransformer(
new FieldFilter() {
public boolean handleRead(String desc, String name) {
return fieldFilter.shouldInstrumentField( classfile.getName(), name );
}
public boolean handleWrite(String desc, String name) {
return fieldFilter.shouldInstrumentField( classfile.getName(), name );
}
public boolean handleReadAccess(String fieldOwnerClassName, String fieldName) {
return fieldFilter.shouldTransformFieldAccess( classfile.getName(), fieldOwnerClassName, fieldName );
}
public boolean handleWriteAccess(String fieldOwnerClassName, String fieldName) {
return fieldFilter.shouldTransformFieldAccess( classfile.getName(), fieldOwnerClassName, fieldName );
}
}
);
|
|