KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > JavadocEnv


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source;
21
22 import com.sun.javadoc.ClassDoc;
23 import com.sun.tools.javac.code.Flags;
24 import com.sun.tools.javac.code.Kinds;
25 import com.sun.tools.javac.code.Symbol.ClassSymbol;
26 import com.sun.tools.javac.code.Symbol.MethodSymbol;
27 import com.sun.tools.javac.code.Symbol.PackageSymbol;
28 import com.sun.tools.javac.code.Symbol.TypeSymbol;
29 import com.sun.tools.javac.code.Symbol.VarSymbol;
30 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
31 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
32 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
33 import com.sun.tools.javac.util.Context;
34 import com.sun.tools.javac.util.Name;
35 import com.sun.tools.javac.util.Position;
36 import com.sun.tools.javadoc.AnnotationTypeDocImpl;
37 import com.sun.tools.javadoc.AnnotationTypeElementDocImpl;
38 import com.sun.tools.javadoc.ClassDocImpl;
39 import com.sun.tools.javadoc.ConstructorDocImpl;
40 import com.sun.tools.javadoc.DocEnv;
41 import com.sun.tools.javadoc.ExecutableMemberDocImpl;
42 import com.sun.tools.javadoc.FieldDocImpl;
43 import com.sun.tools.javadoc.MethodDocImpl;
44 import com.sun.tools.javadoc.ModifierFilter;
45 import com.sun.tools.javadoc.PackageDocImpl;
46 import java.io.IOException JavaDoc;
47 import java.util.StringTokenizer JavaDoc;
48 import javax.lang.model.element.Element;
49 import org.netbeans.api.java.source.CancellableTask;
50 import org.netbeans.api.java.source.ClasspathInfo;
51 import org.netbeans.api.java.source.CompilationController;
52 import org.netbeans.api.java.source.ElementHandle;
53 import org.netbeans.api.java.source.JavaSource;
54 import org.netbeans.api.java.source.JavaSource.Phase;
55 import org.netbeans.api.java.source.SourceUtils;
56 import org.openide.filesystems.FileObject;
57 import org.openide.util.Exceptions;
58
59 /**
60  *
61  * @author Dusan Balek
62  */

