|
1 /* |
|
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import java.util.*; |
|
25 |
|
26 import javax.annotation.processing.*; |
|
27 import javax.lang.model.*; |
|
28 import javax.lang.model.element.*; |
|
29 |
|
30 import com.sun.source.doctree.DocCommentTree; |
|
31 import com.sun.source.doctree.ErroneousTree; |
|
32 import com.sun.source.tree.ClassTree; |
|
33 import com.sun.source.tree.MethodTree; |
|
34 import com.sun.source.tree.VariableTree; |
|
35 import com.sun.source.util.DocTreeScanner; |
|
36 import com.sun.source.util.DocTrees; |
|
37 import com.sun.source.util.TreePath; |
|
38 import com.sun.source.util.TreePathScanner; |
|
39 import com.sun.source.util.TreeScanner; |
|
40 import com.sun.tools.javac.tree.DocPretty; |
|
41 import java.io.PrintWriter; |
|
42 import javax.tools.Diagnostic; |
|
43 |
|
44 /** |
|
45 * Standard annotation processor for use by examples to |
|
46 * scan DocCommentTree nodes looking for ErroneousTree, |
|
47 * on which to call {@code getMessage}. |
|
48 */ |
|
49 @SupportedAnnotationTypes("*") |
|
50 public class DocCommentProcessor extends AbstractProcessor { |
|
51 @Override |
|
52 public SourceVersion getSupportedSourceVersion() { |
|
53 return SourceVersion.latest(); |
|
54 } |
|
55 |
|
56 @Override |
|
57 public void init(ProcessingEnvironment pEnv) { |
|
58 super.init(pEnv); |
|
59 trees = DocTrees.instance(pEnv); |
|
60 messager = pEnv.getMessager(); |
|
61 } |
|
62 |
|
63 @Override |
|
64 public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { |
|
65 for (Element e : rEnv.getRootElements()) { |
|
66 new DocCommentScanner().scan(e); |
|
67 } |
|
68 return true; |
|
69 } |
|
70 |
|
71 class DocCommentScanner extends TreePathScanner<Void,Void> { |
|
72 public void scan(Element e) { |
|
73 scan(trees.getPath(e), null); |
|
74 } |
|
75 |
|
76 @Override |
|
77 public Void visitClass(ClassTree tree, Void ignore) { |
|
78 check(); |
|
79 return super.visitClass(tree, ignore); |
|
80 } |
|
81 |
|
82 @Override |
|
83 public Void visitMethod(MethodTree tree, Void ignore) { |
|
84 check(); |
|
85 return super.visitMethod(tree, ignore); |
|
86 } |
|
87 |
|
88 @Override |
|
89 public Void visitVariable(VariableTree tree, Void ignore) { |
|
90 check(); |
|
91 return super.visitVariable(tree, ignore); |
|
92 } |
|
93 |
|
94 private void check() { |
|
95 DocCommentTree dc = trees.getDocCommentTree(getCurrentPath()); |
|
96 if (dc == null) |
|
97 return; |
|
98 |
|
99 DocTreeScanner<Void, Void> s = new DocTreeScanner<Void, Void>() { |
|
100 @Override |
|
101 public Void visitErroneous(ErroneousTree tree, Void ignore) { |
|
102 messager.printMessage(Diagnostic.Kind.NOTE, tree.getDiagnostic().getMessage(null)); |
|
103 return null; |
|
104 } |
|
105 }; |
|
106 |
|
107 s.scan(dc, null); |
|
108 } |
|
109 |
|
110 } |
|
111 |
|
112 private DocTrees trees; |
|
113 private Messager messager; |
|
114 } |