Fri, 01 Feb 2013 12:01:03 -0800
8007344: javac may not make tree end positions and/or doc comments available to processors and listeners
Reviewed-by: darcy
jjg@1539 | 1 | /* |
jjg@1539 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1539 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1539 | 4 | * |
jjg@1539 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1539 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1539 | 7 | * published by the Free Software Foundation. |
jjg@1539 | 8 | * |
jjg@1539 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1539 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1539 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1539 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1539 | 13 | * accompanied this code). |
jjg@1539 | 14 | * |
jjg@1539 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1539 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1539 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1539 | 18 | * |
jjg@1539 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1539 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1539 | 21 | * questions. |
jjg@1539 | 22 | */ |
jjg@1539 | 23 | |
jjg@1539 | 24 | /* |
jjg@1539 | 25 | * @test |
jjg@1539 | 26 | * @bug 8007344 |
jjg@1539 | 27 | * @summary javac may not make tree end positions and/or doc comments |
jjg@1539 | 28 | * available to processors and listeners |
jjg@1539 | 29 | * @library /tools/javac/lib |
jjg@1539 | 30 | * @build JavacTestingAbstractProcessor |
jjg@1539 | 31 | * @run main Test |
jjg@1539 | 32 | */ |
jjg@1539 | 33 | |
jjg@1539 | 34 | import java.io.File; |
jjg@1539 | 35 | import java.io.PrintWriter; |
jjg@1539 | 36 | import java.util.Arrays; |
jjg@1539 | 37 | import java.util.Set; |
jjg@1539 | 38 | |
jjg@1539 | 39 | import javax.annotation.processing.RoundEnvironment; |
jjg@1539 | 40 | import javax.lang.model.element.Element; |
jjg@1539 | 41 | import javax.lang.model.element.TypeElement; |
jjg@1539 | 42 | import javax.tools.JavaFileObject; |
jjg@1539 | 43 | import javax.tools.StandardJavaFileManager; |
jjg@1539 | 44 | |
jjg@1539 | 45 | import com.sun.source.doctree.DocCommentTree; |
jjg@1539 | 46 | import com.sun.source.tree.*; |
jjg@1539 | 47 | import com.sun.source.util.DocTrees; |
jjg@1539 | 48 | import com.sun.source.util.JavacTask; |
jjg@1539 | 49 | import com.sun.source.util.SourcePositions; |
jjg@1539 | 50 | import com.sun.source.util.TaskEvent; |
jjg@1539 | 51 | import com.sun.source.util.TaskListener; |
jjg@1539 | 52 | import com.sun.source.util.TreePath; |
jjg@1539 | 53 | import com.sun.source.util.TreePathScanner; |
jjg@1539 | 54 | import com.sun.tools.javac.api.JavacTool; |
jjg@1539 | 55 | import com.sun.tools.javac.tree.JCTree; |
jjg@1539 | 56 | import com.sun.tools.javac.tree.Pretty; |
jjg@1539 | 57 | import com.sun.tools.javac.util.Position; |
jjg@1539 | 58 | |
jjg@1539 | 59 | /** Doc comment: Test */ |
jjg@1539 | 60 | public class Test { |
jjg@1539 | 61 | public static final int EXPECT_DOC_COMMENTS = 3; |
jjg@1539 | 62 | |
jjg@1539 | 63 | /** Doc comment: main */ |
jjg@1539 | 64 | public static void main(String... args) throws Exception { |
jjg@1539 | 65 | PrintWriter out = new PrintWriter(System.err); |
jjg@1539 | 66 | try { |
jjg@1539 | 67 | new Test(out).run(); |
jjg@1539 | 68 | } finally { |
jjg@1539 | 69 | out.flush(); |
jjg@1539 | 70 | } |
jjg@1539 | 71 | } |
jjg@1539 | 72 | |
jjg@1539 | 73 | PrintWriter out; |
jjg@1539 | 74 | int errors; |
jjg@1539 | 75 | |
jjg@1539 | 76 | Test(PrintWriter out) { |
jjg@1539 | 77 | this.out = out; |
jjg@1539 | 78 | } |
jjg@1539 | 79 | |
jjg@1539 | 80 | /** Doc comment: run */ |
jjg@1539 | 81 | void run() throws Exception { |
jjg@1539 | 82 | File testSrc = new File(System.getProperty("test.src")); |
jjg@1539 | 83 | File thisFile = new File(testSrc, getClass().getName() + ".java"); |
jjg@1539 | 84 | JavacTool javac = JavacTool.create(); |
jjg@1539 | 85 | StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); |
jjg@1539 | 86 | Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile); |
jjg@1539 | 87 | testAnnoProcessor(javac, fm, fos, out, EXPECT_DOC_COMMENTS); |
jjg@1539 | 88 | testTaskListener(javac, fm, fos, out, EXPECT_DOC_COMMENTS); |
jjg@1539 | 89 | |
jjg@1539 | 90 | if (errors > 0) |
jjg@1539 | 91 | throw new Exception(errors + " errors occurred"); |
jjg@1539 | 92 | } |
jjg@1539 | 93 | |
jjg@1539 | 94 | void testAnnoProcessor(JavacTool javac, StandardJavaFileManager fm, |
jjg@1539 | 95 | Iterable<? extends JavaFileObject> files, PrintWriter out, |
jjg@1539 | 96 | int expectedDocComments) { |
jjg@1539 | 97 | out.println("Test annotation processor"); |
jjg@1539 | 98 | JavacTask task = javac.getTask(out, fm, null, null, null, files); |
jjg@1539 | 99 | AnnoProc ap = new AnnoProc(DocTrees.instance(task)); |
jjg@1539 | 100 | task.setProcessors(Arrays.asList(ap)); |
jjg@1539 | 101 | task.call(); |
jjg@1539 | 102 | ap.checker.checkDocComments(expectedDocComments); |
jjg@1539 | 103 | } |
jjg@1539 | 104 | |
jjg@1539 | 105 | void testTaskListener(JavacTool javac, StandardJavaFileManager fm, |
jjg@1539 | 106 | Iterable<? extends JavaFileObject> files, PrintWriter out, |
jjg@1539 | 107 | int expectedDocComments) { |
jjg@1539 | 108 | out.println("Test task listener"); |
jjg@1539 | 109 | JavacTask task = javac.getTask(out, fm, null, null, null, files); |
jjg@1539 | 110 | TaskListnr tl = new TaskListnr(DocTrees.instance(task)); |
jjg@1539 | 111 | task.addTaskListener(tl); |
jjg@1539 | 112 | task.call(); |
jjg@1539 | 113 | tl.checker.checkDocComments(expectedDocComments); |
jjg@1539 | 114 | } |
jjg@1539 | 115 | |
jjg@1539 | 116 | void error(String msg) { |
jjg@1539 | 117 | out.println("Error: " + msg); |
jjg@1539 | 118 | errors++; |
jjg@1539 | 119 | } |
jjg@1539 | 120 | |
jjg@1539 | 121 | class AnnoProc extends JavacTestingAbstractProcessor { |
jjg@1539 | 122 | Checker checker; |
jjg@1539 | 123 | |
jjg@1539 | 124 | AnnoProc(DocTrees trees) { |
jjg@1539 | 125 | checker = new Checker(trees); |
jjg@1539 | 126 | } |
jjg@1539 | 127 | |
jjg@1539 | 128 | @Override |
jjg@1539 | 129 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
jjg@1539 | 130 | for (Element e : roundEnv.getRootElements()) { |
jjg@1539 | 131 | checker.scan(checker.trees.getPath(e), null); |
jjg@1539 | 132 | } |
jjg@1539 | 133 | return true; |
jjg@1539 | 134 | } |
jjg@1539 | 135 | } |
jjg@1539 | 136 | |
jjg@1539 | 137 | class TaskListnr implements TaskListener { |
jjg@1539 | 138 | Checker checker; |
jjg@1539 | 139 | |
jjg@1539 | 140 | TaskListnr(DocTrees trees) { |
jjg@1539 | 141 | checker = new Checker(trees); |
jjg@1539 | 142 | } |
jjg@1539 | 143 | |
jjg@1539 | 144 | public void started(TaskEvent e) { |
jjg@1539 | 145 | if (e.getKind() == TaskEvent.Kind.ANALYZE) |
jjg@1539 | 146 | checker.scan(new TreePath(e.getCompilationUnit()), null); |
jjg@1539 | 147 | } |
jjg@1539 | 148 | |
jjg@1539 | 149 | public void finished(TaskEvent e) { |
jjg@1539 | 150 | } |
jjg@1539 | 151 | } |
jjg@1539 | 152 | |
jjg@1539 | 153 | class Checker extends TreePathScanner<Void,Void> { |
jjg@1539 | 154 | DocTrees trees; |
jjg@1539 | 155 | SourcePositions srcPosns; |
jjg@1539 | 156 | |
jjg@1539 | 157 | int docComments = 0; |
jjg@1539 | 158 | |
jjg@1539 | 159 | Checker(DocTrees trees) { |
jjg@1539 | 160 | this.trees = trees; |
jjg@1539 | 161 | srcPosns = trees.getSourcePositions(); |
jjg@1539 | 162 | } |
jjg@1539 | 163 | |
jjg@1539 | 164 | @Override |
jjg@1539 | 165 | public Void scan(Tree tree, Void ignore) { |
jjg@1539 | 166 | if (tree != null) { |
jjg@1539 | 167 | switch (tree.getKind()) { |
jjg@1539 | 168 | // HACK: Workaround 8007350 |
jjg@1539 | 169 | // Some tree nodes do not have endpos set |
jjg@1539 | 170 | case ASSIGNMENT: |
jjg@1539 | 171 | case BLOCK: |
jjg@1539 | 172 | case IDENTIFIER: |
jjg@1539 | 173 | case METHOD_INVOCATION: |
jjg@1539 | 174 | break; |
jjg@1539 | 175 | |
jjg@1539 | 176 | default: |
jjg@1539 | 177 | checkEndPos(getCurrentPath().getCompilationUnit(), tree); |
jjg@1539 | 178 | } |
jjg@1539 | 179 | } |
jjg@1539 | 180 | return super.scan(tree, ignore); |
jjg@1539 | 181 | } |
jjg@1539 | 182 | |
jjg@1539 | 183 | @Override |
jjg@1539 | 184 | public Void visitClass(ClassTree tree, Void ignore) { |
jjg@1539 | 185 | checkComment(); |
jjg@1539 | 186 | return super.visitClass(tree, ignore); |
jjg@1539 | 187 | } |
jjg@1539 | 188 | |
jjg@1539 | 189 | @Override |
jjg@1539 | 190 | public Void visitMethod(MethodTree tree, Void ignore) { |
jjg@1539 | 191 | checkComment(); |
jjg@1539 | 192 | return super.visitMethod(tree, ignore); |
jjg@1539 | 193 | } |
jjg@1539 | 194 | |
jjg@1539 | 195 | @Override |
jjg@1539 | 196 | public Void visitVariable(VariableTree tree, Void ignore) { |
jjg@1539 | 197 | checkComment(); |
jjg@1539 | 198 | return super.visitVariable(tree, ignore); |
jjg@1539 | 199 | } |
jjg@1539 | 200 | |
jjg@1539 | 201 | void checkComment() { |
jjg@1539 | 202 | DocCommentTree dc = trees.getDocCommentTree(getCurrentPath()); |
jjg@1539 | 203 | if (dc != null) { |
jjg@1539 | 204 | out.println("comment: " + dc.toString().replaceAll("\\s+", " ")); |
jjg@1539 | 205 | docComments++; |
jjg@1539 | 206 | } |
jjg@1539 | 207 | } |
jjg@1539 | 208 | |
jjg@1539 | 209 | void checkEndPos(CompilationUnitTree unit, Tree tree) { |
jjg@1539 | 210 | long sp = srcPosns.getStartPosition(unit, tree); |
jjg@1539 | 211 | long ep = srcPosns.getEndPosition(unit, tree); |
jjg@1539 | 212 | if (sp >= 0 && ep == Position.NOPOS) { |
jjg@1539 | 213 | error("endpos not set for " + tree.getKind() |
jjg@1539 | 214 | + " " + Pretty.toSimpleString(((JCTree) tree)) |
jjg@1539 | 215 | +", start:" + sp); |
jjg@1539 | 216 | } |
jjg@1539 | 217 | } |
jjg@1539 | 218 | |
jjg@1539 | 219 | void checkDocComments(int expected) { |
jjg@1539 | 220 | if (docComments != expected) { |
jjg@1539 | 221 | error("Unexpected number of doc comments received: " |
jjg@1539 | 222 | + docComments + ", expected: " + expected); |
jjg@1539 | 223 | } |
jjg@1539 | 224 | } |
jjg@1539 | 225 | |
jjg@1539 | 226 | } |
jjg@1539 | 227 | } |