Thu, 10 Oct 2013 14:58:04 +0200
8001931: The new build system whitespace cleanup
Reviewed-by: tbell, simonis, erikj
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 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 7021614 |
aoqi@0 | 27 | * @summary extend com.sun.source API to support parsing javadoc comments |
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.DocTreeVisitor; |
aoqi@0 | 33 | import com.sun.source.tree.ClassTree; |
aoqi@0 | 34 | import com.sun.source.tree.CompilationUnitTree; |
aoqi@0 | 35 | import com.sun.source.tree.MethodTree; |
aoqi@0 | 36 | import com.sun.source.tree.Tree; |
aoqi@0 | 37 | import com.sun.source.tree.VariableTree; |
aoqi@0 | 38 | import com.sun.source.util.DocTreeScanner; |
aoqi@0 | 39 | import com.sun.source.util.DocTrees; |
aoqi@0 | 40 | import com.sun.source.util.JavacTask; |
aoqi@0 | 41 | import com.sun.source.util.SimpleDocTreeVisitor; |
aoqi@0 | 42 | import com.sun.source.util.TreePath; |
aoqi@0 | 43 | import com.sun.source.util.TreePathScanner; |
aoqi@0 | 44 | import com.sun.tools.javac.api.JavacTool; |
aoqi@0 | 45 | import java.io.File; |
aoqi@0 | 46 | import java.util.ArrayList; |
aoqi@0 | 47 | import java.util.EnumSet; |
aoqi@0 | 48 | import java.util.List; |
aoqi@0 | 49 | import java.util.Set; |
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 SimpleDocTreeVisitorTest { |
aoqi@0 | 55 | public static void main(String... args) throws Exception { |
aoqi@0 | 56 | SimpleDocTreeVisitorTest t = new SimpleDocTreeVisitorTest(); |
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 | Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class); |
aoqi@0 | 79 | DeclScanner ds = new DeclScanner(trees, found); |
aoqi@0 | 80 | for (CompilationUnitTree unit: units) { |
aoqi@0 | 81 | ds.scan(unit, null); |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | for (DocTree.Kind k: DocTree.Kind.values()) { |
aoqi@0 | 85 | if (!found.contains(k) && k != DocTree.Kind.OTHER) |
aoqi@0 | 86 | error("not found: " + k); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | if (errors > 0) |
aoqi@0 | 90 | throw new Exception(errors + " errors occurred"); |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | void error(String msg) { |
aoqi@0 | 94 | System.err.println("Error: " + msg); |
aoqi@0 | 95 | errors++; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | int errors; |
aoqi@0 | 99 | |
aoqi@0 | 100 | static class DeclScanner extends TreePathScanner<Void, Void> { |
aoqi@0 | 101 | DocTrees trees; |
aoqi@0 | 102 | DocTreeScanner<Void,Void> cs; |
aoqi@0 | 103 | |
aoqi@0 | 104 | DeclScanner(DocTrees trees, final Set<DocTree.Kind> found) { |
aoqi@0 | 105 | this.trees = trees; |
aoqi@0 | 106 | cs = new CommentScanner(found); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | @Override |
aoqi@0 | 110 | public Void visitClass(ClassTree tree, Void ignore) { |
aoqi@0 | 111 | super.visitClass(tree, ignore); |
aoqi@0 | 112 | visitDecl(tree, tree.getSimpleName()); |
aoqi@0 | 113 | return null; |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | @Override |
aoqi@0 | 117 | public Void visitMethod(MethodTree tree, Void ignore) { |
aoqi@0 | 118 | super.visitMethod(tree, ignore); |
aoqi@0 | 119 | visitDecl(tree, tree.getName()); |
aoqi@0 | 120 | return null; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | @Override |
aoqi@0 | 124 | public Void visitVariable(VariableTree tree, Void ignore) { |
aoqi@0 | 125 | super.visitVariable(tree, ignore); |
aoqi@0 | 126 | visitDecl(tree, tree.getName()); |
aoqi@0 | 127 | return null; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | void visitDecl(Tree tree, Name name) { |
aoqi@0 | 131 | TreePath path = getCurrentPath(); |
aoqi@0 | 132 | DocCommentTree dc = trees.getDocCommentTree(path); |
aoqi@0 | 133 | if (dc != null) |
aoqi@0 | 134 | cs.scan(dc, null); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | static class CommentScanner extends DocTreeScanner<Void, Void> { |
aoqi@0 | 139 | DocTreeVisitor<Void, Void> visitor; |
aoqi@0 | 140 | |
aoqi@0 | 141 | CommentScanner(Set<DocTree.Kind> found) { |
aoqi@0 | 142 | visitor = new Visitor(found); |
aoqi@0 | 143 | } |
aoqi@0 | 144 | |
aoqi@0 | 145 | @Override |
aoqi@0 | 146 | public Void scan(DocTree tree, Void ignore) { |
aoqi@0 | 147 | if (tree != null) |
aoqi@0 | 148 | tree.accept(visitor, ignore); |
aoqi@0 | 149 | return super.scan(tree, ignore); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | static class Visitor extends SimpleDocTreeVisitor<Void, Void> { |
aoqi@0 | 154 | Set<DocTree.Kind> found; |
aoqi@0 | 155 | |
aoqi@0 | 156 | Visitor(Set<DocTree.Kind> found) { |
aoqi@0 | 157 | this.found = found; |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | @Override |
aoqi@0 | 161 | public Void defaultAction(DocTree tree, Void ignore) { |
aoqi@0 | 162 | found.add(tree.getKind()); |
aoqi@0 | 163 | return null; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | } |
aoqi@0 | 166 | } |