1 19 package org.apache.cayenne.enhancer; 20 21 import java.lang.instrument.ClassFileTransformer ; 22 import java.lang.instrument.IllegalClassFormatException ; 23 import java.security.ProtectionDomain ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.objectweb.asm.ClassReader; 28 import org.objectweb.asm.ClassVisitor; 29 import org.objectweb.asm.ClassWriter; 30 31 38 public class Enhancer implements ClassFileTransformer { 39 40 protected Log logger; 41 protected EnhancerVisitorFactory visitorFactory; 42 43 public Enhancer(EnhancerVisitorFactory visitorFactory) { 44 logger = LogFactory.getLog(getClass()); 45 this.visitorFactory = visitorFactory; 46 } 47 48 public byte[] transform( 49 ClassLoader loader, 50 String className, 51 Class <?> classBeingRedefined, 52 ProtectionDomain protectionDomain, 53 byte[] classfileBuffer) throws IllegalClassFormatException { 54 55 ClassReader reader = new ClassReader(classfileBuffer); 56 ClassWriter writer = new ClassWriter(reader, true); 57 58 ClassVisitor visitor = visitorFactory.createVisitor(className, writer); 59 if (visitor == null) { 60 return null; 62 } 63 64 logger.info("enhancing class " + className); 65 reader.accept(visitor, true); 66 return writer.toByteArray(); 67 } 68 } 69 | Popular Tags |