1 22 package org.jboss.aop.instrument; 23 24 import javassist.CannotCompileException; 25 import javassist.CtClass; 26 import javassist.CtConstructor; 27 import javassist.CtField; 28 import javassist.CtMethod; 29 import javassist.CtNewMethod; 30 import javassist.Modifier; 31 import javassist.NotFoundException; 32 33 import org.jboss.aop.util.JavassistMethodHashing; 34 35 41 public class GeneratedAdvisorConstructorExecutionTransformer extends 42 ConstructorExecutionTransformer 43 { 44 45 public GeneratedAdvisorConstructorExecutionTransformer(Instrumentor instrumentor) 46 { 47 super(instrumentor); 48 } 49 50 protected boolean addInfoAsWeakReference() 51 { 52 return false; 53 } 54 55 protected void generateConstructorInfoField(CtClass clazz, CtConstructor constructor, int index) throws CannotCompileException, NotFoundException 56 { 57 String ciname = addConstructorInfoField( 58 Modifier.PROTECTED, 59 ((GeneratedAdvisorInstrumentor)getInstrumentor()).getGenadvisor(), 60 getConstructorInfoFieldName(clazz.getSimpleName(), index)); 61 62 addJoinpoint(clazz, constructor, ciname, index); 63 64 final long wrapperHash = JavassistMethodHashing.methodHash( 65 clazz.getDeclaredMethod( 66 constructorFactory(clazz.getSimpleName()), 67 constructor.getParameterTypes())); 68 final long constructorHash = JavassistMethodHashing.constructorHash(constructor); 69 70 ((GeneratedAdvisorInstrumentor)getInstrumentor()).initialiseConstructorInfoField(ciname, index, constructorHash, wrapperHash); 71 } 72 73 private void addJoinpoint(CtClass clazz, CtConstructor constructor, String ciname, int index)throws CannotCompileException, NotFoundException 74 { 75 CtClass joinpoint = createJoinpointClass(clazz, constructor, ciname, index); 76 CtClass genadvisor = ((GeneratedAdvisorInstrumentor)getInstrumentor()).getGenadvisor(); 77 CtField field = new CtField( 78 joinpoint, 79 ConstructorJoinPointGenerator.getInfoFieldName(clazz.getSimpleName(), index), 80 genadvisor); 81 field.setModifiers(Modifier.PROTECTED); 82 genadvisor.addField(field); 83 } 84 85 private CtClass createJoinpointClass(CtClass clazz, CtConstructor constructor, String ciname, int index) throws CannotCompileException, NotFoundException 86 { 87 return ConstructorJoinPointGenerator.createJoinpointBaseClass( 88 (GeneratedAdvisorInstrumentor)getInstrumentor(), 89 clazz, 90 constructor, 91 ciname, 92 index); 93 } 94 95 protected void initialiseWrapper(int mod, CtConstructor constructor, int index) throws NotFoundException, CannotCompileException 96 { 97 GeneratedAdvisorInstrumentor instrumentor = (GeneratedAdvisorInstrumentor)getInstrumentor(); 98 CtClass genadvisor = getGenAdvisor(); 99 CtClass clazz = constructor.getDeclaringClass(); 100 101 CtClass[] exceptions = constructor.getExceptionTypes(); 102 CtClass type = constructor.getDeclaringClass(); 103 104 CtMethod innerWmethod = CtNewMethod.make( 105 clazz, 106 getInnerWrapperMethodName(constructor), 107 constructor.getParameterTypes(), 108 exceptions, 109 null, 110 genadvisor); 111 innerWmethod.setModifiers(Modifier.clear(mod, Modifier.STATIC)); 112 setTemporaryWrapperCode(type, innerWmethod); 113 genadvisor.addMethod(innerWmethod); 114 115 CtMethod wmethod = CtNewMethod.make( 116 clazz, 117 constructorFactory(clazz.getSimpleName()), 118 constructor.getParameterTypes(), 119 exceptions, 120 null, 121 clazz); 122 wmethod.setModifiers(mod); 123 wmethod.setBody("{return ((" + instrumentor.getAdvisorFQN(clazz) + 124 ")" + Instrumentor.HELPER_FIELD_NAME + ")." + getInnerWrapperMethodName(constructor) + "($$);}"); 125 clazz.addMethod(wmethod); 126 127 getWrapper().prepareForWrapping(constructor, CONSTRUCTOR_STATUS); 129 } 130 131 protected void setEmptyWrapperCode(CtConstructor constructor)throws NotFoundException 132 { 133 CtClass clazz = constructor.getDeclaringClass(); 134 135 136 String innerCode = 137 "{ " + 138 " return new " + clazz.getName() + "($$); " + 139 "}"; 140 try 141 { 142 CtMethod innerWrapperMethod = getInnerWrapperMethod(constructor); 143 innerWrapperMethod.setBody(innerCode); 144 } 145 catch (CannotCompileException e) 146 { 147 System.out.println(innerCode); 148 throw new RuntimeException (e); } 150 151 try 152 { 153 CtMethod outerWrappedMethod = getWrapperMethod(constructor); 154 outerWrappedMethod.setBody(outerDelegatingBody(constructor)); 155 } 156 catch(CannotCompileException e) 157 { 158 System.out.println(outerDelegatingBody(constructor)); 159 throw new RuntimeException (e); } 161 162 } 163 164 protected void setEmptyWrapperCodeLater(CtConstructor constructor) throws NotFoundException 165 { 166 CtMethod outerWrapperMethod = getWrapperMethod(constructor); 167 this.codifier.addPendingCode(outerWrapperMethod, outerDelegatingBody(constructor)); 168 169 CtMethod innerWrapperMethod = getInnerWrapperMethod(constructor); 170 String innerCode = 171 "{ " + 172 " return new " + constructor.getDeclaringClass().getName() + "($$); " + 173 "}"; 174 this.codifier.addPendingCode(innerWrapperMethod, innerCode); 175 } 176 177 private CtMethod getInnerWrapperMethod(CtConstructor constructor)throws NotFoundException 178 { 179 CtClass genadvisor = getGenAdvisor(); 180 return genadvisor.getDeclaredMethod( 181 getInnerWrapperMethodName(constructor), 182 constructor.getParameterTypes()); 183 184 } 185 186 private String getInnerWrapperMethodName(CtConstructor constructor) 187 { 188 return constructorFactory(((GeneratedAdvisorInstrumentor)getInstrumentor()).getAdvisorName(constructor.getDeclaringClass())); 189 } 190 191 private String outerDelegatingBody(CtConstructor constructor) 192 { 193 return "{return ((" + 194 ((GeneratedAdvisorInstrumentor)getInstrumentor()).getAdvisorFQN(constructor.getDeclaringClass()) + 195 ")" + Instrumentor.HELPER_FIELD_NAME + ")." + getInnerWrapperMethodName(constructor) + "($$);}"; 196 } 197 198 private CtClass getGenAdvisor() 199 { 200 return ((GeneratedAdvisorInstrumentor)getInstrumentor()).getGenadvisor(); 201 } 202 203 protected void createWrapper(ConstructorTransformation trans) 204 throws CannotCompileException, NotFoundException 205 { 206 CtMethod outerWrapper = getWrapperMethod(trans.getConstructor()); 207 outerWrapper.setBody(outerDelegatingBody(trans.getConstructor())); 208 209 CtMethod innerWrapper = getInnerWrapperMethod(trans.getConstructor()); 210 211 String infoName = ConstructorJoinPointGenerator.getInfoFieldName( 212 trans.getConstructor().getDeclaringClass().getSimpleName(), 213 trans.getIndex()); 214 215 String generatorName = ConstructorJoinPointGenerator.getJoinPointGeneratorFieldName( 216 trans.getConstructor().getDeclaringClass().getSimpleName(), 217 trans.getIndex()); 218 219 String code = 220 "{" + 221 " if (" + infoName + " == null && " + generatorName + " != null)" + 222 " {" + 223 " " + generatorName + "." + JoinPointGenerator.GENERATE_JOINPOINT_CLASS + "();" + 224 " }" + 225 " if (" + infoName + " == null)" + 226 " { " + 227 " return new " + trans.getClassName() + "($$); " + 228 " }" + 229 " else" + 230 " {" + 231 " return " + infoName + "." + JoinPointGenerator.INVOKE_JOINPOINT + "($$);" + 232 " }" + 233 "}"; 234 235 codifier.addPendingCode(innerWrapper, code); 236 } 237 238 239 240 241 } 242 | Popular Tags |