KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > joinpoints > AdvicePlan


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.joinpoints;
26
27 import org.aspectj.compiler.crosscuts.ast.*;
28 import org.aspectj.compiler.base.ast.*;
29 import org.aspectj.compiler.base.*;
30
31 import org.aspectj.compiler.crosscuts.AspectJCompiler;
32
33 import org.aspectj.util.PartialOrder;
34
35 import java.util.*;
36
37 /**
38  */

39
40 public class AdvicePlan extends JpPlan {
41     public AspectDec aspectDec;
42     public AdviceDec adviceDec;
43     public JpPlan innerPlan;
44
45     public AdviceDec getAdviceDec() { return adviceDec; }
46     public AspectDec getAspectDec() { return aspectDec; }
47
48     public AdvicePlan(JoinPoint jp, AspectDec aspectDec, AdviceDec adviceDec, JpPlan innerPlan) {
49         super(jp);
50         this.aspectDec = aspectDec;
51         this.adviceDec = adviceDec;
52         this.innerPlan = innerPlan;
53         
54         //???
55
JpPlan other = innerPlan;
56         test = simplify(other.test);
57         ifTest = simplify(other.ifTest);
58         instance = other.instance;
59         bindings = other.bindings;
60         dependencies = other.dependencies;
61     }
62
63     void checkExceptions(AdviceDec adviceDec, JoinPoint joinPoint) {
64         TypeDs _throws = adviceDec.getThrows();
65         if (_throws == null) return;
66
67         for (int i = 0; i < _throws.size(); i++) {
68             Type t = _throws.get(i).getType();
69             if (!joinPoint.canThrow(t)) {
70                 joinPoint.showError(adviceDec,
71                                     "can't throw " + t.getPrettyString());
72                 break;
73             }
74         }
75     }
76         
77     
78     public void wrapJoinPoint(JoinPoint joinPoint) {
79         checkExceptions(adviceDec, joinPoint);
80         adviceDec.wrapJoinPoint(joinPoint, this);
81     }
82
83
84     /**
85      * Sorting is based purely on the adviceDec. See AdviceDec.dominates for
86      * detailed rules.
87      */

88     public int compareTo(Object JavaDoc o) {
89         int ret = super.compareTo(o);
90         if (ret != 0) return ret;
91         
92         if (!(o instanceof AdvicePlan)) return 0;
93         AdvicePlan otherPlan = (AdvicePlan)o;
94
95         if (this.getAdviceDec().dominates(otherPlan.getAdviceDec())) {
96             return -1;
97         } else if (otherPlan.getAdviceDec().dominates(this.getAdviceDec())) {
98             return +1;
99         } else {
100             return 0;
101         }
102     }
103     
104     public boolean matches(JpPlan other) {
105         if (other instanceof AdvicePlan) {
106             return getAdviceDec() == ((AdvicePlan)other).getAdviceDec();
107         } else {
108             return false;
109         }
110     }
111     
112     public int getPreSortOrder() {
113         if (adviceDec instanceof AroundAdviceDec) return AROUND;
114         if (adviceDec instanceof BeforeAdviceDec) return BEFORE;
115         return AFTER;
116     }
117     
118     /**
119      * Uses alphabetical order of aspects as a falback comparision rule.
120      * This is a controversial choice.
121      */

122     public int fallbackCompareTo(Object JavaDoc o) {
123         if (!(o instanceof AdvicePlan)) return 0;
124         AdvicePlan otherPlan = (AdvicePlan)o;
125         
126         //XXX keep things deterministic, whether or not that's the right thing to
127
//XXX do is subject to debate -- the sort code works hard to make this possible
128
return getAspectDec().getId().compareTo(otherPlan.getAspectDec().getId());
129     }
130
131
132     public Exprs makeCallExprs(Expr extraExpr) {
133         Expr instance = getInstance();
134
135         Exprs exprs = getCallExprs(adviceDec.getFormals()); //.copy();
136
Type type = adviceDec.getExtraArgType();
137         if (type != null) {
138             //System.out.println("extra type: " + type + ", " + extraExpr.getType());
139
if (type.isEquivalent(getTypeManager().getObjectType())) {
140                 //System.out.println("old extra expr: " + extraExpr);
141
extraExpr = extraExpr.getType().makeObject(extraExpr);
142                 //System.out.println("new extra expr: " + extraExpr);
143
}
144
145             if (!extraExpr.getType().isCoercableTo(type)) {
146                 //System.out.println(extraExpr.getTypeD() + " not coercableTo " + typeD);
147
return null;
148             }
149             extraExpr = getAST().makeCast(type, extraExpr);
150             exprs.add(extraExpr);
151         }
152         if (adviceDec.needsStaticEnclosingJoinPointFormal()) {
153             exprs.add(joinPoint.makeStaticEnclosingJoinPointVarExpr());
154         }
155         if (adviceDec.needsStaticJoinPointFormal()) {
156             exprs.add(joinPoint.makeStaticJoinPointVarExpr());
157         }
158         if (adviceDec.needsDynamicJoinPointFormal()) {
159 // System.out.println(exprs);
160
// System.out.println(joinPoint);
161
exprs.add(joinPoint.makeDynamicJoinPointVarExpr());
162         }
163         return exprs;
164     }
165
166     public Stmt makeCall(Expr extraExpr) {
167         Exprs exprs = makeCallExprs(extraExpr);
168         if (exprs == null) return getAST().makeEmptyStmt();
169         Expr call = adviceDec.makeCall(instance, exprs);
170         call.setSourceLocation(joinPoint.getTargetNode().getSourceLocation());
171         Stmt callStmt = getAST().makeStmt(call);
172         //System.out.println("plan: " + this + " test: " + hasDynamicTest());
173
return wrapDynamicTest(callStmt);
174     }
175
176
177     public Expr getInstance() {
178         if (instance == null) return null;
179
180         return getAST().makeCast(getAspectDec().getType(), instance);
181     }
182
183     public String JavaDoc toString() {
184         if (adviceDec == null || joinPoint == null) {
185             return super.toString();
186         } else {
187             return "plan(" + joinPoint.toString() + ", " + adviceDec.toShortString() + ")";
188         }
189     }
190
191 }
192
Popular Tags