test/tools/javac/doctree/DocTreePathScannerTest.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/doctree/DocTreePathScannerTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,161 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8009724
    1.30 + * @summary adding DocTreePath and DocTreePathScanner
    1.31 + */
    1.32 +
    1.33 +import com.sun.source.doctree.DocCommentTree;
    1.34 +import com.sun.source.doctree.DocTree;
    1.35 +import com.sun.source.doctree.DocTree.Kind;
    1.36 +import com.sun.source.doctree.DocTreeVisitor;
    1.37 +import com.sun.source.tree.ClassTree;
    1.38 +import com.sun.source.tree.CompilationUnitTree;
    1.39 +import com.sun.source.tree.MethodTree;
    1.40 +import com.sun.source.tree.Tree;
    1.41 +import com.sun.source.tree.VariableTree;
    1.42 +import com.sun.source.util.DocTreePath;
    1.43 +import com.sun.source.util.DocTreePathScanner;
    1.44 +import com.sun.source.util.DocTreeScanner;
    1.45 +import com.sun.source.util.DocTrees;
    1.46 +import com.sun.source.util.JavacTask;
    1.47 +import com.sun.source.util.TreePath;
    1.48 +import com.sun.source.util.TreePathScanner;
    1.49 +import com.sun.tools.javac.api.JavacTool;
    1.50 +import java.io.File;
    1.51 +import java.util.ArrayList;
    1.52 +import java.util.List;
    1.53 +import javax.lang.model.element.Name;
    1.54 +import javax.tools.JavaFileObject;
    1.55 +import javax.tools.StandardJavaFileManager;
    1.56 +
    1.57 +public class DocTreePathScannerTest {
    1.58 +    public static void main(String... args) throws Exception {
    1.59 +        DocTreePathScannerTest t = new DocTreePathScannerTest();
    1.60 +        t.run();
    1.61 +    }
    1.62 +
    1.63 +    void run() throws Exception {
    1.64 +        List<File> files = new ArrayList<File>();
    1.65 +        File testSrc = new File(System.getProperty("test.src"));
    1.66 +        for (File f: testSrc.listFiles()) {
    1.67 +            if (f.isFile() && f.getName().endsWith(".java"))
    1.68 +                files.add(f);
    1.69 +        }
    1.70 +
    1.71 +        JavacTool javac = JavacTool.create();
    1.72 +        StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
    1.73 +
    1.74 +        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
    1.75 +
    1.76 +        JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    1.77 +        DocTrees trees = DocTrees.instance(t);
    1.78 +
    1.79 +        Iterable<? extends CompilationUnitTree> units = t.parse();
    1.80 +
    1.81 +        DeclScanner ds = new DeclScanner(trees);
    1.82 +        for (CompilationUnitTree unit: units) {
    1.83 +            ds.scan(unit, null);
    1.84 +        }
    1.85 +
    1.86 +        if (errors > 0)
    1.87 +            throw new Exception(errors + " errors occurred");
    1.88 +    }
    1.89 +
    1.90 +    void error(String msg) {
    1.91 +        System.err.println("Error: " + msg);
    1.92 +        errors++;
    1.93 +    }
    1.94 +
    1.95 +    int errors;
    1.96 +
    1.97 +    class DeclScanner extends TreePathScanner<Void, Void> {
    1.98 +        DocTrees trees;
    1.99 +        DocTreePathScanner<Void,Void> cs;
   1.100 +
   1.101 +        DeclScanner(DocTrees trees) {
   1.102 +            this.trees = trees;
   1.103 +            cs = new CommentPathScanner();
   1.104 +        }
   1.105 +
   1.106 +        @Override
   1.107 +        public Void visitClass(ClassTree tree, Void ignore) {
   1.108 +            super.visitClass(tree, ignore);
   1.109 +            visitDecl(tree, tree.getSimpleName());
   1.110 +            return null;
   1.111 +        }
   1.112 +
   1.113 +        @Override
   1.114 +        public Void visitMethod(MethodTree tree, Void ignore) {
   1.115 +            super.visitMethod(tree, ignore);
   1.116 +            visitDecl(tree, tree.getName());
   1.117 +            return null;
   1.118 +        }
   1.119 +
   1.120 +        @Override
   1.121 +        public Void visitVariable(VariableTree tree, Void ignore) {
   1.122 +            super.visitVariable(tree, ignore);
   1.123 +            visitDecl(tree, tree.getName());
   1.124 +            return null;
   1.125 +        }
   1.126 +
   1.127 +        void visitDecl(Tree tree, Name name) {
   1.128 +            TreePath path = getCurrentPath();
   1.129 +            DocCommentTree dc = trees.getDocCommentTree(path);
   1.130 +            if (dc != null)
   1.131 +                cs.scan(new DocTreePath(path, dc), null);
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    class CommentPathScanner extends DocTreePathScanner<Void, Void> {
   1.136 +        CommentPathScanner() {}
   1.137 +
   1.138 +        @Override
   1.139 +        public Void scan(final DocTree tree, Void ignore) {
   1.140 +            if (tree != null) {
   1.141 +                DocTree previous = null;
   1.142 +                for (DocTree current : getCurrentPath()) {
   1.143 +                    if (previous != null) {
   1.144 +                        final List<DocTree> children = new ArrayList<>();
   1.145 +                        current.accept(new DocTreeScanner<Void, Void>() {
   1.146 +                            @Override public Void scan(DocTree node, Void p) {
   1.147 +                                children.add(node);
   1.148 +                                return null;
   1.149 +                            }
   1.150 +                        }, null);
   1.151 +
   1.152 +                        if (!children.contains(previous)) {
   1.153 +                            error("Invalid DocTreePath for: " + tree);
   1.154 +                        }
   1.155 +                    }
   1.156 +
   1.157 +                    previous = current;
   1.158 +                }
   1.159 +            }
   1.160 +            return super.scan(tree, ignore);
   1.161 +        }
   1.162 +    }
   1.163 +
   1.164 +}

mercurial