KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > ast > PerCFlow


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

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 /**
37  * @grammar percflow(pcd)
38  * @child Pcd pcd
39  * @property boolean includesRoot
40  */

41
42 public class PerCFlow extends PerClause implements CFlowPlanner {
43     public String JavaDoc 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                 //??? might want to include a better filter test here as an optimization
52
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 JavaDoc 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         //stack.setSynthetic();
85
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         //return new CallExpr(getCompiler(),getStackVar(), "peek");
112
}
113
114     // this is called by the CFlowAdviceDec for the "entry points"
115
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     //-----------------------------------------------------------------------------------
140

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         /* public static boolean hasAspect() {
155                return stackVar.isValid();
156            }
157         */

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         /* public static A aspectOf() {
165                //if (stackVar.isValid()) {
166                    Object o = stackVar.getAspect();
167                    if (o != null) return o;
168                //}
169                throw new org.aspectj.runtime.NoAspectException();
170            }
171         */

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     //BEGIN: Generated from @child and @property
186
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 JavaDoc 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 JavaDoc getDefaultDisplayName() {
237         return "PerCFlow(includesRoot: "+includesRoot+")";
238     }
239     
240     //END: Generated from @child and @property
241
}
242
243
Popular Tags