FileDocCategorySizeDatePackage
CglibClassTransformer.javaAPI DocHibernate 3.2.53925Thu Aug 03 15:35:26 BST 2006org.hibernate.bytecode.cglib

CglibClassTransformer

public class CglibClassTransformer 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 CglibClassTransformer(org.hibernate.bytecode.util.ClassFilter classFilter, org.hibernate.bytecode.util.FieldFilter fieldFilter)


	     
		super( classFilter, fieldFilter );
	
Methods Summary
private org.objectweb.asm.Attribute[]attributes()

		return Attributes.getDefaultAttributes();
	
protected byte[]doTransform(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class classBeingRedefined, java.security.ProtectionDomain protectionDomain, byte[] classfileBuffer)

		ClassReader reader;
		try {
			reader = new ClassReader( new ByteArrayInputStream( classfileBuffer ) );
		}
		catch (IOException e) {
			log.error( "Unable to read class", e );
			throw new HibernateException( "Unable to read class: " + e.getMessage() );
		}

		String[] names = ClassNameReader.getClassInfo( reader );
		ClassWriter w = new DebuggingClassWriter( true );
		ClassTransformer t = getClassTransformer( names );
		if ( t != null ) {
			if ( log.isDebugEnabled() ) {
				log.debug( "Enhancing " + className );
			}
			ByteArrayOutputStream out;
			byte[] result;
			try {
				reader = new ClassReader( new ByteArrayInputStream( classfileBuffer ) );
				new TransformingClassGenerator(
						new ClassReaderGenerator( reader, attributes(), skipDebug() ), t
				).generateClass( w );
				out = new ByteArrayOutputStream();
				out.write( w.toByteArray() );
				result = out.toByteArray();
				out.close();
			}
			catch (Exception e) {
				log.error( "Unable to transform class", e );
				throw new HibernateException( "Unable to transform class: " + e.getMessage() );
			}
			return result;
		}
		return classfileBuffer;
	
private net.sf.cglib.transform.ClassTransformergetClassTransformer(java.lang.String[] classInfo)

		if ( isAlreadyInstrumented( classInfo ) ) {
			return null;
		}
		return new InterceptFieldTransformer(
				new InterceptFieldFilter() {
					public boolean acceptRead(Type owner, String name) {
						return fieldFilter.shouldTransformFieldAccess( classInfo[0], owner.getClassName(), name );
					}

					public boolean acceptWrite(Type owner, String name) {
						return fieldFilter.shouldTransformFieldAccess( classInfo[0], owner.getClassName(), name );
					}
				}
		);
	
private booleanisAlreadyInstrumented(java.lang.String[] classInfo)

		for ( int i = 1; i < classInfo.length; i++ ) {
			if ( InterceptFieldEnabled.class.getName().equals( classInfo[i] ) ) {
				return true;
			}
		}
		return false;
	
private booleanskipDebug()

		return false;