test/tools/javac/api/8007344/Test.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/8007344/Test.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,229 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 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 8007344
    1.30 + * @summary javac may not make tree end positions and/or doc comments
    1.31 + *          available to processors and listeners
    1.32 + * @library /tools/javac/lib
    1.33 + * @build JavacTestingAbstractProcessor
    1.34 + * @run main Test
    1.35 + */
    1.36 +
    1.37 +import java.io.File;
    1.38 +import java.io.PrintWriter;
    1.39 +import java.util.Arrays;
    1.40 +import java.util.Set;
    1.41 +
    1.42 +import javax.annotation.processing.RoundEnvironment;
    1.43 +import javax.lang.model.element.Element;
    1.44 +import javax.lang.model.element.TypeElement;
    1.45 +import javax.tools.JavaFileObject;
    1.46 +import javax.tools.StandardJavaFileManager;
    1.47 +import javax.tools.StandardLocation;
    1.48 +
    1.49 +import com.sun.source.doctree.DocCommentTree;
    1.50 +import com.sun.source.tree.*;
    1.51 +import com.sun.source.util.DocTrees;
    1.52 +import com.sun.source.util.JavacTask;
    1.53 +import com.sun.source.util.SourcePositions;
    1.54 +import com.sun.source.util.TaskEvent;
    1.55 +import com.sun.source.util.TaskListener;
    1.56 +import com.sun.source.util.TreePath;
    1.57 +import com.sun.source.util.TreePathScanner;
    1.58 +import com.sun.tools.javac.api.JavacTool;
    1.59 +import com.sun.tools.javac.tree.JCTree;
    1.60 +import com.sun.tools.javac.tree.Pretty;
    1.61 +import com.sun.tools.javac.util.Position;
    1.62 +
    1.63 +/** Doc comment: Test */
    1.64 +public class Test {
    1.65 +    public static final int EXPECT_DOC_COMMENTS = 3;
    1.66 +
    1.67 +    /** Doc comment: main */
    1.68 +    public static void main(String... args) throws Exception {
    1.69 +        PrintWriter out = new PrintWriter(System.err);
    1.70 +        try {
    1.71 +            new Test(out).run();
    1.72 +        } finally {
    1.73 +            out.flush();
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    PrintWriter out;
    1.78 +    int errors;
    1.79 +
    1.80 +    Test(PrintWriter out) {
    1.81 +        this.out = out;
    1.82 +    }
    1.83 +
    1.84 +    /** Doc comment: run */
    1.85 +    void run() throws Exception {
    1.86 +        File testSrc = new File(System.getProperty("test.src"));
    1.87 +        File thisFile = new File(testSrc, getClass().getName() + ".java");
    1.88 +        JavacTool javac = JavacTool.create();
    1.89 +        StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
    1.90 +        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
    1.91 +        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile);
    1.92 +        testAnnoProcessor(javac, fm, fos, out, EXPECT_DOC_COMMENTS);
    1.93 +        testTaskListener(javac, fm, fos, out, EXPECT_DOC_COMMENTS);
    1.94 +
    1.95 +        if (errors > 0)
    1.96 +            throw new Exception(errors + " errors occurred");
    1.97 +    }
    1.98 +
    1.99 +    void testAnnoProcessor(JavacTool javac, StandardJavaFileManager fm,
   1.100 +            Iterable<? extends JavaFileObject> files, PrintWriter out,
   1.101 +            int expectedDocComments) {
   1.102 +        out.println("Test annotation processor");
   1.103 +        JavacTask task = javac.getTask(out, fm, null, null, null, files);
   1.104 +        AnnoProc ap = new AnnoProc(DocTrees.instance(task));
   1.105 +        task.setProcessors(Arrays.asList(ap));
   1.106 +        task.call();
   1.107 +        ap.checker.checkDocComments(expectedDocComments);
   1.108 +    }
   1.109 +
   1.110 +    void testTaskListener(JavacTool javac, StandardJavaFileManager fm,
   1.111 +            Iterable<? extends JavaFileObject> files, PrintWriter out,
   1.112 +            int expectedDocComments) {
   1.113 +        out.println("Test task listener");
   1.114 +        JavacTask task = javac.getTask(out, fm, null, null, null, files);
   1.115 +        TaskListnr tl = new TaskListnr(DocTrees.instance(task));
   1.116 +        task.addTaskListener(tl);
   1.117 +        task.call();
   1.118 +        tl.checker.checkDocComments(expectedDocComments);
   1.119 +    }
   1.120 +
   1.121 +    void error(String msg) {
   1.122 +        out.println("Error: " + msg);
   1.123 +        errors++;
   1.124 +    }
   1.125 +
   1.126 +    class AnnoProc extends JavacTestingAbstractProcessor {
   1.127 +        Checker checker;
   1.128 +
   1.129 +        AnnoProc(DocTrees trees) {
   1.130 +            checker = new Checker(trees);
   1.131 +        }
   1.132 +
   1.133 +        @Override
   1.134 +        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   1.135 +            for (Element e : roundEnv.getRootElements()) {
   1.136 +                checker.scan(checker.trees.getPath(e), null);
   1.137 +            }
   1.138 +            return true;
   1.139 +        }
   1.140 +    }
   1.141 +
   1.142 +    class TaskListnr implements TaskListener {
   1.143 +        Checker checker;
   1.144 +
   1.145 +        TaskListnr(DocTrees trees) {
   1.146 +            checker = new Checker(trees);
   1.147 +        }
   1.148 +
   1.149 +        public void started(TaskEvent e) {
   1.150 +            if (e.getKind() == TaskEvent.Kind.ANALYZE)
   1.151 +                checker.scan(new TreePath(e.getCompilationUnit()), null);
   1.152 +        }
   1.153 +
   1.154 +        public void finished(TaskEvent e) {
   1.155 +        }
   1.156 +    }
   1.157 +
   1.158 +    class Checker extends TreePathScanner<Void,Void> {
   1.159 +        DocTrees trees;
   1.160 +        SourcePositions srcPosns;
   1.161 +
   1.162 +        int docComments = 0;
   1.163 +
   1.164 +        Checker(DocTrees trees) {
   1.165 +            this.trees = trees;
   1.166 +            srcPosns = trees.getSourcePositions();
   1.167 +        }
   1.168 +
   1.169 +        @Override
   1.170 +        public Void scan(Tree tree, Void ignore) {
   1.171 +            if (tree != null) {
   1.172 +                switch (tree.getKind()) {
   1.173 +                    // HACK: Workaround 8007350
   1.174 +                    // Some tree nodes do not have endpos set
   1.175 +                    case ASSIGNMENT:
   1.176 +                    case BLOCK:
   1.177 +                    case IDENTIFIER:
   1.178 +                    case METHOD_INVOCATION:
   1.179 +                        break;
   1.180 +
   1.181 +                    default:
   1.182 +                        checkEndPos(getCurrentPath().getCompilationUnit(), tree);
   1.183 +                }
   1.184 +            }
   1.185 +            return super.scan(tree, ignore);
   1.186 +        }
   1.187 +
   1.188 +        @Override
   1.189 +        public Void visitClass(ClassTree tree, Void ignore) {
   1.190 +            checkComment();
   1.191 +            return super.visitClass(tree, ignore);
   1.192 +        }
   1.193 +
   1.194 +        @Override
   1.195 +        public Void visitMethod(MethodTree tree, Void ignore) {
   1.196 +            checkComment();
   1.197 +            return super.visitMethod(tree, ignore);
   1.198 +        }
   1.199 +
   1.200 +        @Override
   1.201 +        public Void visitVariable(VariableTree tree, Void ignore) {
   1.202 +            checkComment();
   1.203 +            return super.visitVariable(tree, ignore);
   1.204 +        }
   1.205 +
   1.206 +        void checkComment() {
   1.207 +            DocCommentTree dc = trees.getDocCommentTree(getCurrentPath());
   1.208 +            if (dc != null) {
   1.209 +                out.println("comment: " + dc.toString().replaceAll("\\s+", " "));
   1.210 +                docComments++;
   1.211 +            }
   1.212 +        }
   1.213 +
   1.214 +        void checkEndPos(CompilationUnitTree unit, Tree tree) {
   1.215 +            long sp = srcPosns.getStartPosition(unit, tree);
   1.216 +            long ep = srcPosns.getEndPosition(unit, tree);
   1.217 +            if (sp >= 0 && ep == Position.NOPOS) {
   1.218 +                error("endpos not set for " + tree.getKind()
   1.219 +                        + " " + Pretty.toSimpleString(((JCTree) tree))
   1.220 +                        +", start:" + sp);
   1.221 +            }
   1.222 +        }
   1.223 +
   1.224 +        void checkDocComments(int expected) {
   1.225 +            if (docComments != expected) {
   1.226 +                error("Unexpected number of doc comments received: "
   1.227 +                        + docComments + ", expected: " + expected);
   1.228 +            }
   1.229 +        }
   1.230 +
   1.231 +    }
   1.232 +}

mercurial