Fri, 24 Apr 2020 03:58:51 +0100
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 8009724 |
aoqi@0 | 27 | * @summary adding DocTreePath and DocTreePathScanner |
aoqi@0 | 28 | */ |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.source.doctree.DocCommentTree; |
aoqi@0 | 31 | import com.sun.source.doctree.DocTree; |
aoqi@0 | 32 | import com.sun.source.doctree.DocTree.Kind; |
aoqi@0 | 33 | import com.sun.source.doctree.DocTreeVisitor; |
aoqi@0 | 34 | import com.sun.source.tree.ClassTree; |
aoqi@0 | 35 | import com.sun.source.tree.CompilationUnitTree; |
aoqi@0 | 36 | import com.sun.source.tree.MethodTree; |
aoqi@0 | 37 | import com.sun.source.tree.Tree; |
aoqi@0 | 38 | import com.sun.source.tree.VariableTree; |
aoqi@0 | 39 | import com.sun.source.util.DocTreePath; |
aoqi@0 | 40 | import com.sun.source.util.DocTreePathScanner; |
aoqi@0 | 41 | import com.sun.source.util.DocTreeScanner; |
aoqi@0 | 42 | import com.sun.source.util.DocTrees; |
aoqi@0 | 43 | import com.sun.source.util.JavacTask; |
aoqi@0 | 44 | import com.sun.source.util.TreePath; |
aoqi@0 | 45 | import com.sun.source.util.TreePathScanner; |
aoqi@0 | 46 | import com.sun.tools.javac.api.JavacTool; |
aoqi@0 | 47 | import java.io.File; |
aoqi@0 | 48 | import java.util.ArrayList; |
aoqi@0 | 49 | import java.util.List; |
aoqi@0 | 50 | import javax.lang.model.element.Name; |
aoqi@0 | 51 | import javax.tools.JavaFileObject; |
aoqi@0 | 52 | import javax.tools.StandardJavaFileManager; |
aoqi@0 | 53 | |
aoqi@0 | 54 | public class DocTreePathScannerTest { |
aoqi@0 | 55 | public static void main(String... args) throws Exception { |
aoqi@0 | 56 | DocTreePathScannerTest t = new DocTreePathScannerTest(); |
aoqi@0 | 57 | t.run(); |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | void run() throws Exception { |
aoqi@0 | 61 | List<File> files = new ArrayList<File>(); |
aoqi@0 | 62 | File testSrc = new File(System.getProperty("test.src")); |
aoqi@0 | 63 | for (File f: testSrc.listFiles()) { |
aoqi@0 | 64 | if (f.isFile() && f.getName().endsWith(".java")) |
aoqi@0 | 65 | files.add(f); |
aoqi@0 | 66 | } |
aoqi@0 | 67 | |
aoqi@0 | 68 | JavacTool javac = JavacTool.create(); |
aoqi@0 | 69 | StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); |
aoqi@0 | 70 | |
aoqi@0 | 71 | Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files); |
aoqi@0 | 72 | |
aoqi@0 | 73 | JavacTask t = javac.getTask(null, fm, null, null, null, fos); |
aoqi@0 | 74 | DocTrees trees = DocTrees.instance(t); |
aoqi@0 | 75 | |
aoqi@0 | 76 | Iterable<? extends CompilationUnitTree> units = t.parse(); |
aoqi@0 | 77 | |
aoqi@0 | 78 | DeclScanner ds = new DeclScanner(trees); |
aoqi@0 | 79 | for (CompilationUnitTree unit: units) { |
aoqi@0 | 80 | ds.scan(unit, null); |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | if (errors > 0) |
aoqi@0 | 84 | throw new Exception(errors + " errors occurred"); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | void error(String msg) { |
aoqi@0 | 88 | System.err.println("Error: " + msg); |
aoqi@0 | 89 | errors++; |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | int errors; |
aoqi@0 | 93 | |
aoqi@0 | 94 | class DeclScanner extends TreePathScanner<Void, Void> { |
aoqi@0 | 95 | DocTrees trees; |
aoqi@0 | 96 | DocTreePathScanner<Void,Void> cs; |
aoqi@0 | 97 | |
aoqi@0 | 98 | DeclScanner(DocTrees trees) { |
aoqi@0 | 99 | this.trees = trees; |
aoqi@0 | 100 | cs = new CommentPathScanner(); |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | @Override |
aoqi@0 | 104 | public Void visitClass(ClassTree tree, Void ignore) { |
aoqi@0 | 105 | super.visitClass(tree, ignore); |
aoqi@0 | 106 | visitDecl(tree, tree.getSimpleName()); |
aoqi@0 | 107 | return null; |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | @Override |
aoqi@0 | 111 | public Void visitMethod(MethodTree tree, Void ignore) { |
aoqi@0 | 112 | super.visitMethod(tree, ignore); |
aoqi@0 | 113 | visitDecl(tree, tree.getName()); |
aoqi@0 | 114 | return null; |
aoqi@0 | 115 | } |
aoqi@0 | 116 | |
aoqi@0 | 117 | @Override |
aoqi@0 | 118 | public Void visitVariable(VariableTree tree, Void ignore) { |
aoqi@0 | 119 | super.visitVariable(tree, ignore); |
aoqi@0 | 120 | visitDecl(tree, tree.getName()); |
aoqi@0 | 121 | return null; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | |
aoqi@0 | 124 | void visitDecl(Tree tree, Name name) { |
aoqi@0 | 125 | TreePath path = getCurrentPath(); |
aoqi@0 | 126 | DocCommentTree dc = trees.getDocCommentTree(path); |
aoqi@0 | 127 | if (dc != null) |
aoqi@0 | 128 | cs.scan(new DocTreePath(path, dc), null); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | class CommentPathScanner extends DocTreePathScanner<Void, Void> { |
aoqi@0 | 133 | CommentPathScanner() {} |
aoqi@0 | 134 | |
aoqi@0 | 135 | @Override |
aoqi@0 | 136 | public Void scan(final DocTree tree, Void ignore) { |
aoqi@0 | 137 | if (tree != null) { |
aoqi@0 | 138 | DocTree previous = null; |
aoqi@0 | 139 | for (DocTree current : getCurrentPath()) { |
aoqi@0 | 140 | if (previous != null) { |
aoqi@0 | 141 | final List<DocTree> children = new ArrayList<>(); |
aoqi@0 | 142 | current.accept(new DocTreeScanner<Void, Void>() { |
aoqi@0 | 143 | @Override public Void scan(DocTree node, Void p) { |
aoqi@0 | 144 | children.add(node); |
aoqi@0 | 145 | return null; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | }, null); |
aoqi@0 | 148 | |
aoqi@0 | 149 | if (!children.contains(previous)) { |
aoqi@0 | 150 | error("Invalid DocTreePath for: " + tree); |
aoqi@0 | 151 | } |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | previous = current; |
aoqi@0 | 155 | } |
aoqi@0 | 156 | } |
aoqi@0 | 157 | return super.scan(tree, ignore); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | } |