FileDocCategorySizeDatePackage
JavassistClassTransformer.javaAPI DocHibernate 3.2.53563Thu Aug 03 15:35:26 BST 2006org.hibernate.bytecode.javassist

JavassistClassTransformer

public 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.
author
Emmanuel Bernard
author
Steve Ebersole

Fields Summary
private static Log
log
Constructors Summary
public JavassistClassTransformer(org.hibernate.bytecode.util.ClassFilter classFilter, org.hibernate.bytecode.util.FieldFilter fieldFilter)


	     
		super( classFilter, fieldFilter );
	
Methods Summary
private booleanalreadyInstrumented(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 FieldTransformergetFieldTransformer(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 );
					}
				}
		);