1 22 package org.jboss.aop.instrument; 23 24 import javassist.CannotCompileException; 25 import javassist.CtClass; 26 import javassist.CtField; 27 import javassist.CtMethod; 28 import javassist.Modifier; 29 import javassist.NotFoundException; 30 import javassist.expr.FieldAccess; 31 32 38 public class OptimizedFieldAccessTransformer extends FieldAccessTransformer 39 { 40 41 public OptimizedFieldAccessTransformer(Instrumentor instrumentor) 42 { 43 super(instrumentor); 44 } 45 46 protected void doBuildFieldWrappers(CtClass clazz, CtField field, int fieldIndex, JoinpointClassification classificationGet, JoinpointClassification classificationSet) throws NotFoundException, CannotCompileException 47 { 48 instrumentor.setupBasics(clazz); 49 boolean wrappedGet = classificationGet.equals(JoinpointClassification.WRAPPED); 50 boolean wrappedSet = classificationSet.equals(JoinpointClassification.WRAPPED); 51 int mod = getStaticModifiers(field); 52 53 buildWrapperPlaceHolders(clazz, field, isPrepared(classificationGet), isPrepared(classificationSet), mod); 59 try { 60 if (isPrepared(classificationGet)) 61 { 62 addFieldReadInfoFieldWithAccessors(Modifier.PRIVATE | Modifier.STATIC, clazz, field); 63 OptimizedFieldInvocations.createOptimizedInvocationClass(instrumentor, clazz, field, true); 64 wrapper.prepareForWrapping(field, GET_INDEX); 66 } 67 68 if (isPrepared(classificationSet)) 69 { 70 addFieldWriteInfoField(Modifier.PRIVATE | Modifier.STATIC, clazz, field); 71 OptimizedFieldInvocations.createOptimizedInvocationClass(instrumentor, clazz, field, false); 72 wrapper.prepareForWrapping(field, SET_INDEX); 74 } 75 } catch (Exception e) 76 { 77 throw new CannotCompileException(e); 78 } 79 80 if (wrappedGet) 82 { 83 wrapper.wrap(field, GET_INDEX); 84 if (classificationGet.equals(JoinpointClassification.DYNAMICALY_WRAPPED)) 85 { 86 instrumentor.dynamicTransformationObserver.fieldReadDynamicalyWrapped(field); 87 } 88 } 89 if (wrappedSet) 90 { 91 wrapper.wrap(field, SET_INDEX); 92 if (classificationSet.equals(JoinpointClassification.DYNAMICALY_WRAPPED)) 93 { 94 instrumentor.dynamicTransformationObserver.fieldWriteDynamicalyWrapped(field); 95 } 96 } 97 98 replaceFieldAccessInternally(clazz, field, wrappedGet, wrappedSet, fieldIndex); 100 buildWrappers(clazz, field, wrappedGet, wrappedSet, fieldIndex); 101 } 102 103 112 protected String getWrapperBody(CtClass clazz, CtField field, boolean get, int index) 113 throws NotFoundException, CannotCompileException 114 { 115 if (get) 116 { 117 return getReadWrapperBody(clazz, field, index); 118 } 119 return getWriteWrapperBody(clazz, field, index); 120 } 121 122 129 private String getReadWrapperBody(CtClass clazz, CtField field, int index) 130 throws NotFoundException, CannotCompileException 131 { 132 String wrappedName = field.getName(); 133 134 String optimizedInvocation; 135 try 136 { 137 optimizedInvocation = OptimizedFieldInvocations.getOptimizedInvocationClassName(clazz, field, true); 138 optimizedInvocation = optimizedInvocation.substring(optimizedInvocation.lastIndexOf('.') + 1); 139 optimizedInvocation = clazz.getName() + "$" + optimizedInvocation; 140 } 141 catch (Exception e) 142 { 143 e.printStackTrace(); 144 throw new CannotCompileException(e); 145 } 146 147 String infoName = getFieldReadInfoFieldName(field.getName()); 148 boolean isStatic = javassist.Modifier.isStatic(field.getModifiers()); 149 150 String code; 151 152 if (!isStatic) 153 { 154 code = 155 "{ " 156 + " " + fieldInfoFromWeakReference("info", infoName) 157 + " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$1)._getInstanceAdvisor();" 158 + " org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors(); " 159 + " if (interceptors != (org.jboss.aop.advice.Interceptor[])null || (instAdv != null && instAdv.hasInstanceAspects))" 160 + " { " 161 + " if (instAdv != null) " 162 + " { " 163 + " interceptors = instAdv.getInterceptors(interceptors); " 164 + " } " 165 + " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(" + Instrumentor.HELPER_FIELD_NAME + ".getAdvisedFields()[" + index + "]," + index + ", interceptors); " 166 + " invocation.setTargetObject($1); " 167 + " invocation.typedTargetObject = (" + clazz.getName() + ")$1; " 168 + " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " 169 + " return ($r)invocation.invokeNext(); " 170 + " } " 171 + " else " 172 + " {" 173 + " return ((" + clazz.getName() + ")$1)." + wrappedName + ";" 174 + " }" 175 + "}"; 176 } 177 else 178 { 179 code = 180 "{ " 181 + " org.jboss.aop.advice.Interceptor[] interceptors = " + Instrumentor.HELPER_FIELD_NAME + ".getFieldReadInfos()[" + index + "].getInterceptors(); " 182 + " if (interceptors != (org.jboss.aop.advice.Interceptor[])null) " 183 + " { " 184 + " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(" + Instrumentor.HELPER_FIELD_NAME + ".getAdvisedFields()[" + index + "]," + index + ", interceptors); " 185 + " invocation.setTargetObject($1); " 186 + " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " 187 + " return ($r)invocation.invokeNext(); " 188 + " } " 189 + " else " 190 + " {" 191 + " return " + clazz.getName() + "." + wrappedName + ";" 192 + " }" 193 + "}"; 194 } 195 return code; 196 } 197 198 205 private String getWriteWrapperBody(CtClass clazz, CtField field, int index) 206 throws NotFoundException, CannotCompileException 207 { 208 String wrappedName = field.getName(); 209 210 String optimizedInvocation; 211 try 212 { 213 optimizedInvocation = OptimizedFieldInvocations.getOptimizedInvocationClassName(clazz, field, false); 214 optimizedInvocation = optimizedInvocation.substring(optimizedInvocation.lastIndexOf('.') + 1); 215 optimizedInvocation = clazz.getName() + "$" + optimizedInvocation; 216 } 217 catch (Exception e) 218 { 219 throw new CannotCompileException(e); 220 } 221 222 final String infoName = getFieldWriteInfoFieldName(field.getName()); 223 final boolean isStatic = javassist.Modifier.isStatic(field.getModifiers()); 224 String code; 225 if (!isStatic) 226 { 227 code = 228 "{ " 229 + " " + fieldInfoFromWeakReference("info", infoName) 230 + " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$1)._getInstanceAdvisor();" 231 + " org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors();" 232 + " if (interceptors != (org.jboss.aop.advice.Interceptor[])null || (instAdv != null && instAdv.hasInstanceAspects)) " 233 + " { " 234 + " if (instAdv != null) " 235 + " { " 236 + " interceptors = instAdv.getInterceptors(interceptors); " 237 + " } " 238 + " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(" + Instrumentor.HELPER_FIELD_NAME + ".getAdvisedFields()[" + index + "]," + index + ", ($w)$2" + ", interceptors); " 239 + " invocation.setTargetObject($1); " 240 + " invocation.typedTargetObject = (" + clazz.getName() + ")$1; " 241 + " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " 242 + " invocation.invokeNext(); " 243 + " } " 244 + " else " 245 + " {" 246 + " ((" + clazz.getName() + ")$1)." + wrappedName + "=$2" + ";" 247 + " }" 248 + "}"; 249 } 250 else 251 { 252 code = 253 "{ " 254 + " org.jboss.aop.advice.Interceptor[] interceptors = " + Instrumentor.HELPER_FIELD_NAME + ".getFieldWriteInfos()[" + index + "].getInterceptors(); " 255 + " if (interceptors != (org.jboss.aop.advice.Interceptor[])null) " 256 + " { " 257 + " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(" + Instrumentor.HELPER_FIELD_NAME + ".getAdvisedFields()[" + index + "]," + index + ", ($w)$2" + ", interceptors); " 258 + " invocation.setTargetObject($1); " 259 + " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " 260 + " invocation.invokeNext(); " 261 + " } " 262 + " else " 263 + " {" 264 + " " + clazz.getName() + "." + wrappedName + "=$2" + ";" 265 + " }" 266 + "}"; 267 } 268 return code; 269 } 270 271 272 private void buildWrappers(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index) throws NotFoundException, CannotCompileException 273 { 274 if (doGet) 275 { 276 String code = getReadWrapperBody(clazz, field, index); 277 CtMethod method = clazz.getDeclaredMethod(fieldRead(field.getName())); 278 method.setBody(code); 279 } 280 if (doSet) 281 { 282 String code = getWriteWrapperBody(clazz, field, index); 283 CtMethod method = clazz.getDeclaredMethod(fieldWrite(field.getName())); 284 method.setBody(code); 285 } 286 287 288 } 289 290 protected void replaceFieldAccessInternally(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index) throws CannotCompileException 291 { 292 OptimizedFieldAccessExprEditor expr = new OptimizedFieldAccessExprEditor(clazz, field, doGet, doSet, index); 293 clazz.instrument(expr); 294 } 295 296 protected class OptimizedFieldAccessExprEditor extends FieldAccessExprEditor 297 { 298 public OptimizedFieldAccessExprEditor(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index) 299 { 300 super(clazz, field, doGet, doSet, index); 301 } 302 303 protected void replaceRead(FieldAccess fieldAccess) throws CannotCompileException 304 { 305 if (fieldAccess.isStatic()) 306 { 307 String code = 308 " { " + 309 " $_ = ($r)" + fieldRead(field.getName()) + "(null);" + 310 " } " + 311 ""; 312 fieldAccess.replace(code); 313 } 314 else 315 { 316 String code = 317 " { " + 318 " $_ = ($r)" + fieldRead(field.getName()) + "($0);" + 319 " } " + 320 ""; 321 fieldAccess.replace(code); 322 } 323 } 324 325 protected void replaceWrite(FieldAccess fieldAccess) throws CannotCompileException 326 { 327 String fieldWrite = fieldWrite(field.getName()); 328 if (fieldAccess.isStatic()) 329 { 330 String code = 331 " { " + 332 " " + fieldWrite + "(null, $1);" + 333 " } " + 334 ""; 335 fieldAccess.replace(code); 336 337 } 338 else 339 { 340 String code = 341 " { " + 342 " " + fieldWrite + "($0, $1);" + 343 " } " + 344 ""; 345 fieldAccess.replace(code); 346 } 347 } 348 } 349 350 351 } 352 | Popular Tags |