KickJava   Java API By Example, From Geeks To Geeks.

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


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.base.ast.*;
28 import org.aspectj.util.FuzzyBoolean;
29 import java.util.*;
30
31 public abstract class JpPlanner {
32     public boolean isStaticPlanner() { return false; }
33
34 // protected static class FastMatch {
35
// FuzzyBoolean value;
36
// private FastMatch(FuzzyBoolean value) {
37
// this.value = value;
38
// }
39

40         
41
42 // protected abstract static class FastMatch {
43
// public abstract FastMatch or(FastMatch other);
44
// public abstract FastMatch and(FastMatch other);
45
// public abstract FastMatch not();
46
// }
47
//
48
// protected static final FastMatch YES = new FastMatch() {
49
// public FastMatch or(FastMatch other) { return this; }
50
// public FastMatch and(FastMatch other) { return other; }
51
// public FastMatch not() { return NO; }
52
// };
53
//
54
// protected static final FastMatch NO = new FastMatch() {
55
// public FastMatch or(FastMatch other) { return other; }
56
// public FastMatch and(FastMatch other) { return this; }
57
// public FastMatch not() { return YES; }
58
// };
59
//
60
// protected static final FastMatch MAYBE = new FastMatch() {
61
// public FastMatch or(FastMatch other) {
62
// if (other == YES) return other;
63
// else return this;
64
// }
65
// public FastMatch and(FastMatch other) {
66
// if (other == NO) return other;
67
// else return this;
68
// }
69
// public FastMatch not() { return this; }
70
// };
71
//
72

73     
74     protected abstract FuzzyBoolean fastMatch(JoinPoint jp);
75
76     public boolean alwaysMatches(JoinPoint jp) {
77         return fastMatch(jp).alwaysTrue();
78     }
79
80     public boolean sometimesMatches(JoinPoint jp) {
81         return fastMatch(jp).maybeTrue();
82     }
83                                                    
84     public JpPlan makePlan(JoinPoint jp) {
85         FuzzyBoolean m = fastMatch(jp);
86         if (m.alwaysTrue()) {
87             return JpPlan.ANY_PLAN;
88         } else if (m.alwaysFalse()) {
89             return JpPlan.NO_PLAN;
90         } else {
91             jp.getCompiler().internalError("bad fastMatch result from " + this);
92             return JpPlan.NEVER_PLAN;
93         }
94     }
95     
96     public JpPlanner or(JpPlanner other) {
97         return new OrJpPlanner(this, other);
98     }
99     public JpPlanner and(JpPlanner other) {
100         return new AndJpPlanner(this, other);
101     }
102     public JpPlanner not() {
103         List newList = new ArrayList();
104         newList.add(this);
105         return new AndJpPlanner(Collections.EMPTY_LIST, newList);
106     }
107     public void walk(Object JavaDoc data) {}
108     
109     
110     public static final JpPlanner NO_PLANNER = new JpPlanner() {
111         public FuzzyBoolean fastMatch(JoinPoint jp) {
112             return FuzzyBoolean.NO;
113         }
114     };
115 }
116
117 class OrJpPlanner extends JpPlanner {
118     List list;
119     
120     public OrJpPlanner(List list) {
121         this.list = list;
122     }
123     
124     public OrJpPlanner(JpPlanner p1, JpPlanner p2) {
125         list = new ArrayList();
126         list.add(p1); list.add(p2);
127     }
128     
129     public void walk(Object JavaDoc data) {
130         for (Iterator i = list.iterator(); i.hasNext(); ) {
131             JpPlanner p = (JpPlanner)i.next();
132             p.walk(data);
133         }
134     }
135     
136     public FuzzyBoolean fastMatch(JoinPoint jp) {
137         FuzzyBoolean ret = FuzzyBoolean.NO;
138         for (Iterator i = list.iterator(); i.hasNext(); ) {
139             JpPlanner p = (JpPlanner)i.next();
140             ret = ret.or(p.fastMatch(jp));
141         }
142         return ret;
143     }
144     
145     public JpPlan makePlan(JoinPoint jp) {
146         JpPlan plan = JpPlan.NO_PLAN;
147         for (Iterator i = list.iterator(); i.hasNext(); ) {
148             JpPlanner p = (JpPlanner)i.next();
149             plan = p.makePlan(jp).or(plan);
150         }
151         return plan;
152     }
153     
154     public JpPlanner or(JpPlanner other) {
155         List newList = new ArrayList(list);
156         if (other instanceof OrJpPlanner) {
157             newList.addAll(((OrJpPlanner)other).list);
158         } else {
159             newList.add(other);
160         }
161         return new OrJpPlanner(newList);
162     }
163     
164     public JpPlanner and(JpPlanner other) {
165         List newList = new ArrayList(list.size());
166         for (Iterator i = list.iterator(); i.hasNext(); ) {
167             JpPlanner p = (JpPlanner)i.next();
168             newList.add(p.and(other));
169         }
170         return new OrJpPlanner(newList);
171     }
172     
173     public JpPlanner not() {
174         List newList = new ArrayList(list.size());
175         //XXX this wreaks the SumOfProducts game, but should still be valid
176
for (Iterator i = list.iterator(); i.hasNext(); ) {
177             JpPlanner p = (JpPlanner)i.next();
178             newList.add(p.not());
179         }
180         return new AndJpPlanner(newList, Collections.EMPTY_LIST);
181     }
182
183     public String JavaDoc toString() {
184         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
185         
186         for (Iterator i = list.iterator(); i.hasNext(); ) {
187             JpPlanner p = (JpPlanner)i.next();
188             buf.append(p.toString());
189             if (i.hasNext()) buf.append(" || ");
190         }
191         return buf.toString();
192     }
193 }
194
195
196 class AndJpPlanner extends JpPlanner {
197     List/*JpPlanner*/ _true;
198     List/*JpPlanner*/ _false;
199     
200     public AndJpPlanner(List _true, List _false) {
201         this._true = _true;
202         this._false = _false;
203     }
204     
205     public AndJpPlanner(JpPlanner p1, JpPlanner p2) {
206         _true = new ArrayList();
207         _true.add(p1); _true.add(p2);
208         _false = new ArrayList();
209     }
210     
211     public void walk(Object JavaDoc data) {
212         for (Iterator i = _true.iterator(); i.hasNext(); ) {
213             JpPlanner p = (JpPlanner)i.next();
214             p.walk(data);
215         }
216         for (Iterator i = _false.iterator(); i.hasNext(); ) {
217             JpPlanner p = (JpPlanner)i.next();
218             p.walk(data);
219         }
220     }
221     
222     public FuzzyBoolean fastMatch(JoinPoint jp) {
223         FuzzyBoolean ret = FuzzyBoolean.YES;
224         for (Iterator i = _true.iterator(); i.hasNext(); ) {
225             JpPlanner p = (JpPlanner)i.next();
226             ret = ret.and(p.fastMatch(jp));
227         }
228
229         for (Iterator i = _false.iterator(); i.hasNext(); ) {
230             JpPlanner p = (JpPlanner)i.next();
231             ret = ret.and(p.fastMatch(jp).not());
232         }
233         return ret;
234     }
235     
236     public JpPlan makePlan(JoinPoint jp) {
237 // System.out.println("jp: " + jp + ", " + this);
238
JpPlan plan = JpPlan.ANY_PLAN;
239         for (Iterator i = _true.iterator(); i.hasNext(); ) {
240             JpPlanner p = (JpPlanner)i.next();
241             plan = plan.and(p.makePlan(jp));
242         }
243         for (Iterator i = _false.iterator(); i.hasNext(); ) {
244             JpPlanner p = (JpPlanner)i.next();
245 // System.out.println("plan: " + plan);
246
// System.out.println(" p: " + p.makePlan(jp));
247
// System.out.println(" !p: " + p.makePlan(jp).not());
248

249             plan = plan.and(p.makePlan(jp).not());
250         }
251 // System.out.println("ret: " + plan);
252
return plan;
253     }
254     
255     public JpPlanner or(JpPlanner other) {
256         return new OrJpPlanner(this, other);
257     }
258     
259     public JpPlanner and(JpPlanner other) {
260         List newTrue = new ArrayList(_true);
261         List newFalse = new ArrayList(_false);
262         
263         if (other instanceof AndJpPlanner) {
264             AndJpPlanner otherAnd = (AndJpPlanner)other;
265             newTrue.addAll(otherAnd._true);
266             newFalse.addAll(otherAnd._false);
267         } else {
268             newTrue.add(other);
269         }
270         return new AndJpPlanner(newTrue, newFalse);
271     }
272     
273     public JpPlanner not() {
274         List newList = new ArrayList();
275         for (Iterator i = _true.iterator(); i.hasNext(); ) {
276             JpPlanner p = (JpPlanner)i.next();
277             newList.add(p.not());
278         }
279         for (Iterator i = _false.iterator(); i.hasNext(); ) {
280             JpPlanner p = (JpPlanner)i.next();
281             newList.add(p);
282         }
283         return new OrJpPlanner(newList);
284     }
285     
286     public String JavaDoc toString() {
287         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
288         
289         for (Iterator i = _true.iterator(); i.hasNext(); ) {
290             JpPlanner p = (JpPlanner)i.next();
291             buf.append(p.toString());
292             if (i.hasNext() || _false.size() > 0) buf.append(" && ");
293         }
294         for (Iterator i = _false.iterator(); i.hasNext(); ) {
295             JpPlanner p = (JpPlanner)i.next();
296             buf.append("!");
297             buf.append(p.toString());
298             if (i.hasNext()) buf.append(" && ");
299         }
300         return buf.toString();
301     }
302 }
303
Popular Tags