KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.aspectj.compiler.base.ast.*;
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.crosscuts.*;
29 import org.aspectj.compiler.crosscuts.joinpoints.*;
30 import org.aspectj.compiler.base.cst.*;
31
32 import java.util.*;
33
34 /**
35  * @grammar ...
36  * @child GenTypeName dominates @after superInterfaces
37  * @child PerClause perClause @after dominates
38  */

39 public class AspectDec extends ClassDec {
40     public String JavaDoc getKind() { return "aspect"; }
41       
42     public Type getSuperClassType() {
43         //??? better error handling here
44
return super.getSuperClassType();
45     }
46     
47     public PerClause findPerClause() { return getPerClause(); }
48
49     public AspectDec getParentAspect() {
50         Type superType = getSuperClassType();
51         if (superType.isAspect()) return (AspectDec)((NameType)superType).getTypeDec();
52         return null;
53     }
54
55     public void checkSpec() {
56         super.checkSpec();
57         if (getType().isSubtypeOf(getTypeManager().getSerializableType()) &&
58             !getOptions().XserializableAspects) {
59             showError("aspects may not implement java.io.Serializable");
60         }
61         if (getType().isSubtypeOf(getTypeManager().getCloneableType())) {
62             showError("aspects may not implement java.lang.Cloneable");
63         }
64
65         if (isInnerType() && !isStatic()) {
66             showError("inner aspects must be static");
67         }
68     }
69
70
71     protected boolean explicitDominates(AspectDec otherAspect) {
72         if (dominates == null) {
73             if (getParentAspect() != null) {
74                 return getParentAspect().explicitDominates(otherAspect);
75             } else {
76                 return false;
77             }
78         }
79         return dominates.matches(otherAspect.getType());
80     }
81
82     public boolean dominates(AspectDec otherAspect) {
83         if (otherAspect == this) return false;
84         
85         if (explicitDominates(otherAspect)) return true;
86         if (otherAspect.explicitDominates(this)) return false;
87
88         if (getType().isStrictSubtypeOf(otherAspect.getType())) return true;
89         //??? might want to do some extra work here for explict dominates specs
90
return false;
91     }
92
93     public boolean hasAdvicesOrIntroductions() {
94         return true;
95     }
96
97     protected void maybeShowWarning(Dec dec) { }
98
99     public List getIntroductions() {
100         return introductions;
101     }
102     
103     List introductions = new LinkedList();
104     public void addDec(Dec dec) {
105         if (dec instanceof AdviceDec) {
106             return;
107         } else if (dec instanceof IntroducedDec) {
108             introductions.add(dec);
109         } else if (dec instanceof ShowErrorDec) {
110             return;
111         } else if (dec instanceof SoftThrowableDec) {
112             return;
113         } else if (dec instanceof IntroducedSuperDec) {
114             introductions.add(dec);
115         } else {
116             super.addDec(dec);
117         }
118     }
119     
120
121     public void postIntroductionFinish() {
122         super.postIntroductionFinish();
123                      
124         if (perClause == null) {
125             if (getParentAspect() == null) {
126                 setPerClause(new PerSingleton(getSourceLocation()));
127             } else {
128                 setPerClause((PerClause)getParentAspect().getPerClause().copy());
129             }
130         }
131         
132         if (getParentAspect() != null && !getParentAspect().isAbstract()) {
133             superClass.showError("can only extend abstract aspects, not "
134                 + getParentAspect().getType().getString());
135             return;
136         }
137        
138         
139         if (!isAbstract()) {
140             getPerClause().setupAspect();
141         }
142     }
143
144     private Map extraPlanners = new HashMap();
145     public Map getExtraPlanners() { return extraPlanners; }
146
147     protected void walkExtendsAndImplements(ScopeWalker walker) {
148         //XXX extremely odd location for this
149
if (perClause instanceof PerThisOrTarget) {
150             getModifiers().setPublic(true);
151         }
152
153         if (dominates != null) walker.process(dominates);
154         super.walkExtendsAndImplements(walker);
155     }
156
157     protected void walkBody(ScopeWalker walker) {
158         if (perClause != null) walker.process(perClause);
159         super.walkBody(walker);
160     }
161     
162     public void addConstructorDec(ConstructorDec constructorDec) {
163         if (constructorDec.getFormals().size() != 0) {
164             constructorDec.showError("only 0-argument constructors allowed in an aspect");
165             return;
166         }
167         super.addConstructorDec(constructorDec);
168     }
169     
170
171
172
173     protected List myJpPlannerMakers = null;
174     public List getJpPlannerMakers() {
175         if (myJpPlannerMakers != null) return myJpPlannerMakers;
176
177         List ret = new LinkedList();
178         final int N = getBody().size();
179         for (int i = 0; i < N; i++) {
180             Dec dec = getBody().get(i);
181             if (dec instanceof JpPlannerMaker) {
182                 ret.add(dec);
183             }
184         }
185
186         AspectDec parentAspect = getParentAspect();
187         if (parentAspect != null) {
188             if (!parentAspect.isAbstract()) {
189                 showError("only abstract aspects can be extended, " + parentAspect.toShortString() + " is not abstract");
190             }
191
192             ret.addAll(parentAspect.getJpPlannerMakers());
193         }
194
195         myJpPlannerMakers = ret;
196         return ret;
197     }
198     
199
200     /*
201     protected List myAdvice = null;
202     public List getAdvice() {
203         if (myAdvice != null) return myAdvice;
204
205 // if (isAbstract() && ofClause != null) {
206 // showError("abstract aspects can not have of clauses");
207 // }
208
209         if (isInnerType() && !isStatic()) {
210             showError("inner aspects must be static");
211         }
212
213         List ret = new LinkedList();
214         final int N = getBody().size();
215         for (int i = 0; i < N; i++) {
216             Dec dec = getBody().get(i);
217             if (dec instanceof AdviceDec) {
218                 ret.add(dec);
219             }
220         }
221
222         //??? add in non-copies of all advice from my parent
223         AspectDec parentAspect = getParentAspect();
224         if (parentAspect != null) {
225             if (!parentAspect.isAbstract()) {
226                 showError("only abstract aspects can be extended, " + parentAspect.toShortString() + " is not abstract");
227             }
228             
229             List extraAdvice = parentAspect.getAdvice();
230             for(Iterator i = extraAdvice.iterator(); i.hasNext(); ) {
231                 AdviceDec adviceDec = (AdviceDec)i.next(); //).copy();
232
233                 //this will go away when we remove deprecated support for static advice
234                 if (adviceDec.isStatic()) continue;
235
236                 //adviceDec.buildContext(getBody().getContext());
237                 ret.add(adviceDec);
238             }
239         }
240
241         myAdvice = ret;
242         return ret;
243     }
244     */

245     
246     // ------------------------------
247
// Inner class stuff
248

249     // AAARGH! This shouldn't be a subtype of class dec.
250

251     public boolean isInner() { return false; }
252
253     //BEGIN: Generated from @child and @property
254
protected GenTypeName dominates;
255     public GenTypeName getDominates() { return dominates; }
256     public void setDominates(GenTypeName _dominates) {
257         if (_dominates != null) _dominates.setParent(this);
258         dominates = _dominates;
259     }
260     
261     protected PerClause perClause;
262     public PerClause getPerClause() { return perClause; }
263     public void setPerClause(PerClause _perClause) {
264         if (_perClause != null) _perClause.setParent(this);
265         perClause = _perClause;
266     }
267     
268     public AspectDec(SourceLocation location, Modifiers _modifiers, String JavaDoc _id, TypeD _superClass, TypeDs _superInterfaces, GenTypeName _dominates, PerClause _perClause, Decs _body) {
269         super(location, _modifiers, _id, _superClass, _superInterfaces, _body);
270         setDominates(_dominates);
271         setPerClause(_perClause);
272     }
273     protected AspectDec(SourceLocation source) {
274         super(source);
275     }
276     
277     public ASTObject copyWalk(CopyWalker walker) {
278         AspectDec ret = new AspectDec(getSourceLocation());
279         ret.preCopy(walker, this);
280         if (modifiers != null) ret.setModifiers( (Modifiers)walker.process(modifiers) );
281         ret.id = id;
282         if (superClass != null) ret.setSuperClass( (TypeD)walker.process(superClass) );
283         if (superInterfaces != null) ret.setSuperInterfaces( (TypeDs)walker.process(superInterfaces) );
284         if (dominates != null) ret.setDominates( (GenTypeName)walker.process(dominates) );
285         if (perClause != null) ret.setPerClause( (PerClause)walker.process(perClause) );
286         if (body != null) ret.setBody( (Decs)walker.process(body) );
287         return ret;
288     }
289     
290     public ASTObject getChildAt(int childIndex) {
291         switch(childIndex) {
292         case 0: return modifiers;
293         case 1: return superClass;
294         case 2: return superInterfaces;
295         case 3: return dominates;
296         case 4: return perClause;
297         case 5: return body;
298         default: return super.getChildAt(childIndex);
299         }
300     }
301      public String JavaDoc getChildNameAt(int childIndex) {
302         switch(childIndex) {
303         case 0: return "modifiers";
304         case 1: return "superClass";
305         case 2: return "superInterfaces";
306         case 3: return "dominates";
307         case 4: return "perClause";
308         case 5: return "body";
309         default: return super.getChildNameAt(childIndex);
310         }
311     }
312      public void setChildAt(int childIndex, ASTObject child) {
313         switch(childIndex) {
314         case 0: setModifiers((Modifiers)child); return;
315         case 1: setSuperClass((TypeD)child); return;
316         case 2: setSuperInterfaces((TypeDs)child); return;
317         case 3: setDominates((GenTypeName)child); return;
318         case 4: setPerClause((PerClause)child); return;
319         case 5: setBody((Decs)child); return;
320         default: super.setChildAt(childIndex, child); return;
321         }
322     }
323      public int getChildCount() {
324         return 6;
325     }
326     
327     public String JavaDoc getDefaultDisplayName() {
328         return "AspectDec(id: "+id+")";
329     }
330     
331     //END: Generated from @child and @property
332
}
333
334
Popular Tags