1 24 package org.aspectj.compiler.crosscuts.ast; 25 26 import org.aspectj.compiler.base.ast.*; 27 import org.aspectj.compiler.base.*; 28 import org.aspectj.compiler.crosscuts.joinpoints.*; 29 30 import java.util.*; 31 import org.aspectj.util.*; 32 33 38 39 public class PerObject extends PerClause { 40 public String toShortString() { 41 return "per" + (onThis ? "this" : "target") + 42 "(" + getPcd().toShortString() + ")"; 43 } 44 45 public JpPlanner makeInnerPlanner(PlanData planData) { 46 return new JpPlanner() { 47 public FuzzyBoolean fastMatch(JoinPoint jp) { 48 return FuzzyBoolean.MAYBE; 50 } 51 public JpPlan makePlan(JoinPoint jp) { 52 Expr onExpr = makeOnExpr(jp); 53 if (onExpr == null) return JpPlan.NO_PLAN; 54 55 JpPlan plan = new JpPlan(jp); 56 plan.test = testMapExpr(makeOnExpr(jp)); 57 plan.setInstanceExpr(getAspectOfExpr(makeOnExpr(jp))); 58 return plan; 59 } 60 }; 61 } 62 63 public JpPlanner makeInitializerPlanner(PlanData planData) { 64 return new WrappedJpPlanner(getPcd().makePlanner(planData)) { 65 public JpPlan makePlan(JoinPoint jp) { 66 if (makeOnExpr(jp) == null) return JpPlan.NO_PLAN; 67 JpPlan plan = super.makePlan(jp); 68 return new EachObjectPlan(plan, PerObject.this); 69 } 70 }; 71 } 72 73 public Expr makeOnExpr(JoinPoint point) { 74 if (onThis) return point.makeThisExpr(); 75 else return point.makeTargetExpr(); 76 } 77 78 79 private FieldDec map; 80 FieldDec getMapField() { 81 if (map != null) return map; 82 83 final AST ast = getAST(); 84 String name = "permap$ajc"; 85 Type type = getTypeManager().getType("org.aspectj.runtime.internal", "PerObjectMap"); 86 87 Modifiers modifiers = 88 ast.makeModifiers(Modifiers.PUBLIC | Modifiers.STATIC | Modifiers.FINAL); 89 map = ast.makeField(modifiers, type, name, ast.makeNew(type)); 90 this.getAspectDec().addToBody(map); 92 93 return map; 94 } 95 96 private MethodDec bindMethod; 97 MethodDec getBindMethod() { 98 if (bindMethod != null) return bindMethod; 99 104 final AST ast = getAST(); 105 106 FormalDec toObjectFormal = ast.makeFormal(getTypeManager().getObjectType(), 107 "toObject"); 108 109 BlockStmt body = ast.makeBlock( 110 ast.makeIf( 111 ast.makeUnop("!", testMapExpr(ast.makeVar(toObjectFormal))), 112 ast.makeBlock( 113 ast.makeCall(getMapVar(), "bind", 114 ast.makeVar(toObjectFormal), 115 ast.makeNew(getAspectType()))) 116 )); 117 118 Modifiers modifiers = 119 ast.makeModifiers(Modifiers.STATIC | Modifiers.PUBLIC | Modifiers.SYNCHRONIZED); 120 121 bindMethod = ast.makeMethod(modifiers, getTypeManager().voidType, 122 "bind$ajc", ast.makeFormals(toObjectFormal), body); 123 this.getAspectDec().addToBody(bindMethod); 124 return bindMethod; 125 } 126 127 Expr getMapVar() { 128 return getAST().makeGet(getMapField()); 129 } 130 131 Expr testMapExpr(Expr onExpr) { 132 return getAST().makeCall(getMapVar(), "hasAspect", onExpr); 133 } 134 135 Expr bindExpr(Expr onExpr) { 136 return getAST().makeStaticCall(getBindMethod(), onExpr); 137 } 138 139 Expr getAspectOfExpr(Expr onExpr) { 140 final AST ast = getAST(); 141 return ast.makeParen(ast.forceCast( 142 getAspectType(), ast.makeCall(getMapVar(), "aspectOf", onExpr))); 143 } 144 145 protected MethodDec makeHasAspectMethod() { 146 150 final AST ast = getAST(); 151 FormalDec onObjectFormal = ast.makeFormal(getTypeManager().getObjectType(), 152 "onObject"); 153 154 BlockStmt body = 155 ast.makeBlock(ast.makeReturn(testMapExpr(ast.makeVar(onObjectFormal)))); 156 return makeHasAspectMethod(ast.makeFormals(onObjectFormal), body); 157 } 158 159 protected MethodDec makeAspectOfMethod() { 160 166 final AST ast = getAST(); 167 FormalDec onObjectFormal = ast.makeFormal(getTypeManager().getObjectType(), 168 "onObject"); 169 170 VarDec aspectDec = ast.makeVarDec(getAspectType(), "asp", 171 getAspectOfExpr(ast.makeVar(onObjectFormal))); 172 173 BlockStmt body = ast.makeBlock( 174 aspectDec, 175 ast.makeIf(ast.makeNonNullTest(ast.makeVar(aspectDec)), 176 ast.makeReturn(ast.makeVar(aspectDec))), 177 ast.makeThrow(getTypeManager().getType("org.aspectj.lang", "NoAspectBoundException"))); 178 179 return makeAspectOfMethod(ast.makeFormals(onObjectFormal), body); 180 } 181 183 184 185 186 public Stmts wrapCheckAndSet(JpPlan plan, JoinPoint jp, Stmts body) { 188 final AST ast = getAST(); 189 Expr onExpr = makeOnExpr(jp); 190 if (onExpr == null) return body; 191 192 Stmt stmt = ast.makeStmt(bindExpr(onExpr)); 193 stmt = plan.wrapDynamicTest(ast.makeBlock(stmt)); 194 195 body.add(0, stmt); 196 return body; 197 } 198 199 protected Pcd pcd; 201 public Pcd getPcd() { return pcd; } 202 public void setPcd(Pcd _pcd) { 203 if (_pcd != null) _pcd.setParent(this); 204 pcd = _pcd; 205 } 206 207 protected boolean onThis; 208 public boolean getOnThis() { return onThis; } 209 public void setOnThis(boolean _onThis) { onThis = _onThis; } 210 211 public PerObject(SourceLocation location, Pcd _pcd, boolean _onThis) { 212 super(location); 213 setPcd(_pcd); 214 setOnThis(_onThis); 215 } 216 protected PerObject(SourceLocation source) { 217 super(source); 218 } 219 220 public ASTObject copyWalk(CopyWalker walker) { 221 PerObject ret = new PerObject(getSourceLocation()); 222 ret.preCopy(walker, this); 223 if (pcd != null) ret.setPcd( (Pcd)walker.process(pcd) ); 224 ret.onThis = onThis; 225 return ret; 226 } 227 228 public ASTObject getChildAt(int childIndex) { 229 switch(childIndex) { 230 case 0: return pcd; 231 default: return super.getChildAt(childIndex); 232 } 233 } 234 public String getChildNameAt(int childIndex) { 235 switch(childIndex) { 236 case 0: return "pcd"; 237 default: return super.getChildNameAt(childIndex); 238 } 239 } 240 public void setChildAt(int childIndex, ASTObject child) { 241 switch(childIndex) { 242 case 0: setPcd((Pcd)child); return; 243 default: super.setChildAt(childIndex, child); return; 244 } 245 } 246 public int getChildCount() { 247 return 1; 248 } 249 250 public String getDefaultDisplayName() { 251 return "PerObject(onThis: "+onThis+")"; 252 } 253 254 } 256 257 | Popular Tags |