63 public class JavadocEnv extends DocEnv {
64
65     public static void preRegister(final Context context, final ClasspathInfo cpInfo) {
66         context.put(docEnvKey, new Context.Factory<DocEnv>() {
67             public DocEnv make() {
68                 return new JavadocEnv(context, cpInfo);
69             }
70         });
71     }
72     
73     private ClasspathInfo cpInfo;
74     private Context ctx;
75     
76     private JavadocEnv(Context context, ClasspathInfo cpInfo) {
77         super(context);
78         this.ctx = context;
79         this.cpInfo = cpInfo;
80         this.showAccess = new ModifierFilter(ModifierFilter.ALL_ACCESS);
81         this.legacyDoclet = false;
82     }
83     
84     @Override JavaDoc
85     public ClassDocImpl getClassDoc(ClassSymbol clazz) {
86         ClassDocImpl result = classMap.get(clazz);
87         if (result != null) return result;
88         if (isAnnotationType(clazz)) {
89             result = new JavadocAnnotation(this, clazz);
90         } else {
91             result = new JavadocClass(this, clazz);
92         }
93         classMap.put(clazz, result);
94         return result;
95     }
96     
97     @Override JavaDoc
98     protected void makeClassDoc(ClassSymbol clazz, String JavaDoc docComment, JCClassDecl tree, Position.LineMap lineMap) {
99         ClassDocImpl result = classMap.get(clazz);
100         if (result != null) {
101             if (docComment != null) result.setRawCommentText(docComment);
102             return;
103         }
104         if (isAnnotationType(tree)) { // flags of clazz may not yet be set
105
result = new JavadocAnnotation(this, clazz, docComment);
106         } else {
107             result = new JavadocClass(this, clazz, docComment);
108         }
109         classMap.put(clazz, result);
110     }
111     
112     @Override JavaDoc
113     public FieldDocImpl getFieldDoc(VarSymbol var) {
114         FieldDocImpl result = fieldMap.get(var);
115         if (result != null) return result;
116         result = new JavadocField(this, var);
117         fieldMap.put(var, result);
118         return result;
119     }
120
121     @Override JavaDoc
122     protected void makeFieldDoc(VarSymbol var, String JavaDoc docComment, JCVariableDecl tree, Position.LineMap lineMap) {
123         FieldDocImpl result = fieldMap.get(var);
124         if (result != null) {
125             if (docComment != null) result.setRawCommentText(docComment);
126         } else {
127             result = new JavadocField(this, var, docComment);
128             fieldMap.put(var, result);
129         }
130     }
131
132     @Override JavaDoc
133     public MethodDocImpl getMethodDoc(MethodSymbol meth) {
134         ExecutableMemberDocImpl docImpl = methodMap.get(meth);
135         if (docImpl != null && !docImpl.isMethod())
136             return null;
137         MethodDocImpl result = (MethodDocImpl)docImpl;
138         if (result != null) return result;
139         result = new JavadocMethod(this, meth);
140         methodMap.put(meth, result);
141         return result;
142     }
143     
144     @Override JavaDoc
145     protected void makeMethodDoc(MethodSymbol meth, String JavaDoc docComment, JCMethodDecl tree, Position.LineMap lineMap) {
146         MethodDocImpl result = (MethodDocImpl)methodMap.get(meth);
147         if (result != null) {
148             if (docComment != null) result.setRawCommentText(docComment);
149         } else {
150             result = new JavadocMethod(this, meth, docComment);
151             methodMap.put(meth, result);
152         }
153     }
154     
155     @Override JavaDoc
156     public ConstructorDocImpl getConstructorDoc(MethodSymbol meth) {
157         ConstructorDocImpl result = (ConstructorDocImpl)methodMap.get(meth);
158         if (result != null) return result;
159         result = new JavadocConstructor(this, meth);
160         methodMap.put(meth, result);
161         return result;
162     }
163
164     @Override JavaDoc
165     protected void makeConstructorDoc(MethodSymbol meth, String JavaDoc docComment, JCMethodDecl tree, Position.LineMap lineMap) {
166         ConstructorDocImpl result = (ConstructorDocImpl)methodMap.get(meth);
167         if (result != null) {
168             if (docComment != null) result.setRawCommentText(docComment);
169         } else {
170             result = new JavadocConstructor(this, meth, docComment);
171             methodMap.put(meth, result);
172         }
173     }
174
175     @Override JavaDoc
176     public AnnotationTypeElementDocImpl getAnnotationTypeElementDoc(MethodSymbol meth) {
177         AnnotationTypeElementDocImpl result = (AnnotationTypeElementDocImpl)methodMap.get(meth);
178         if (result != null) return result;
179         result = new JavadocAnnotationTypeElement(this, meth);
180         methodMap.put(meth, result);
181         return result;
182     }
183     
184     @Override JavaDoc
185     protected void makeAnnotationTypeElementDoc(MethodSymbol meth, String JavaDoc docComment, JCMethodDecl tree, Position.LineMap lineMap) {
186         AnnotationTypeElementDocImpl result = (AnnotationTypeElementDocImpl)methodMap.get(meth);
187         if (result != null) {
188             if (docComment != null) result.setRawCommentText(docComment);
189         } else {
190             result = new JavadocAnnotationTypeElement(this, meth, docComment);
191             methodMap.put(meth, result);
192         }
193     }
194
195     /**
196      * Return the AnnotationTypeElementDoc for a MethodSymbol.
197      * Should be called only on symbols representing annotation type elements.
198      */

199     @Override JavaDoc
200     public PackageDocImpl getPackageDoc(PackageSymbol pack) {
201         PackageDocImpl result = packageMap.get(pack);
202         if (result != null) return result;
203         result = new JavaDocPackage(this, pack, ctx);
204         packageMap.put(pack, result);
205         return result;
206     }
207     
208     @Override JavaDoc
209     public ClassDocImpl lookupClass(String JavaDoc name) {
210         ClassDocImpl cls = super.lookupClass(name);
211         if (cls == null)
212             cls = loadClass(name);
213         return cls;
214     }
215     
216     public interface ElementHolder {
217         Element getElement();
218     }
219     
220     private String JavaDoc getRawCommentFor(Element element) {
221         try {
222             FileObject fo = SourceUtils.getFile(element, cpInfo);
223             if (fo != null) {
224                 JavaSource js = JavaSource.forFileObject(fo);
225                 if (js != null) {
226                     final String JavaDoc[] ret = new String JavaDoc[1];
227                     final ElementHandle<? extends Element> handle = ElementHandle.create(element);
228                     js.runUserActionTask(new CancellableTask<CompilationController>() {
229                         public void cancel() {
230                         }
231                         public void run(CompilationController controller) throws Exception JavaDoc {
232                             controller.toPhase(Phase.ELEMENTS_RESOLVED);
233                             Element e = handle.resolve(controller);
234                             if (e != null)
235                                 ret[0] = controller.getElements().getDocComment(e);
236                         }
237                     },true);
238                     return ret[0] != null ? ret[0] : ""; //NOI18N
239
}
240             }
241         } catch (IOException JavaDoc ex) {
242             Exceptions.printStackTrace(ex);
243         }
244         return ""; //NOI18N
245
}
246     
247     private class JavadocClass extends ClassDocImpl implements ElementHolder {
248         
249         private JavadocClass(DocEnv env, ClassSymbol sym) {
250             this(env, sym, null);
251         }
252         
253         private JavadocClass(DocEnv env, ClassSymbol sym, String JavaDoc docComment) {
254             super(env, sym, docComment, null, null);
255         }
256         
257         @Override JavaDoc
258         protected String JavaDoc documentation() {
259             if (documentation == null) {
260                 setRawCommentText(getRawCommentFor(getElement()));
261             }
262             return documentation;
263         }
264         
265         public Element getElement() {
266             return tsym;
267         }
268     }
269     
270     private class JavadocAnnotation extends AnnotationTypeDocImpl implements ElementHolder {
271         
272         private JavadocAnnotation(DocEnv env, ClassSymbol sym) {
273             this(env, sym, null);
274         }
275         
276         private JavadocAnnotation(DocEnv env, ClassSymbol sym, String JavaDoc docComment) {
277             super(env, sym, docComment, null, null);
278         }
279         
280         @Override JavaDoc
281         protected String JavaDoc documentation() {
282             if (documentation == null) {
283                 setRawCommentText(getRawCommentFor(getElement()));
284             }
285             return documentation;
286         }
287         
288         public Element getElement() {
289             return tsym;
290         }
291     }
292     
293     private class JavadocField extends FieldDocImpl implements ElementHolder {
294
295         private JavadocField(DocEnv env, VarSymbol sym) {
296             this(env, sym, null);
297         }
298         
299         private JavadocField(DocEnv env, VarSymbol sym, String JavaDoc docComment) {
300             super(env, sym, docComment, null, null);
301         }
302
303         @Override JavaDoc
304         protected String JavaDoc documentation() {
305             if (documentation == null) {
306                 setRawCommentText(getRawCommentFor(getElement()));
307             }
308             return documentation;
309         }
310         
311         public Element getElement() {
312             return sym;
313         }
314     }
315
316     private class JavadocMethod extends MethodDocImpl implements ElementHolder {
317
318         private JavadocMethod(DocEnv env, MethodSymbol sym) {
319             this(env, sym, null);
320         }
321         
322         private JavadocMethod(DocEnv env, MethodSymbol sym, String JavaDoc docComment) {
323             super(env, sym, docComment, null, null);
324         }
325
326         @Override JavaDoc
327         protected String JavaDoc documentation() {
328             if (documentation == null) {
329                 setRawCommentText(getRawCommentFor(getElement()));
330             }
331             return documentation;
332         }
333         
334         public Element getElement() {
335             return sym;
336         }
337     }
338         
339     private class JavadocConstructor extends ConstructorDocImpl implements ElementHolder {
340
341         private JavadocConstructor(DocEnv env, MethodSymbol sym) {
342             this(env, sym, null);
343         }
344         
345         private JavadocConstructor(DocEnv env, MethodSymbol sym, String JavaDoc docComment) {
346             super(env, sym, docComment, null, null);
347         }
348
349         @Override JavaDoc
350         protected String JavaDoc documentation() {
351             if (documentation == null) {
352                 setRawCommentText(getRawCommentFor(getElement()));
353             }
354             return documentation;
355         }
356         
357         public Element getElement() {
358             return sym;
359         }
360     }
361         
362     private class JavadocAnnotationTypeElement extends AnnotationTypeElementDocImpl implements ElementHolder {
363
364         private JavadocAnnotationTypeElement(DocEnv env, MethodSymbol sym) {
365             this(env, sym, null);
366         }
367         
368         private JavadocAnnotationTypeElement(DocEnv env, MethodSymbol sym, String JavaDoc docComment) {
369             super(env, sym, docComment, null, null);
370         }
371
372         @Override JavaDoc
373         protected String JavaDoc documentation() {
374             if (documentation == null) {
375                 setRawCommentText(getRawCommentFor(getElement()));
376             }
377             return documentation;
378         }
379         
380         public Element getElement() {
381             return sym;
382         }
383     }
384         
385     private class JavaDocPackage extends PackageDocImpl implements ElementHolder {
386         
387         private JavaDocPackage(DocEnv env, PackageSymbol sym, Context ctx) {
388             super(env, sym);
389         }
390         
391         public ClassDoc findClass(String JavaDoc className) {
392             Name.Table nameTable = Name.Table.instance(ctx);
393             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(className, "."); //NOI18N
394
TypeSymbol s = sym;
395             while(s != null && st.hasMoreTokens()) {
396                 Name clsName = nameTable.fromString(st.nextToken());
397                 com.sun.tools.javac.code.Scope.Entry e = s.members().lookup(clsName);
398                 s = null;
399                 while (e.scope != null) {
400                     if (e.sym.kind == Kinds.TYP && (e.sym.flags_field & Flags.SYNTHETIC) == 0) {
401                         s = (TypeSymbol)e.sym;
402                         break;
403                     }
404                     e = e.next();
405                 }
406             }
407             return s instanceof ClassSymbol ? env.getClassDoc((ClassSymbol)s) : null;
408         }
409
410         public Element getElement() {
411             return sym;
412         }
413     }
414 }
415
Popular Tags