1 22 package org.jboss.aop.instrument; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javassist.CannotCompileException; 28 import javassist.ClassPool; 29 import javassist.CtClass; 30 import javassist.CtField; 31 import javassist.CtMethod; 32 import javassist.NotFoundException; 33 import javassist.expr.ExprEditor; 34 import javassist.expr.FieldAccess; 35 36 import org.jboss.aop.ClassAdvisor; 37 import org.jboss.aop.instrument.OptimizedFieldAccessTransformer.OptimizedFieldAccessExprEditor; 38 39 45 public class NonOptimizedFieldAccessTransformer extends FieldAccessTransformer 46 { 47 48 public NonOptimizedFieldAccessTransformer(Instrumentor instrumentor) 49 { 50 super(instrumentor); 51 } 52 53 protected void doBuildFieldWrappers(CtClass clazz, CtField field, int fieldIndex, JoinpointClassification classificationGet, JoinpointClassification classificationSet) 54 throws NotFoundException, CannotCompileException 55 { 56 instrumentor.setupBasics(clazz); 57 58 boolean wrappedGet = classificationGet.equals(JoinpointClassification.WRAPPED); 59 boolean wrappedSet = classificationSet.equals(JoinpointClassification.WRAPPED); 60 int mod = getStaticModifiers(field); 61 62 replaceFieldAccessInternally(clazz, field, wrappedGet, wrappedSet, fieldIndex); 64 65 if (!javassist.Modifier.isPrivate(field.getModifiers())) 68 { 69 if (isPrepared(classificationGet)) 71 { 72 wrapper.prepareForWrapping(field, GET_INDEX); 73 } 74 if (isPrepared(classificationSet)) 75 { 76 wrapper.prepareForWrapping(field, SET_INDEX); 77 } 78 79 if (wrappedGet) 81 { 82 wrapper.wrap(field, GET_INDEX); 83 if (classificationGet.equals(JoinpointClassification.DYNAMICALY_WRAPPED)) 84 { 85 instrumentor.dynamicTransformationObserver.fieldReadDynamicalyWrapped(field); 86 } 87 } 88 if (wrappedSet) 89 { 90 wrapper.wrap(field, SET_INDEX); 91 if (classificationSet.equals(JoinpointClassification.DYNAMICALY_WRAPPED)) 92 { 93 instrumentor.dynamicTransformationObserver.fieldWriteDynamicalyWrapped(field); 94 } 95 } 96 buildWrapperPlaceHolders(clazz, field, isPrepared(classificationGet), isPrepared(classificationSet), mod); 98 buildWrappers(clazz, field, isPrepared(classificationGet), isPrepared(classificationSet), fieldIndex); } 100 } 101 102 private void buildWrappers(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index) 103 throws NotFoundException, CannotCompileException 104 { 105 if (doGet) 106 { 107 String code = getWrapperBody(clazz, field, true, index); 108 CtMethod method = clazz.getDeclaredMethod(fieldRead(field.getName())); 109 method.setBody(code); 110 } 111 if (doSet) 112 { 113 String code = getWrapperBody(clazz, field, false, index); 114 CtMethod method = clazz.getDeclaredMethod(fieldWrite(field.getName())); 115 method.setBody(code); 116 } 117 } 118 119 protected String getWrapperBody(CtClass clazz, CtField field, boolean get, int fieldIndex) throws NotFoundException, CannotCompileException 120 { 121 String name = field.getName(); 122 boolean isStatic = (javassist.Modifier.isStatic(field.getModifiers())); 123 String access = ""; 124 String instanceCheck = ""; 125 if (!isStatic) 126 { 127 access = "((" + clazz.getName() + ")$1)."; 128 instanceCheck = " || ((org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$1)._getInstanceAdvisor()).hasInstanceAspects"; 129 } 130 131 if (get) 133 { 134 return 135 "{ " + 136 " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() " + instanceCheck + " ) " + 137 " { " + 138 " return ($r)" + Instrumentor.HELPER_FIELD_NAME + ".invokeRead($1, (int)" + (fieldIndex) + "); " + 139 " } " + 140 " return " + access + name + "; " + 141 "}"; 142 } 143 return 145 "{ " + 146 " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() " + instanceCheck + " ) " + 147 " { " + 148 " " + Instrumentor.HELPER_FIELD_NAME + ".invokeWrite($1, (int)" + (fieldIndex) + ", ($w)$2); " + 149 " } " + 150 " else " + 151 " { " + 152 " " + access + name + " = $2; " + 153 " } " + 154 "}"; 155 } 156 157 protected void replaceFieldAccessInternally(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index) throws CannotCompileException 158 { 159 NonOptimizedFieldAccessExprEditor expr = new NonOptimizedFieldAccessExprEditor(clazz, field, doGet, doSet, index); 160 clazz.instrument(expr); 161 } 162 163 164 protected class NonOptimizedFieldAccessExprEditor extends FieldAccessExprEditor 165 { 166 public NonOptimizedFieldAccessExprEditor(CtClass clazz, CtField field, boolean doGet, boolean doSet, int index) 167 { 168 super(clazz, field, doGet, doSet, index); 169 } 170 171 protected void replaceRead(FieldAccess fieldAccess) throws CannotCompileException 172 { 173 String code = null; 174 try 175 { 176 if (fieldAccess.isStatic()) 177 { 178 code = 179 " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects()) " + 180 " { " + 181 " Object obj = null;" + 182 " $_ = ($r)" + Instrumentor.HELPER_FIELD_NAME + ".invokeRead(obj, (int)" + (fieldIndex) + "); " + 183 " } " + 184 " else " + 185 " { " + 186 " $_ = " + clazz.getName() + "." + field.getName() + "; " + 187 " } " + 188 ""; 189 fieldAccess.replace(code); 190 } 191 else 192 { 193 code = 194 " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$0)._getInstanceAdvisor();" + 195 " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() || (instAdv != null && instAdv.hasInstanceAspects)) " + 196 " { " + 197 " $_ = ($r)" + Instrumentor.HELPER_FIELD_NAME + ".invokeRead($0, (int)" + (fieldIndex) + "); " + 198 " } " + 199 " else " + 200 " { " + 201 " $_ = $0." + fieldAccess.getFieldName() + "; " + 202 " } "; 203 fieldAccess.replace(code); 204 } 205 } 206 catch (CannotCompileException e) 207 { 208 throw new RuntimeException ("failed with: " + code, e); 209 } 210 } 211 212 protected void replaceWrite(FieldAccess fieldAccess) throws CannotCompileException 213 { 214 if (fieldAccess.isStatic()) 215 { 216 String code = 217 " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects()) " + 218 " { " + 219 " Object obj = null;" + 220 " " + Instrumentor.HELPER_FIELD_NAME + ".invokeWrite(obj, (int)" + (fieldIndex) + ", ($w)$1); " + 221 " } " + 222 " else " + 223 " { " + 224 " " + clazz.getName() + "." + fieldAccess.getFieldName() + " = $1; " + 225 " } "; 226 fieldAccess.replace(code); 227 } 228 else 229 { 230 String code = 231 " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)((org.jboss.aop.InstanceAdvised)$0)._getInstanceAdvisor();" + 232 " if (" + Instrumentor.HELPER_FIELD_NAME + ".hasAspects() || (instAdv != null && instAdv.hasInstanceAspects)) " + 233 " { " + 234 " " + Instrumentor.HELPER_FIELD_NAME + ".invokeWrite($0, (int)" + (fieldIndex) + ", ($w)$1); " + 235 " } " + 236 " else " + 237 " { " + 238 " $0." + fieldAccess.getFieldName() + " = $1; " + 239 " } "; 240 fieldAccess.replace(code); 241 } 242 } 243 244 245 } 247 } 248 | Popular Tags |