Fri, 24 Apr 2020 03:58:51 +0100
Merge
1 /*
2 * Copyright (c) 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 */
24 /*
25 * @test
26 * @bug 8009724
27 * @summary adding DocTreePath and DocTreePathScanner
28 */
30 import com.sun.source.doctree.DocCommentTree;
31 import com.sun.source.doctree.DocTree;
32 import com.sun.source.doctree.DocTree.Kind;
33 import com.sun.source.doctree.DocTreeVisitor;
34 import com.sun.source.tree.ClassTree;
35 import com.sun.source.tree.CompilationUnitTree;
36 import com.sun.source.tree.MethodTree;
37 import com.sun.source.tree.Tree;
38 import com.sun.source.tree.VariableTree;
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.JavacTask;
44 import com.sun.source.util.TreePath;
45 import com.sun.source.util.TreePathScanner;
46 import com.sun.tools.javac.api.JavacTool;
47 import java.io.File;
48 import java.util.ArrayList;
49 import java.util.List;
50 import javax.lang.model.element.Name;
51 import javax.tools.JavaFileObject;
52 import javax.tools.StandardJavaFileManager;
54 public class DocTreePathScannerTest {
55 public static void main(String... args) throws Exception {
56 DocTreePathScannerTest t = new DocTreePathScannerTest();
57 t.run();
58 }
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 }
68 JavacTool javac = JavacTool.create();
69 StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
71 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
73 JavacTask t = javac.getTask(null, fm, null, null, null, fos);
74 DocTrees trees = DocTrees.instance(t);
76 Iterable<? extends CompilationUnitTree> units = t.parse();
78 DeclScanner ds = new DeclScanner(trees);
79 for (CompilationUnitTree unit: units) {
80 ds.scan(unit, null);
81 }
83 if (errors > 0)
84 throw new Exception(errors + " errors occurred");
85 }
87 void error(String msg) {
88 System.err.println("Error: " + msg);
89 errors++;
90 }
92 int errors;
94 class DeclScanner extends TreePathScanner<Void, Void> {
95 DocTrees trees;
96 DocTreePathScanner<Void,Void> cs;
98 DeclScanner(DocTrees trees) {
99 this.trees = trees;
100 cs = new CommentPathScanner();
101 }
103 @Override
104 public Void visitClass(ClassTree tree, Void ignore) {
105 super.visitClass(tree, ignore);
106 visitDecl(tree, tree.getSimpleName());
107 return null;
108 }
110 @Override
111 public Void visitMethod(MethodTree tree, Void ignore) {
112 super.visitMethod(tree, ignore);
113 visitDecl(tree, tree.getName());
114 return null;
115 }
117 @Override
118 public Void visitVariable(VariableTree tree, Void ignore) {
119 super.visitVariable(tree, ignore);
120 visitDecl(tree, tree.getName());
121 return null;
122 }
124 void visitDecl(Tree tree, Name name) {
125 TreePath path = getCurrentPath();
126 DocCommentTree dc = trees.getDocCommentTree(path);
127 if (dc != null)
128 cs.scan(new DocTreePath(path, dc), null);
129 }
130 }
132 class CommentPathScanner extends DocTreePathScanner<Void, Void> {
133 CommentPathScanner() {}
135 @Override
136 public Void scan(final DocTree tree, Void ignore) {
137 if (tree != null) {
138 DocTree previous = null;
139 for (DocTree current : getCurrentPath()) {
140 if (previous != null) {
141 final List<DocTree> children = new ArrayList<>();
142 current.accept(new DocTreeScanner<Void, Void>() {
143 @Override public Void scan(DocTree node, Void p) {
144 children.add(node);
145 return null;
146 }
147 }, null);
149 if (!children.contains(previous)) {
150 error("Invalid DocTreePath for: " + tree);
151 }
152 }
154 previous = current;
155 }
156 }
157 return super.scan(tree, ignore);
158 }
159 }
161 }