test/tools/javac/doctree/ReferenceTest.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2012, 2013, 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 /*
25 * @test
26 * @bug 7021614
27 * @summary extend com.sun.source API to support parsing javadoc comments
28 * @summary check references in at-see and {at-link} tags
29 * @build ReferenceTest
30 * @compile -processor ReferenceTest -proc:only ReferenceTest.java
31 */
32
33 import com.sun.source.doctree.DocCommentTree;
34 import com.sun.source.doctree.DocTree;
35 import com.sun.source.doctree.LinkTree;
36 import com.sun.source.doctree.ReferenceTree;
37 import com.sun.source.doctree.SeeTree;
38 import com.sun.source.doctree.TextTree;
39 import com.sun.source.util.DocTreePath;
40 import com.sun.source.util.DocTreePathScanner;
41 import com.sun.source.util.DocTreeScanner;
42 import com.sun.source.util.DocTrees;
43 import com.sun.source.util.TreePath;
44
45 import java.util.List;
46 import java.util.Set;
47 import javax.annotation.processing.AbstractProcessor;
48 import javax.annotation.processing.ProcessingEnvironment;
49 import javax.annotation.processing.RoundEnvironment;
50 import javax.annotation.processing.SupportedAnnotationTypes;
51 import javax.lang.model.SourceVersion;
52 import javax.lang.model.element.Element;
53 import javax.lang.model.element.TypeElement;
54 import javax.tools.Diagnostic.Kind;
55
56 /**
57 * {@link java.lang Package}
58 * {@link java.lang.ERROR Bad}
59 *
60 * {@link java.lang.String Class}
61 * {@link String Class}
62 * {@link java.lang.String#CASE_INSENSITIVE_ORDER Field}
63 * {@link java.lang.String#String Constructor}
64 * {@link java.lang.String#String(byte[]) Constructor}
65 * {@link java.lang.String#String(byte[] bytes) Constructor}
66 * {@link java.lang.String#String(byte[], String) Constructor}
67 * {@link java.lang.String#String(byte[] bytes, String charSetName) Constructor}
68 * {@link java.lang.String#isEmpty Method}
69 * {@link java.lang.String#isEmpty() Method}
70 * {@link java.lang.String#ERROR Bad}
71 * {@link java.lang.String#equals(Object) Method}
72 *
73 * {@link AbstractProcessor Class}
74 *
75 * {@link List#add(Object) Method}
76 *
77 * {@link #trees Field}
78 * {@link #getSupportedSourceVersion Method}
79 * {@link #init(ProcessingEnvironment Method}
80 *
81 * @see java.lang Package
82 * @see java.lang.ERROR Bad
83 *
84 * @see java.lang.String Class
85 * @see String Class
86 * @see java.lang.String#CASE_INSENSITIVE_ORDER Field
87 * @see java.lang.String#String Constructor
88 * @see java.lang.String#String(byte[]) Constructor
89 * @see java.lang.String#String(byte[] bytes) Constructor
90 * @see java.lang.String#String(byte[],String) Constructor
91 * @see java.lang.String#String(byte[] bytes, String charsetName) Constructor
92 * @see java.lang.String#isEmpty Method
93 * @see java.lang.String#isEmpty() Method
94 * @see java.lang.String#ERROR Bad
95 * @see java.lang.String#equals(Object) Method
96 *
97 * @see AbstractProcessor Class
98 *
99 * @see List#add(Object) Method
100 *
101 * @see #trees Field
102 * @see #getSupportedSourceVersion Method
103 * @see #init(ProcessingEnvironment) Method
104 *
105 * @see java.io.BufferedInputStream#BufferedInputStream(InputStream) Constructor
106 */
107 @SupportedAnnotationTypes("*")
108 public class ReferenceTest extends AbstractProcessor {
109 DocTrees trees;
110
111 @Override
112 public SourceVersion getSupportedSourceVersion() {
113 return SourceVersion.latest();
114 }
115
116 @Override
117 public void init(ProcessingEnvironment pEnv) {
118 super.init(pEnv);
119 trees = DocTrees.instance(pEnv);
120 }
121
122 @Override
123 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
124 for (Element e: roundEnv.getRootElements()) {
125 new DocCommentScanner(trees.getPath(e)).scan();
126 }
127 return true;
128 }
129
130 class DocCommentScanner extends DocTreePathScanner<Void, Void> {
131 TreePath path;
132 DocCommentTree dc;
133
134 DocCommentScanner(TreePath path) {
135 this.path = path;
136 }
137
138 void scan() {
139 dc = trees.getDocCommentTree(path);
140 scan(new DocTreePath(path, dc), null);
141 }
142
143 @Override
144 public Void visitLink(LinkTree tree, Void ignore) {
145 checkReference(tree.getReference(), tree.getLabel());
146 return null;
147 }
148
149 @Override
150 public Void visitSee(SeeTree tree, Void ignore) {
151 List<? extends DocTree> refLabel = tree.getReference();
152 if (refLabel.size() > 1 && (refLabel.get(0) instanceof ReferenceTree)) {
153 ReferenceTree ref = (ReferenceTree) refLabel.get(0);
154 List<? extends DocTree> label = refLabel.subList(1, refLabel.size());
155 checkReference(ref, label);
156 }
157 return null;
158 }
159
160 void checkReference(ReferenceTree tree, List<? extends DocTree> label) {
161 String sig = tree.getSignature();
162
163 Element found = trees.getElement(new DocTreePath(getCurrentPath(), tree));
164 if (found == null) {
165 System.err.println(sig + " NOT FOUND");
166 } else {
167 System.err.println(sig + " found " + found.getKind() + " " + found);
168 }
169
170 String expect = "UNKNOWN";
171 if (label.size() > 0 && label.get(0) instanceof TextTree)
172 expect = ((TextTree) label.get(0)).getBody();
173
174 if (!expect.equalsIgnoreCase(found == null ? "bad" : found.getKind().name())) {
175 error(tree, "Unexpected value found: " + found +", expected: " + expect);
176 }
177 }
178
179 void error(DocTree tree, String msg) {
180 trees.printMessage(Kind.ERROR, msg, tree, dc, path.getCompilationUnit());
181 }
182 }
183 }
184
185 /**
186 * @see ReferenceTestExtras Class
187 * @see #ReferenceTestExtras Field
188 * @see #ReferenceTestExtras() Constructor
189 *
190 * @see #X Field
191 * @see #X() Method
192 *
193 * @see #m Method
194 *
195 * @see #varargs(int...) Method
196 * @see #varargs(int... args) Method
197 * @see #varargs(int[]) Method
198 * @see #varargs(int[] args) Method
199 */
200 class ReferenceTestExtras {
201 int ReferenceTestExtras; // field
202 ReferenceTestExtras() { } // constructor
203 void ReferenceTestExtras() { } // method
204
205 int X;
206 void X() { }
207 static class X { }
208
209 void m() { }
210 void m(int i) { }
211 void m(int i, int j) { }
212
213 void varargs(int... args) { }
214 }
215
216

mercurial