1 22 package org.jboss.test.aop.jdk15.dynamic.common; 23 24 import javassist.CannotCompileException; 25 import javassist.CtClass; 26 import javassist.CtField; 27 import javassist.CtMethod; 28 import javassist.NotFoundException; 29 import javassist.expr.ExprEditor; 30 import javassist.expr.FieldAccess; 31 import javassist.expr.MethodCall; 32 import javassist.expr.NewExpr; 33 34 import org.jboss.aop.ClassAdvisor; 35 import org.jboss.aop.instrument.ConstructorExecutionTransformer; 36 import org.jboss.aop.joinpoint.InvocationBase; 37 43 public class POJOWrappingInfo 44 { 45 48 private class POJOMethodIntrospector extends ExprEditor 49 { 50 private String notAdvisedMethod; 51 56 public POJOMethodIntrospector(String notAdvisedMethod) 57 { 58 this.notAdvisedMethod = notAdvisedMethod; 59 } 60 public void edit(MethodCall methodCall) throws CannotCompileException 61 { 62 if (methodCall.where().getName().endsWith("$aop")) 63 return; 64 if (methodCall.getClassName().equals(getClass().getPackage().getName() + ".scenario.POJO") && 65 methodCall.getMethodName().equals(notAdvisedMethod)) 66 { 67 methodWrapped = true; 68 } 69 } 70 } 71 72 76 private class POJOClientIntrospector extends ExprEditor 77 { 78 public void edit(NewExpr newExpr) 79 { 80 if (newExpr.getClassName().equals(getClass().getPackage().getName() + "scenario.POJO")) 82 { 83 constructorWrapped = false; 84 } 85 } 86 87 public void edit(FieldAccess fieldAccess) 88 { 89 if (fieldAccess.getFieldName().equals("counter") && 90 fieldAccess.getClassName().equals(getClass().getPackage().getName() + "scenario.POJO")) 91 { 92 if (fieldAccess.isReader()) 94 { 95 fieldReadWrapped = false; 96 } 97 else 99 { 100 fieldWriteWrapped = false; 101 } 102 } 103 } 104 } 105 106 111 private class WrapperIntrospector extends ExprEditor 112 { 113 boolean wrapperEmpty = true; 115 public void edit(MethodCall call) 116 { 117 if (call.getClassName().equals(ClassAdvisor.class.getName()) 118 || call.getClassName().equals(InvocationBase.class.getName())) 119 { 120 wrapperEmpty = false; 121 } 122 } 123 } 124 125 private boolean constructorWrapped; 126 private boolean fieldReadWrapped; 127 private boolean fieldWriteWrapped; 128 private boolean methodWrapped; 129 public POJOWrappingInfo(){} 130 131 137 public POJOWrappingInfo(CtClass pojoClass, CtField field, CtMethod method, 138 String notAdvisedMethod, CtClass pojoClient) throws CannotCompileException, NotFoundException 139 { 140 this.constructorWrapped = true; 141 this.fieldReadWrapped = true; 142 this.fieldWriteWrapped = true; 143 this.methodWrapped = false; 144 if (pojoClass.isFrozen()) 145 { 146 pojoClass.defrost(); 147 } 148 method.instrument(new POJOMethodIntrospector(notAdvisedMethod)); 149 if (pojoClient.isFrozen()) 150 { 151 pojoClient.defrost(); 152 } 153 pojoClient.instrument(new POJOClientIntrospector()); 154 if (this.constructorWrapped) 155 { 156 String constructorWrapper = ConstructorExecutionTransformer.constructorFactory(pojoClass.getSimpleName()); 159 constructorWrapped = !isWrapperEmpty(pojoClass, constructorWrapper); 160 } 161 if (this.fieldReadWrapped) 162 { 163 String fieldWrapper = field.getName() + "_r_" + ClassAdvisor.NOT_TRANSFORMABLE_SUFFIX; 166 fieldReadWrapped = !isWrapperEmpty(pojoClass, fieldWrapper); 167 } 168 if (this.fieldWriteWrapped) 169 { 170 String fieldWrapper = field.getName() + "_w_" + ClassAdvisor.NOT_TRANSFORMABLE_SUFFIX; 173 fieldWriteWrapped = !isWrapperEmpty(pojoClass,fieldWrapper); 174 } 175 } 176 177 183 private boolean isWrapperEmpty(CtClass pojoClass, String wrapperName) throws CannotCompileException 184 { 185 CtMethod wrapper; 186 try { 187 wrapper = pojoClass.getDeclaredMethod(wrapperName); 188 } catch (NotFoundException e) 189 { 190 return true; 191 } 192 WrapperIntrospector introspector = new WrapperIntrospector(); 193 wrapper.instrument(introspector); 194 return introspector.wrapperEmpty; 195 } 196 197 200 public boolean isConstructorWrapped() 201 { 202 return this.constructorWrapped; 203 } 204 205 208 public boolean isFieldReadWrapped() 209 { 210 return this.fieldReadWrapped; 211 } 212 213 216 public boolean isFieldWriteWrapped() 217 { 218 return this.fieldWriteWrapped; 219 } 220 221 224 public boolean isMethodWrapped() 225 { 226 return this.methodWrapped; 227 } 228 } | Popular Tags |