1 24 25 package org.aspectj.compiler.crosscuts.ast; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.base.*; 29 import org.aspectj.compiler.crosscuts.joinpoints.*; 30 31 32 import java.util.*; 33 34 import org.aspectj.util.*; 35 36 41 42 public class PerCFlow extends PerClause implements CFlowPlanner { 43 public String toShortString() { 44 return "percflow" + (includesRoot ? NO_ROOT_NAME : "") 45 + "(" + getPcd().toShortString() + ")"; 46 } 47 48 public JpPlanner makeInnerPlanner(PlanData planData) { 49 return new JpPlanner() { 50 public FuzzyBoolean fastMatch(JoinPoint jp) { 51 return FuzzyBoolean.MAYBE; 53 } 54 public JpPlan makePlan(JoinPoint jp) { 55 JpPlan plan = new JpPlan(jp); 56 plan.test = testStackExpr(); 57 plan.setInstanceExpr(makeAspectInstance()); 58 plan.addDependency(this); 59 return plan; 60 } 61 }; 62 } 63 64 public JpPlanner makeInitializerPlanner(PlanData planData) { 65 return new WrappedJpPlanner(getPcd().makePlanner(planData)) { 66 public JpPlan makePlan(JoinPoint jp) { 67 JpPlan plan = super.makePlan(jp); 68 return new CFlowEntryPlan(plan, PerCFlow.this); 69 } 70 }; 71 } 72 73 private FieldDec stack; 74 FieldDec getStackField() { 75 if (stack != null) return stack; 76 77 final AST ast = getAST(); 78 String name = "eachcflow$ajc"; 79 Type type = getTypeManager().getType("org.aspectj.runtime.internal", "CFlowStack"); 80 81 Modifiers modifiers = 82 ast.makeModifiers(Modifiers.PUBLIC | Modifiers.STATIC | Modifiers.FINAL); 83 stack = ast.makeField(modifiers, type, name, ast.makeNew(type)); 84 this.getAspectDec().getBody().add(0, stack); 86 87 return stack; 88 } 89 90 Expr getStackVar() { 91 return getAST().makeGet(getStackField()); 92 } 93 94 Expr testStackExpr() { 95 return getAST().makeCall(getStackVar(), "isValid"); 96 } 97 98 Expr pushStackExpr() { 99 return pushStackExpr(getAST().makeNew(getTypeManager().getObjectType())); 100 } 101 Expr pushStackExpr(Expr expr) { 102 return getAST().makeCall(getStackVar(), "push", expr); 103 } 104 105 Expr popStackExpr() { 106 return getAST().makeCall(getStackVar(), "pop"); 107 } 108 109 Expr peekStackExpr() { 110 return getAST().makeCall(getStackVar(), "peek"); 111 } 113 114 private static int ecounter = 0; 116 public Stmts wrapPushPop(JpPlan plan, Stmts body) { 117 final AST ast = getAST(); 118 119 Expr test = plan.getDynamicTest(); 120 if (test == null) { 121 return ast.makeStmts( 122 ast.makeStmt(pushStackExpr(makeNewAspectExpr())), 123 ast.makeTryFinally(ast.makeBlock(body), 124 ast.makeBlock(popStackExpr()))); 125 } else { 126 VarDec tmp = ast.makeFinalVar(getTypeManager().booleanType, 127 "ajc_enter_ecflow_" + ecounter++, test); 128 return ast.makeStmts( 129 tmp, 130 ast.makeIf(ast.makeVar(tmp), 131 ast.makeStmt(pushStackExpr(makeNewAspectExpr()))), 132 ast.makeTryFinally(ast.makeBlock(body), 133 ast.makeBlock(ast.makeIf(ast.makeVar(tmp), 134 ast.makeStmt(popStackExpr()))))); 135 } 136 } 137 138 139 141 Expr makeAspectInstance() { 142 return getAST().makeCast(getAspectType(), peekStackExpr()); 143 } 144 145 public Expr makeAspectOfExpr(Expr fromObject) { return makeAspectInstance(); } 146 147 148 149 Expr makeNewAspectExpr() { 150 return getAST().makeNew(getAspectType()); 151 } 152 153 protected MethodDec makeHasAspectMethod() { 154 158 final AST ast = getAST(); 159 BlockStmt body = ast.makeBlock(ast.makeReturn(testStackExpr())); 160 return makeHasAspectMethod(ast.makeFormals(), body); 161 } 162 163 protected MethodDec makeAspectOfMethod() { 164 172 final AST ast = getAST(); 173 174 VarDec aspectDec = ast.makeVarDec(getAspectType(), "asp", makeAspectInstance()); 175 176 BlockStmt body = ast.makeBlock( 177 aspectDec, 178 ast.makeIf(ast.makeNonNullTest(ast.makeVar(aspectDec)), 179 ast.makeReturn(ast.makeVar(aspectDec))), 180 ast.makeThrow(getTypeManager().getType("org.aspectj.lang", "NoAspectBoundException"))); 181 182 return makeAspectOfMethod(ast.makeFormals(), body); 183 } 184 185 protected Pcd pcd; 187 public Pcd getPcd() { return pcd; } 188 public void setPcd(Pcd _pcd) { 189 if (_pcd != null) _pcd.setParent(this); 190 pcd = _pcd; 191 } 192 193 protected boolean includesRoot; 194 public boolean getIncludesRoot() { return includesRoot; } 195 public void setIncludesRoot(boolean _includesRoot) { includesRoot = _includesRoot; } 196 197 public PerCFlow(SourceLocation location, Pcd _pcd, boolean _includesRoot) { 198 super(location); 199 setPcd(_pcd); 200 setIncludesRoot(_includesRoot); 201 } 202 protected PerCFlow(SourceLocation source) { 203 super(source); 204 } 205 206 public ASTObject copyWalk(CopyWalker walker) { 207 PerCFlow ret = new PerCFlow(getSourceLocation()); 208 ret.preCopy(walker, this); 209 if (pcd != null) ret.setPcd( (Pcd)walker.process(pcd) ); 210 ret.includesRoot = includesRoot; 211 return ret; 212 } 213 214 public ASTObject getChildAt(int childIndex) { 215 switch(childIndex) { 216 case 0: return pcd; 217 default: return super.getChildAt(childIndex); 218 } 219 } 220 public String getChildNameAt(int childIndex) { 221 switch(childIndex) { 222 case 0: return "pcd"; 223 default: return super.getChildNameAt(childIndex); 224 } 225 } 226 public void setChildAt(int childIndex, ASTObject child) { 227 switch(childIndex) { 228 case 0: setPcd((Pcd)child); return; 229 default: super.setChildAt(childIndex, child); return; 230 } 231 } 232 public int getChildCount() { 233 return 1; 234 } 235 236 public String getDefaultDisplayName() { 237 return "PerCFlow(includesRoot: "+includesRoot+")"; 238 } 239 240 } 242 243 | Popular Tags |