KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdoc > AspectDocImpl


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger 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 package org.aspectj.tools.ajdoc;
23
24 import org.aspectj.ajdoc.AdviceDoc;
25 import org.aspectj.ajdoc.AspectDoc;
26 import org.aspectj.ajdoc.IntroductionDoc;
27 import org.aspectj.ajdoc.OfClauseDoc;
28 import org.aspectj.compiler.base.ast.Decs;
29 import org.aspectj.compiler.crosscuts.ast.AdviceDec;
30 import org.aspectj.compiler.crosscuts.ast.AspectDec;
31 import org.aspectj.compiler.crosscuts.ast.IntroducedSuperDec;
32
33 import com.sun.javadoc.ClassDoc;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42 public class AspectDocImpl extends ClassDocImpl implements AspectDoc {
43
44     /** Array of AdviceDoc created from this AspectDecs AdviceDecs. */
45     private final Collection JavaDoc advice;
46
47     /** Array of IntroductionDec created from this AspectDecs introductions. */
48     private final Collection JavaDoc introductions;
49
50     /** The of clause this aspect has -- may be null. */
51     private final OfClauseDoc ofClause;
52
53     /** The aspects that dominate this aspect. */
54     private final Collection JavaDoc dominators = new ArrayList JavaDoc();
55
56     /** The aspects that are dominated by this aspect. */
57     private final Collection JavaDoc dominatees = new ArrayList JavaDoc();
58
59     /**
60      * Constructs an AspectDoc with the containing ClassDoc
61      * and underlying AspectDec.
62      *
63      * @param containingClass contained ClassDoc.
64      * @param aspectDec underlying AspectDec.
65      */

66     public AspectDocImpl(ClassDoc containingClass, AspectDec aspectDec) {
67         super(containingClass, aspectDec);
68         introductions = createIntroductions();
69         advice = createAdvice();
70         ofClause = createOfClause();
71     }
72
73     /**
74      * Returns an instance of AdviceDocImpl corresponding to
75      * the AdviceDec passed in.
76      *
77      * @param dec the AdviceDec mapping to the desired
78      * AdviceDocImpl.
79      * @return an instance of AdviceDocImpl corresponding to
80      * the AdviceDec passed in.
81      */

82     public AdviceDocImpl docForDec(AdviceDec dec) {
83         for (Iterator JavaDoc i = advice.iterator(); i.hasNext();) {
84             AdviceDocImpl ad = (AdviceDocImpl)i.next();
85             if (ad.dec() == dec) return ad;
86         }
87         return null;
88     }
89
90     /**
91      * Returns an instance of IntroducedSuperDocImpl corresponding to
92      * the IntroducedSuperDec passed in.
93      *
94      * @param dec the IntroducedSuperDec mapping to the
95      * desired IntroducedSuperDocImpl
96      * @return an instance of IntroducedSuperDocImpl
97      * corresponding to the IntroducedSuperDec
98      * passed in.
99      */

100     public IntroducedSuperDocImpl introDocForDec(IntroducedSuperDec dec) {
101         for (Iterator JavaDoc i = introductions.iterator(); i.hasNext();) {
102             ProgramElementDocImpl id = (ProgramElementDocImpl)i.next();
103             if (id.dec() == dec) return (IntroducedSuperDocImpl)id;
104         }
105         return null;
106     }
107
108     /**
109      * Returns the underlying AspectDec.
110      *
111      * @return the underlying AspectDec.
112      */

113     protected AspectDec aspectDec() {
114         return (AspectDec)typeDec();
115     }
116
117     /**
118      * Returns the visible advice in this aspect.
119      *
120      * @return an array of AdviceDoc representing the
121      * visible advice in this aspect.
122      */

123     public AdviceDoc[] advice() {
124         return (AdviceDocImpl[])advice.toArray
125             (new AdviceDocImpl[advice.size()]);
126     }
127
128     /**
129      * Returns the aspects that are dominated by this aspect.
130      *
131      * @return an array of AspectDec representing the aspects
132      * that are dominated by this aspect.
133      */

134     public AspectDoc[] dominatees() {
135         return (AspectDoc[])dominatees.toArray
136             (new AspectDoc[dominatees.size()]);
137     }
138    
139     /**
140      * Return the aspects that dominate this aspect.
141      *
142      * @return an array of AspectDoc representing the aspects
143      * that dominate this aspect.
144      */

145     public AspectDoc[] dominators() {
146         return (AspectDoc[])dominators.toArray
147             (new AspectDoc[dominators.size()]);
148     }
149
150     /**
151      * TODO
152      * Returns the visible introductions of this aspect.
153      *
154      * @return an array of IntroductionDoc representing the
155      * visible introductions in this aspect.
156      */

157     public IntroductionDoc[] introductions() {
158         return (IntroductionDocImpl[])introductions.toArray
159             (new IntroductionDocImpl[introductions.size()]);
160     }
161
162     /**
163      * Returns the <i>of clause</i> of this aspect.
164      *
165      * @return the <i>of clause</i> of this aspect.
166      */

167     public OfClauseDoc ofClause() {
168         return ofClause;
169     }
170
171     /**
172      * Returns <code>true</code>.
173      *
174      * @return <code>true</code>.
175      */

176     public boolean isAspect() {
177         return true;
178     }
179
180     /**
181      * Returns <code>true</code> if this aspects dominates
182      * the passed in aspect.
183      *
184      * @param other an AspectDoc that represents another
185      * aspect in this world.
186      * @return <code>true</code> if this aspects dominates
187      * the passed in aspect.
188      */

189     public boolean dominates(AspectDoc other) {
190         if (!(other instanceof AspectDocImpl)) {
191             return false;
192         }
193         return aspectDec().dominates(((AspectDocImpl)other).aspectDec());
194     }
195
196     /**
197      * Adds a dominates relation from <code>dominator</code> to
198      * <code>this</code>. For example, somewhere in the code
199      * the line
200      * <code>
201      * aspect dominator dominates this { ... }
202      * </code>
203      * exists.
204      *
205      * @param dominator an instance of AspectDocImpl that
206      * dominates this.
207      */

208     public void addDominator(AspectDoc dominator) {
209         dominators.add(dominator);
210     }
211
212     /**
213      * Adds a dominates relation from <code>dominator</code> to
214      * <code>this</code>. For example, somewhere in the code
215      * the line
216      * <code>
217      * aspect this dominates dominatee { ... }
218      * </code>
219      * exists.
220      *
221      * @param dominatee an instance of AspectDocImpl that
222      * is dominated by this.
223      */

224     public void addDominatee(AspectDoc dominatee) {
225         dominatees.add(dominatee);
226     }
227
228     /**
229      * Returns a Collection of IntroductionDocImpl representing
230      * the introductions declared in this aspect.
231      *
232      * @return a Collection of IntroductionDocImpl representing
233      * the introductions declared in this aspect.
234      */

235     private Collection JavaDoc createIntroductions() {
236         Decs decs = aspectDec().getBody();
237         if (decs.size() < 1) return Collections.EMPTY_LIST;
238         Collection JavaDoc list = new HashSet JavaDoc();
239         for (Iterator JavaDoc i = decs.getList().iterator(); i.hasNext();) {
240             Object JavaDoc o = IntroductionDocImpl.getInstance(this, i.next());
241             if (o != null) list.add(o);
242         }
243         return list;
244     }
245
246     /**
247      * Returns a Collection of AdviceDocImpl representing
248      * the advice declared in this aspect.
249      *
250      * @return a Collection of AdviceDocImpl representing
251      * the advice declared in this aspect.
252      */

253     private Collection JavaDoc createAdvice() {
254         // pluck the AdviceDec from the list of JpPlannerMakers
255
List JavaDoc decs = aspectDec().getJpPlannerMakers();
256         if (null != decs) {
257             final int QUIT = 2;
258             for (int tries = 0; tries < QUIT; tries++) {
259                 try {
260                     for (Iterator JavaDoc i = decs.iterator(); i.hasNext();) {
261                         Object JavaDoc o = i.next();
262                         if (!(o instanceof AdviceDec)) {
263                             i.remove();
264                         }
265                     }
266                     tries = QUIT;
267                 } catch (UnsupportedOperationException JavaDoc o) {
268                     if (0 != tries) {
269                         tries = QUIT;
270                     } else {
271                         ArrayList JavaDoc list = new ArrayList JavaDoc();
272                         list.addAll(decs);
273                         decs = list;
274                     }
275                 }
276             }
277         }
278         
279         if ((null == decs) || (decs.size() < 1)) {
280             return Collections.EMPTY_LIST;
281         }
282
283         List JavaDoc list = new ArrayList JavaDoc();
284         for (Iterator JavaDoc i = decs.iterator(); i.hasNext();) {
285             list.add(new AdviceDocImpl(this, (AdviceDec)i.next()));
286         }
287         return list;
288     }
289
290     /**
291      * Returns an instance of OfClauseDoc representing
292      * the of clause declared by this aspect.
293      *
294      * @return an instance of OfClauseDoc representing
295      * the of clause declared by this aspect.
296      */

297     private OfClauseDoc createOfClause() {
298         return OfClauseDocImpl.getInstance(aspectDec().getPerClause());
299     }
300 }
301
Popular Tags