KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > doclets > standard > ClassUseMapper


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.doclets.standard;
23
24 import org.aspectj.ajdoc.AdviceDoc;
25 import org.aspectj.ajdoc.AspectDoc;
26 import org.aspectj.ajdoc.IntroducedDoc;
27 import org.aspectj.ajdoc.IntroducedSuperDoc;
28 import org.aspectj.ajdoc.IntroductionDoc;
29 import org.aspectj.ajdoc.PointcutDoc;
30 import org.aspectj.tools.ajdoc.Util;
31
32 import com.sun.javadoc.ClassDoc;
33 import com.sun.javadoc.ExecutableMemberDoc;
34 import com.sun.javadoc.FieldDoc;
35 import com.sun.javadoc.MemberDoc;
36 import com.sun.javadoc.PackageDoc;
37 import com.sun.javadoc.Parameter;
38 import com.sun.javadoc.ProgramElementDoc;
39 import com.sun.javadoc.RootDoc;
40 import com.sun.javadoc.Type;
41 import com.sun.tools.doclets.ClassTree;
42 import com.sun.tools.doclets.DocletAbortException;
43
44 import java.lang.reflect.Constructor JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Collection JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.HashSet JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52
53 /**
54  * Provides support for aspects.
55  *
56  * @author Jeff Palm
57  */

