1 22 package org.jboss.aop.instrument; 23 24 import javassist.CannotCompileException; 25 import javassist.CtClass; 26 import javassist.Modifier; 27 import javassist.NotFoundException; 28 29 import org.jboss.aop.AspectManager; 30 import org.jboss.aop.ClassAdvisor; 31 32 38 public class OptimizedCallerTransformer extends CallerTransformer 39 { 40 public OptimizedCallerTransformer(Instrumentor instrumentor, AspectManager manager) 41 { 42 super(instrumentor, manager, true, new ClassicCallerInfoAdder(instrumentor)); 43 } 44 45 protected CallerExprEditor callerExprEditorFactory(ClassAdvisor advisor, CtClass clazz) 46 { 47 return new OptimizedCallerExprEditor(advisor, clazz); 48 } 49 50 class OptimizedCallerExprEditor extends CallerExprEditor 51 { 52 53 public OptimizedCallerExprEditor(ClassAdvisor advisor, CtClass callingClass) 54 { 55 super(advisor, callingClass); 56 } 57 58 protected void setupConstructor(ConstructorDetail cd)throws NotFoundException, CannotCompileException 59 { 60 if (callerInfos.get(cd.callerInfoField) == null) 61 { 62 String createName = getOptimizedMethodCalledByConstructorClassName(cd.callingIndex, callingClass.getName(), cd.calledHash); 63 String invocationClassName = OptimizedCallerInvocations.createOptimizedMethodCalledByConInvocationClass(instrumentor, createName, callingClass, cd.calledMethod, cd.callingIndex, cd.calledHash); 64 65 callerInfos.put(cd.callerInfoField, invocationClassName); 66 callerInfoAdder.addMethodByConInfoField(callingClass, cd.callerInfoField, cd.callingIndex, cd.classname, cd.calledHash); 67 } 68 } 69 70 protected void setupMethod(MethodDetail md) throws NotFoundException, CannotCompileException 71 { 72 if (callerInfos.get(md.callerInfoField) == null) 73 { 74 String createName = getOptimizedMethodCalledByMethodClassName(md.callingHash, callingClass.getName(), md.calledHash); 75 String invocationClassName = OptimizedCallerInvocations.createOptimizedMethodCalledByMethodInvocationClass(instrumentor, createName, callingClass, md.calledMethod, md.callingHash, md.calledHash); 76 77 callerInfos.put(md.callerInfoField, invocationClassName); 78 callerInfoAdder.addMethodByMethodInfoField(callingClass, md.callerInfoField, md.callingHash, md.classname, md.calledHash); 79 } 80 } 81 82 protected void setupMethod(ConByMethodDetail cd) throws NotFoundException, CannotCompileException 83 { 84 if (callerInfos.get(cd.callerInfoField) == null) 85 { 86 if (!cd.isTgtConAdvised) 89 { 90 String createName = getOptimizedConCalledByMethodInvocationClassName(cd.callingHash, callingClass.getName(), cd.calledHash); 91 String invocationClassName = OptimizedCallerInvocations.createOptimizedConCalledByMethodInvocationClass(instrumentor, createName, callingClass, cd.calledConstructor, cd.callingHash, cd.calledHash); 92 callerInfos.put(cd.callerInfoField, invocationClassName); 93 } 94 else 95 { 96 callerInfos.put(cd.callerInfoField, NonOptimizedCallerTransformer.PLACEHOLDER); 97 } 98 99 callerInfoAdder.addConByMethodInfoField(callingClass, cd.callerInfoField, cd.callingHash, cd.classname, cd.calledHash); 100 } 101 } 102 103 protected void setupConstructor(ConByConDetail cd)throws NotFoundException, CannotCompileException 104 { 105 if (callerInfos.get(cd.callerInfoField) == null) 106 { 107 if (!cd.isTgtConAdvised) 110 { 111 String createName = getOptimizedConCalledByConInvocationClassName(cd.callingIndex, callingClass.getName(), cd.calledHash); 112 String invocationClassName = OptimizedCallerInvocations.createOptimizedConCalledByConInvocationClass(instrumentor, createName, callingClass, cd.calledConstructor, cd.callingIndex, cd.calledHash); 113 callerInfos.put(cd.callerInfoField, invocationClassName); 114 } 115 else 116 { 117 callerInfos.put(cd.callerInfoField, NonOptimizedCallerTransformer.PLACEHOLDER); 118 } 119 callerInfoAdder.addConByConInfoField(callingClass, cd.callerInfoField, cd.callingIndex, cd.classname, cd.calledHash); 120 } 121 } 122 123 protected void replaceMethodCallInCon(ConstructorDetail cd)throws CannotCompileException, NotFoundException 124 { 125 String invocationClassName = (String ) callerInfos.get(cd.callerInfoField); 126 String typedTargetObject = javassist.Modifier.isStatic(cd.calledMethod.getModifiers()) ? "" : "invocation.typedTargetObject=$0;"; 127 128 String replaced = 129 methodByConInfoFromWeakReference("info", cd.callerInfoField) + 130 "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " + 131 invocationClassName + " invocation = new " 132 + invocationClassName + " (info, $0, $args, info.getInterceptors());" + 133 typedTargetObject + 134 OptimizedBehaviourInvocations.setArguments(cd.calledMethod.getParameterTypes().length) + 135 "invocation.setTargetObject($0);" + 136 "$_ = ($r)invocation.invokeNext();" + 137 "} else { " + 138 "$_ = $proceed($$); " + 139 "}"; 140 141 cd.call.replace(replaced); 142 } 143 144 protected void replaceMethodCallInMethod(MethodDetail md)throws NotFoundException, CannotCompileException 145 { 146 String callingObject = ", null"; 147 if (!Modifier.isStatic(md.where.getModifiers())) 148 { 149 callingObject = ", this"; 150 } 151 152 String invocationClassName = (String ) callerInfos.get(md.callerInfoField); 153 String typedTargetObject = javassist.Modifier.isStatic(md.calledMethod.getModifiers()) ? "" : "invocation.typedTargetObject=$0;"; 154 155 String replaced = 156 methodByMethodInfoFromWeakReference("info", md.callerInfoField) + 157 "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " + 158 invocationClassName + " invocation = new " 159 + invocationClassName + " (info" + callingObject + ", $0, $args, info.getInterceptors());" + 160 OptimizedBehaviourInvocations.setArguments(md.calledMethod.getParameterTypes().length) + 161 typedTargetObject + 162 "invocation.setTargetObject($0);" + 163 "$_ = ($r)invocation.invokeNext();" + 164 "} else { " + 165 "$_ = $proceed($$); " + 166 "}"; 167 168 md.call.replace(replaced); 169 } 170 171 protected void replaceConCallInMethod(ConByMethodDetail cd) throws NotFoundException, CannotCompileException 172 { 173 if (cd.isTgtConAdvised) 176 { 177 super.replaceConCallInMethod(cd); 178 return; 179 } 180 String callingObject = "null"; 181 if (!Modifier.isStatic(cd.where.getModifiers())) 182 { 183 callingObject = "this"; 184 } 185 186 String invocationClassName = (String ) callerInfos.get(cd.callerInfoField); 187 String replaced = 188 conByMethodInfoFromWeakReference("info", cd.callerInfoField) + 189 "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " + 190 "java.lang.Object callingObject = " + callingObject + "; " + 191 invocationClassName + " invocation = new " 192 + invocationClassName + " (info," + callingObject + ", $args, info.getInterceptors());" + 193 OptimizedBehaviourInvocations.setArguments(cd.calledConstructor.getParameterTypes().length) + 194 "$_ = ($r)invocation.invokeNext();" + 195 "} else { " + 196 "$_ = $proceed($$); " + 197 "}"; 198 199 cd.call.replace(replaced); 200 } 201 202 protected void replaceConCallInCon(ConByConDetail cd)throws CannotCompileException, NotFoundException 203 { 204 if (cd.isTgtConAdvised) 207 { 208 super.replaceConCallInCon(cd); 209 return; 210 } 211 String invocationClassName = (String ) callerInfos.get(cd.callerInfoField); 212 213 String replaced = 214 conByConInfoFromWeakReference("info", cd.callerInfoField) + 215 "if (info.getInterceptors() != (org.jboss.aop.advice.Interceptor[])null) { " + 216 invocationClassName + " invocation = new " 217 + invocationClassName + "(info, $args, info.getInterceptors());" + 218 OptimizedBehaviourInvocations.setArguments(cd.calledConstructor.getParameterTypes().length) + 219 "$_ = ($r)invocation.invokeNext();" + 220 "} else { " + 221 "$_ = $proceed($$); " + 222 "}"; 223 224 cd.call.replace(replaced); 225 } 226 } 227 228 } 229 | Popular Tags |