test/tools/javac/doctree/SimpleDocTreeVisitorTest.java

changeset 0
959103a6100f
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
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 /*
25 * @test
26 * @bug 7021614
27 * @summary extend com.sun.source API to support parsing javadoc comments
28 */
29
30 import com.sun.source.doctree.DocCommentTree;
31 import com.sun.source.doctree.DocTree;
32 import com.sun.source.doctree.DocTreeVisitor;
33 import com.sun.source.tree.ClassTree;
34 import com.sun.source.tree.CompilationUnitTree;
35 import com.sun.source.tree.MethodTree;
36 import com.sun.source.tree.Tree;
37 import com.sun.source.tree.VariableTree;
38 import com.sun.source.util.DocTreeScanner;
39 import com.sun.source.util.DocTrees;
40 import com.sun.source.util.JavacTask;
41 import com.sun.source.util.SimpleDocTreeVisitor;
42 import com.sun.source.util.TreePath;
43 import com.sun.source.util.TreePathScanner;
44 import com.sun.tools.javac.api.JavacTool;
45 import java.io.File;
46 import java.util.ArrayList;
47 import java.util.EnumSet;
48 import java.util.List;
49 import java.util.Set;
50 import javax.lang.model.element.Name;
51 import javax.tools.JavaFileObject;
52 import javax.tools.StandardJavaFileManager;
53
54 public class SimpleDocTreeVisitorTest {
55 public static void main(String... args) throws Exception {
56 SimpleDocTreeVisitorTest t = new SimpleDocTreeVisitorTest();
57 t.run();
58 }
59
60 void run() throws Exception {
61 List<File> files = new ArrayList<File>();
62 File testSrc = new File(System.getProperty("test.src"));
63 for (File f: testSrc.listFiles()) {
64 if (f.isFile() && f.getName().endsWith(".java"))
65 files.add(f);
66 }
67
68 JavacTool javac = JavacTool.create();
69 StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
70
71 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
72
73 JavacTask t = javac.getTask(null, fm, null, null, null, fos);
74 DocTrees trees = DocTrees.instance(t);
75
76 Iterable<? extends CompilationUnitTree> units = t.parse();
77
78 Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class);
79 DeclScanner ds = new DeclScanner(trees, found);
80 for (CompilationUnitTree unit: units) {
81 ds.scan(unit, null);
82 }
83
84 for (DocTree.Kind k: DocTree.Kind.values()) {
85 if (!found.contains(k) && k != DocTree.Kind.OTHER)
86 error("not found: " + k);
87 }
88
89 if (errors > 0)
90 throw new Exception(errors + " errors occurred");
91 }
92
93 void error(String msg) {
94 System.err.println("Error: " + msg);
95 errors++;
96 }
97
98 int errors;
99
100 static class DeclScanner extends TreePathScanner<Void, Void> {
101 DocTrees trees;
102 DocTreeScanner<Void,Void> cs;
103
104 DeclScanner(DocTrees trees, final Set<DocTree.Kind> found) {
105 this.trees = trees;
106 cs = new CommentScanner(found);
107 }
108
109 @Override
110 public Void visitClass(ClassTree tree, Void ignore) {
111 super.visitClass(tree, ignore);
112 visitDecl(tree, tree.getSimpleName());
113 return null;
114 }
115
116 @Override
117 public Void visitMethod(MethodTree tree, Void ignore) {
118 super.visitMethod(tree, ignore);
119 visitDecl(tree, tree.getName());
120 return null;
121 }
122
123 @Override
124 public Void visitVariable(VariableTree tree, Void ignore) {
125 super.visitVariable(tree, ignore);
126 visitDecl(tree, tree.getName());
127 return null;
128 }
129
130 void visitDecl(Tree tree, Name name) {
131 TreePath path = getCurrentPath();
132 DocCommentTree dc = trees.getDocCommentTree(path);
133 if (dc != null)
134 cs.scan(dc, null);
135 }
136 }
137
138 static class CommentScanner extends DocTreeScanner<Void, Void> {
139 DocTreeVisitor<Void, Void> visitor;
140
141 CommentScanner(Set<DocTree.Kind> found) {
142 visitor = new Visitor(found);
143 }
144
145 @Override
146 public Void scan(DocTree tree, Void ignore) {
147 if (tree != null)
148 tree.accept(visitor, ignore);
149 return super.scan(tree, ignore);
150 }
151 }
152
153 static class Visitor extends SimpleDocTreeVisitor<Void, Void> {
154 Set<DocTree.Kind> found;
155
156 Visitor(Set<DocTree.Kind> found) {
157 this.found = found;
158 }
159
160 @Override
161 public Void defaultAction(DocTree tree, Void ignore) {
162 found.add(tree.getKind());
163 return null;
164 }
165 }
166 }

mercurial