1 22 package org.jboss.aop.instrument; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import org.jboss.aop.ClassAdvisor; 27 import org.jboss.aop.pointcut.Pointcut; 28 29 import javassist.CannotCompileException; 30 import javassist.CtClass; 31 import javassist.CtConstructor; 32 import javassist.CtField; 33 import javassist.Modifier; 34 import javassist.NotFoundException; 35 36 42 public abstract class ConstructionTransformer 43 { 44 final static String CONSTRUCTION_INFO_CLASS_NAME = "org.jboss.aop.ConstructionInfo"; 45 protected Instrumentor instrumentor; 46 47 protected ConstructionTransformer(Instrumentor instrumentor) 48 { 49 this.instrumentor = instrumentor; 50 } 51 52 55 protected String addConstructionInfoField(int modifiers, CtClass addTo, CtConstructor ctor, int index) throws NotFoundException, CannotCompileException 56 { 57 return addConstructionInfoField(modifiers, addTo, ctor, index, null); 58 } 59 60 63 protected String addConstructionInfoField(int modifiers, CtClass addTo, CtConstructor ctor, int index, CtField.Initializer init) throws NotFoundException, CannotCompileException 64 { 65 String name = getConstructionInfoFieldName(ctor.getDeclaringClass().getSimpleName(), index); 66 67 if (instrumentor.getClassPool() != null) 70 { 71 try 72 { 73 addTo.getDeclaredField(name); 74 return name; 75 } 76 catch(NotFoundException e) 77 { 78 } 79 TransformerCommon.addInfoField(instrumentor, CONSTRUCTION_INFO_CLASS_NAME, name, modifiers, addTo, addInfoAsWeakReference(), init); 80 81 } 82 return name; 83 } 84 85 protected boolean addInfoAsWeakReference() 86 { 87 return true; 88 } 89 90 public static String getConstructionInfoFieldName(String classname, int index) 91 { 92 if (classname.indexOf(".") >= 0) 93 { 94 throw new RuntimeException ("Use simple class name for construction info field name: " + classname); 95 } 96 return "aop$constructionInfo_" + classname.replace('.','$') + "_" + index; 97 } 98 99 protected static String constructionInfoFromWeakReference(String localName, String ctorInfoName) 100 { 101 return TransformerCommon.infoFromWeakReference(CONSTRUCTION_INFO_CLASS_NAME, localName, ctorInfoName); 102 } 103 104 public boolean insertConstructionInterception(CtClass clazz, ClassAdvisor advisor) 106 throws Exception 107 { 108 if (!advisor.getManager().isConstruction()) return false; 109 110 boolean oneMatch = false; 111 List constructors = instrumentor.getConstructors(clazz); 112 113 Iterator it = constructors.iterator(); 114 for (int index = 0; it.hasNext(); index++) 115 { 116 CtConstructor constructor = (CtConstructor) it.next(); 118 if (constructor.isClassInitializer() || !isAdvisableConstructor(constructor, advisor)) 119 { 120 if (oneMatch) 121 { 122 generateNotMatchedConstructionInfoField(constructor, index); 124 } 125 continue; 126 } 127 128 if (!oneMatch) 129 { 130 oneMatch = true; 131 instrumentor.setupBasics(clazz); 132 133 for (int j = 0 ; j < index ; j++) 134 { 135 generateNotMatchedConstructionInfoField((CtConstructor)constructors.get(j), j); 136 } 137 } 138 139 generateConstructionInfoField(constructor, index); 140 insertInterception(constructor, index); 141 } 142 return oneMatch; 143 } 144 145 protected abstract void insertInterception(CtConstructor constructor, int index) throws Exception ; 146 147 public static boolean isAdvisableConstructor(CtConstructor con, ClassAdvisor advisor) throws NotFoundException 149 { 150 Iterator pointcuts = advisor.getManager().getPointcuts().values().iterator(); 151 while (pointcuts.hasNext()) 152 { 153 Pointcut pointcut = (Pointcut) pointcuts.next(); 154 if (pointcut.matchesConstruction(advisor, con)) 155 { 156 return true; 157 } 158 } 159 return false; 160 } 161 162 protected void generateConstructionInfoField(CtConstructor constructor, int index) throws NotFoundException, CannotCompileException 163 { 164 addConstructionInfoField(Modifier.STATIC | Modifier.PRIVATE, 165 constructor.getDeclaringClass(), 166 constructor, 167 index); 168 } 169 170 protected void generateNotMatchedConstructionInfoField(CtConstructor constructor, int index) throws NotFoundException, CannotCompileException 171 { 172 173 } 174 175 } | Popular Tags |