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