1 22 package org.jboss.aop; 23 24 import org.jboss.aop.classpool.AOPClassPool; 25 import org.jboss.aop.instrument.Instrumentor; 26 import org.jboss.aop.instrument.InstrumentorFactory; 27 28 import javassist.ByteArrayClassPath; 29 import javassist.CtClass; 30 import javassist.NotFoundException; 31 32 39 public class ClassicWeavingStrategy extends WeavingStrategySupport 40 { 41 private boolean verbose = AspectManager.verbose; 42 45 public byte[] translate(AspectManager manager, String className, ClassLoader loader, byte[] classfileBuffer) throws Exception 46 { 47 if (isReEntry()) 48 { 49 return null; 50 } 51 setReEntry(); 52 manager.transformationStarted = true; 53 try 54 { 55 if (manager.isNonAdvisableClassName(className)) 56 { 57 return null; 58 } 59 AOPClassPool pool = (AOPClassPool) manager.registerClassLoader(loader); 60 CtClass clazz = null; 61 try 62 { 63 clazz = pool.getLocally(className); 64 } 65 catch (NotFoundException e) 66 { 67 ByteArrayClassPath cp = new ByteArrayClassPath(className, classfileBuffer); 70 pool.insertClassPath(cp); 71 clazz = pool.getLocally(className); 72 } 73 if (clazz.isArray()) 74 { 75 if (verbose) System.out.println("[cannot compile] isArray: " + className); 76 pool.flushClass(className); 77 return null; 78 } 79 if (clazz.isInterface()) 80 { 81 if (verbose) System.out.println("[cannot compile] isInterface: " + className); 82 pool.flushClass(className); 83 return null; 84 } 85 if (clazz.isFrozen()) 86 { 87 if (verbose) System.out.println("[warning] isFrozen: " + className); 88 clazz.defrost(); 89 } 90 91 ClassAdvisor advisor = new ClassAdvisor(className, manager); 92 Instrumentor instrumentor = InstrumentorFactory.getInstrumentor( 93 pool, 94 manager, 95 manager.dynamicStrategy.getJoinpointClassifier(), 96 manager.dynamicStrategy.getDynamicTransformationObserver(clazz)); 97 98 if (!Instrumentor.isTransformable(clazz)) 99 { 100 if (verbose) System.out.println("[cannot compile] implements Untransformable: " + className); 101 pool.flushClass(className); 102 return null; 103 } 104 105 manager.attachMetaData(advisor, clazz, true); 106 manager.applyInterfaceIntroductions(advisor, clazz); 107 boolean transformed = instrumentor.transform(clazz, advisor); 108 if (transformed) 109 { 110 pool.lockInCache(clazz); 111 if (AspectManager.debugClasses) 112 { 113 clazz.debugWriteFile(); 114 } 115 116 byte[] rtn = clazz.toBytecode(); 117 if (AspectManager.getPrune()) clazz.prune(); 118 return rtn; 119 } 120 else 121 { 122 pool.soften(clazz); 123 } 124 return null; 125 } 126 catch (Exception ex) 127 { 128 if (!(ex instanceof NotFoundException)) 129 { 130 if (verbose) 131 ex.printStackTrace(); 132 else 133 System.err.println("[error] " + ex.getMessage() + ".. Do verbose mode if you want full stack trace."); 134 135 } 136 throw ex; 137 } 138 finally 139 { 140 clearReEntry(); 141 } 142 } 143 } | Popular Tags |