58 public class ClassUseMapper {
59
60
61     /**
62      * Maps a ClassDoc to advice that return its type.
63      */

64     public final Map JavaDoc classToAdviceReturn = new HashMap JavaDoc();
65     
66     /**
67      * Maps a ClassDoc to advice that have its type
68      * are arguments.
69      */

70     public final Map JavaDoc classToAdviceArgs = new HashMap JavaDoc();
71     
72     /**
73      * Maps a ClassDoc to pointcuts that return its type.
74      */

75     public final Map JavaDoc classToPointcutReturn = new HashMap JavaDoc();
76     
77     /**
78      * Maps a ClassDoc to pointcuts that have its type
79      * as arguments.
80      */

81     public final Map JavaDoc classToPointcutArgs = new HashMap JavaDoc();
82     
83     /**
84      * Maps a ClassDoc to field introductions that
85      * are its type.
86      */

87     public final Map JavaDoc classToFieldIntroductions = new HashMap JavaDoc();
88
89     /**
90      * Maps a ClassDoc to class introductions that
91      * are its type.
92      */

93     public final Map JavaDoc classToClassIntroductions = new HashMap JavaDoc();
94     
95     /**
96      * Maps a ClassDoc to interface introductions that
97      * are its type.
98      */

99     public final Map JavaDoc classToInterfaceIntroductions = new HashMap JavaDoc();
100     
101     /**
102      * Maps a ClassDoc to aspects that advise it.
103      */

104     public final Map JavaDoc classToAdvisors = new HashMap JavaDoc();
105     
106     /**
107      * Maps a ClassDoc to aspects that it dominates.
108      */

109     public final Map JavaDoc classToDominatees = new HashMap JavaDoc();
110     
111     /**
112      * Maps a ClassDoc to aspects that dominate it..
113      */

114     public final Map JavaDoc classToDominators = new HashMap JavaDoc();
115     
116
117     public static void generate(RootDoc root, ClassTree classtree)
118         throws DocletAbortException {
119         try {
120             
121             ClassUseMapper mapper = new ClassUseMapper(root, classtree);
122
123             ClassDoc[] classes = root.classes();
124             for (int i = 0; i < classes.length; i++) {
125                 ClassUseWriter.generate(mapper, classes[i]);
126             }
127             PackageDoc[] pkgs = Standard.configuration().packages;
128             for (int i = 0; i < pkgs.length; i++) {
129                 com.sun.tools.doclets.standard.PackageUseWriter.
130                     generate(mapper.mapper, pkgs[i]);
131             }
132         } catch (Exception JavaDoc e) {
133             e.printStackTrace();
134             Standard.configuration().standardmessage.
135                 error("doclet.exception", e+"",
136                       "creating class use tree");
137             throw new DocletAbortException();
138         }
139     }
140
141     protected final com.sun.tools.doclets.standard.ClassUseMapper mapper;
142     
143     public ClassUseMapper(RootDoc root, ClassTree classtree)
144         throws Exception JavaDoc {
145         Constructor JavaDoc constr =
146             com.sun.tools.doclets.standard.ClassUseMapper.class.
147             getDeclaredConstructor(new Class JavaDoc[] {
148                 com.sun.javadoc.RootDoc.class,
149                 com.sun.tools.doclets.ClassTree.class,
150             });
151         constr.setAccessible(true);
152         mapper = (com.sun.tools.doclets.standard.ClassUseMapper)constr.
153             newInstance(new Object JavaDoc[]{root, classtree});
154
155         classToPackageSave = new HashMap JavaDoc();
156         for (Iterator JavaDoc i = mapper.classToPackage.keySet().iterator(); i.hasNext();) {
157             Object JavaDoc key = i.next();
158             classToPackageSave.put(key, new HashSet JavaDoc((Collection JavaDoc)
159                                                     mapper.classToPackage.
160                                                     get(key)));
161         }
162         
163         finish(root, classtree);
164     }
165
166     protected Object JavaDoc saved;
167     protected final Map JavaDoc classToPackageSave;
168     protected final com.sun.tools.doclets.standard.ClassUseMapper mapper
169         (ClassDoc classdoc)
170     {
171         Object JavaDoc noaspects = classToPackageSave.get(classdoc);
172         saved = mapper.classToPackage.get(classdoc);
173         mapper.classToPackage.put(classdoc, noaspects);
174         return mapper;
175     }
176
177     protected void restore(ClassDoc classdoc) {
178         mapper.classToPackage.put(classdoc, saved);
179     }
180    
181     protected void finish(RootDoc root, ClassTree classtree) {
182        
183         ClassDoc[] classes = root.classes();
184         for (int i = 0; i < classes.length; i++) {
185             ClassDoc cd = classes[i];
186             if (cd instanceof org.aspectj.ajdoc.ClassDoc) {
187                 org.aspectj.ajdoc.ClassDoc acd = (org.aspectj.ajdoc.ClassDoc)cd;
188                 PointcutDoc[] pcs = acd.pointcuts();
189                 for (int j = 0; j < pcs.length; j++) {
190                     PointcutDoc pd = pcs[j];
191                     mapExecutable(pd);
192                     Type result = pd.resultType();
193                     if (result != null) {
194                         ClassDoc tcd = result.asClassDoc();
195                         if (tcd != null) {
196                             add(classToPointcutReturn, tcd, pd);
197                         }
198                     }
199                 }
200             }
201             
202             if (cd instanceof AspectDoc) {
203                 AspectDoc ad = (AspectDoc)cd;
204                 AdviceDoc[] adocs = ad.advice();
205                 for (int j = 0; j < adocs.length; j++) {
206                     AdviceDoc adoc = adocs[j];
207                     mapExecutable(adoc);
208                     Type result = adoc.returnType();
209                     if (result != null) {
210                         ClassDoc tcd = result.asClassDoc();
211                         if (tcd != null) {
212                             add(classToAdviceReturn, tcd, adoc);
213                         }
214                     }
215                     ExecutableMemberDoc[] emds = adoc.crosscuts();
216                     if (null != emds) {
217                         for (int k = 0; k < emds.length; k++) {
218                             ExecutableMemberDoc emd = emds[k];
219                             if (null != emd) {
220                                 ClassDoc tcd = emd.containingClass();
221                                 ClassDoc fcd = adoc.containingClass();
222                                 //TODO: This probably sucks!!!
223
if (!refList(classToAdvisors, tcd).contains(fcd)) {
224                                     add(classToAdvisors, tcd, fcd);
225                                 }
226                             }
227                         }
228                     }
229                 }
230
231                 IntroductionDoc[] ids = ad.introductions();
232                 for (int j = 0; j < ids.length; j++) {
233                     IntroductionDoc id = ids[j];
234                     if (id instanceof IntroducedDoc) {
235                         IntroducedDoc idd = (IntroducedDoc)id;
236                         MemberDoc mem = idd.member();
237                         if (mem.isField()) {
238                             FieldDoc fd = (FieldDoc)mem;
239                             ClassDoc tcd = fd.type().asClassDoc();
240                             add(classToFieldIntroductions, tcd, fd);
241                         }
242                     } else if (id instanceof IntroducedSuperDoc) {
243                         IntroducedSuperDoc idd = (IntroducedSuperDoc)id;
244                         boolean isImplements = idd.isImplements();
245                         Type[] types = idd.types();
246                         for (int k = 0; k < types.length; k++) {
247                             ClassDoc tcd = types[k].asClassDoc();
248                             add(isImplements ?
249                                 classToInterfaceIntroductions :
250                                 classToClassIntroductions, tcd, idd);
251                         }
252                     }
253                 }
254                 AspectDoc[] dominatees = ad.dominatees();
255                 for (int j = 0; j < dominatees.length; j++) {
256                     add(classToDominatees, ad, dominatees[j]);
257                 }
258                 AspectDoc[] dominators = ad.dominators();
259                 for (int j = 0; j < dominators.length; j++) {
260                     add(classToDominators, ad, dominators[j]);
261                 }
262             }
263         }
264     }
265     
266     protected void mapExecutable(ExecutableMemberDoc em) {
267         Parameter[] params = em.parameters();
268         List JavaDoc classargs = new ArrayList JavaDoc();
269         Map JavaDoc argsmap = ((org.aspectj.ajdoc.MemberDoc)em).isAdvice() ?
270             classToAdviceArgs : classToPointcutArgs ;
271         for (int i = 0; i < params.length; i++) {
272             ClassDoc pcd = params[i].type().asClassDoc();
273             if (pcd != null && !classargs.contains(pcd)) {
274                 add(argsmap, pcd, em);
275                 classargs.add(pcd);
276             }
277         }
278     }
279     
280     protected List JavaDoc refList(Map JavaDoc map, ClassDoc cd) {
281         return (List JavaDoc)Util.invoke(mapper, "refList",
282                                  new Class JavaDoc[]{java.util.Map JavaDoc.class,
283                                              com.sun.javadoc.ClassDoc.class},
284                                  new Object JavaDoc[]{map, cd});
285     }
286
287     protected void add(Map JavaDoc map, ClassDoc cd, ProgramElementDoc ref) {
288         Util.invoke(mapper, "add",
289                     new Class JavaDoc[]{java.util.Map JavaDoc.class,
290                                 com.sun.javadoc.ClassDoc.class,
291                                 com.sun.javadoc.ProgramElementDoc.class},
292                     new Object JavaDoc[]{map, cd, ref});
293     }
294 }
295
Popular Tags