KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
34  * @grammar per[this|target](pointcut)
35  * @child Pcd pcd
36  * @property boolean onThis
37  */

38
39 public class PerObject extends PerClause {
40     public String JavaDoc 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                 //??? might want to include a better filter test here as an optimization
49
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 JavaDoc 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         //stack.setSynthetic();
91
this.getAspectDec().addToBody(map);
92
93         return map;
94     }
95     
96     private MethodDec bindMethod;
97     MethodDec getBindMethod() {
98         if (bindMethod != null) return bindMethod;
99         /* synchronized public static void bindToAspect(Object toObject) {
100                if (!permap$ajc.hasAspect(toObject)) {
101                    permap$ajc.bind(new A(), toObject);
102                }
103            }*/

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         /* public static boolean hasAspect(Object onObject) {
147                return map.hasAspect(onObject);
148            }
149         */

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         /* public static A aspectOf(Object onObject) {
161                Object asp = map.aspectOf(onObject);
162                if (asp != null) return asp;
163                throw new org.aspectj.runtime.NoAspectException();
164            }
165         */

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     //public Expr makeAspectOfExpr(Expr fromObject) { return makeAspectInstance(fromObject); }
182

183
184     /***********************************************************************************/
185
186     // this is called by the EachObjectPlan for the "entry points"
187
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     //BEGIN: Generated from @child and @property
200
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 JavaDoc 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 JavaDoc getDefaultDisplayName() {
251         return "PerObject(onThis: "+onThis+")";
252     }
253     
254     //END: Generated from @child and @property
255
}
256
257
Popular Tags