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

Tue, 05 Feb 2013 21:55:41 -0800

author
jjg
date
Tue, 05 Feb 2013 21:55:41 -0800
changeset 1549
de932285124c
parent 1539
0b1c88705568
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8007485: test creates .class files in the test/ directory
Reviewed-by: mcimadamore

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@1549 44 import javax.tools.StandardLocation;
jjg@1539 45
jjg@1539 46 import com.sun.source.doctree.DocCommentTree;
jjg@1539 47 import com.sun.source.tree.*;
jjg@1539 48 import com.sun.source.util.DocTrees;
jjg@1539 49 import com.sun.source.util.JavacTask;
jjg@1539 50 import com.sun.source.util.SourcePositions;
jjg@1539 51 import com.sun.source.util.TaskEvent;
jjg@1539 52 import com.sun.source.util.TaskListener;
jjg@1539 53 import com.sun.source.util.TreePath;
jjg@1539 54 import com.sun.source.util.TreePathScanner;
jjg@1539 55 import com.sun.tools.javac.api.JavacTool;
jjg@1539 56 import com.sun.tools.javac.tree.JCTree;
jjg@1539 57 import com.sun.tools.javac.tree.Pretty;
jjg@1539 58 import com.sun.tools.javac.util.Position;
jjg@1539 59
jjg@1539 60 /** Doc comment: Test */
jjg@1539 61 public class Test {
jjg@1539 62 public static final int EXPECT_DOC_COMMENTS = 3;
jjg@1539 63
jjg@1539 64 /** Doc comment: main */
jjg@1539 65 public static void main(String... args) throws Exception {
jjg@1539 66 PrintWriter out = new PrintWriter(System.err);
jjg@1539 67 try {
jjg@1539 68 new Test(out).run();
jjg@1539 69 } finally {
jjg@1539 70 out.flush();
jjg@1539 71 }
jjg@1539 72 }
jjg@1539 73
jjg@1539 74 PrintWriter out;
jjg@1539 75 int errors;
jjg@1539 76
jjg@1539 77 Test(PrintWriter out) {
jjg@1539 78 this.out = out;
jjg@1539 79 }
jjg@1539 80
jjg@1539 81 /** Doc comment: run */
jjg@1539 82 void run() throws Exception {
jjg@1539 83 File testSrc = new File(System.getProperty("test.src"));
jjg@1539 84 File thisFile = new File(testSrc, getClass().getName() + ".java");
jjg@1539 85 JavacTool javac = JavacTool.create();
jjg@1539 86 StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
jjg@1549 87 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
jjg@1539 88 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile);
jjg@1539 89 testAnnoProcessor(javac, fm, fos, out, EXPECT_DOC_COMMENTS);
jjg@1539 90 testTaskListener(javac, fm, fos, out, EXPECT_DOC_COMMENTS);
jjg@1539 91
jjg@1539 92 if (errors > 0)
jjg@1539 93 throw new Exception(errors + " errors occurred");
jjg@1539 94 }
jjg@1539 95
jjg@1539 96 void testAnnoProcessor(JavacTool javac, StandardJavaFileManager fm,
jjg@1539 97 Iterable<? extends JavaFileObject> files, PrintWriter out,
jjg@1539 98 int expectedDocComments) {
jjg@1539 99 out.println("Test annotation processor");
jjg@1539 100 JavacTask task = javac.getTask(out, fm, null, null, null, files);
jjg@1539 101 AnnoProc ap = new AnnoProc(DocTrees.instance(task));
jjg@1539 102 task.setProcessors(Arrays.asList(ap));
jjg@1539 103 task.call();
jjg@1539 104 ap.checker.checkDocComments(expectedDocComments);
jjg@1539 105 }
jjg@1539 106
jjg@1539 107 void testTaskListener(JavacTool javac, StandardJavaFileManager fm,
jjg@1539 108 Iterable<? extends JavaFileObject> files, PrintWriter out,
jjg@1539 109 int expectedDocComments) {
jjg@1539 110 out.println("Test task listener");
jjg@1539 111 JavacTask task = javac.getTask(out, fm, null, null, null, files);
jjg@1539 112 TaskListnr tl = new TaskListnr(DocTrees.instance(task));
jjg@1539 113 task.addTaskListener(tl);
jjg@1539 114 task.call();
jjg@1539 115 tl.checker.checkDocComments(expectedDocComments);
jjg@1539 116 }
jjg@1539 117
jjg@1539 118 void error(String msg) {
jjg@1539 119 out.println("Error: " + msg);
jjg@1539 120 errors++;
jjg@1539 121 }
jjg@1539 122
jjg@1539 123 class AnnoProc extends JavacTestingAbstractProcessor {
jjg@1539 124 Checker checker;
jjg@1539 125
jjg@1539 126 AnnoProc(DocTrees trees) {
jjg@1539 127 checker = new Checker(trees);
jjg@1539 128 }
jjg@1539 129
jjg@1539 130 @Override
jjg@1539 131 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@1539 132 for (Element e : roundEnv.getRootElements()) {
jjg@1539 133 checker.scan(checker.trees.getPath(e), null);
jjg@1539 134 }
jjg@1539 135 return true;
jjg@1539 136 }
jjg@1539 137 }
jjg@1539 138
jjg@1539 139 class TaskListnr implements TaskListener {
jjg@1539 140 Checker checker;
jjg@1539 141
jjg@1539 142 TaskListnr(DocTrees trees) {
jjg@1539 143 checker = new Checker(trees);
jjg@1539 144 }
jjg@1539 145
jjg@1539 146 public void started(TaskEvent e) {
jjg@1539 147 if (e.getKind() == TaskEvent.Kind.ANALYZE)
jjg@1539 148 checker.scan(new TreePath(e.getCompilationUnit()), null);
jjg@1539 149 }
jjg@1539 150
jjg@1539 151 public void finished(TaskEvent e) {
jjg@1539 152 }
jjg@1539 153 }
jjg@1539 154
jjg@1539 155 class Checker extends TreePathScanner<Void,Void> {
jjg@1539 156 DocTrees trees;
jjg@1539 157 SourcePositions srcPosns;
jjg@1539 158
jjg@1539 159 int docComments = 0;
jjg@1539 160
jjg@1539 161 Checker(DocTrees trees) {
jjg@1539 162 this.trees = trees;
jjg@1539 163 srcPosns = trees.getSourcePositions();
jjg@1539 164 }
jjg@1539 165
jjg@1539 166 @Override
jjg@1539 167 public Void scan(Tree tree, Void ignore) {
jjg@1539 168 if (tree != null) {
jjg@1539 169 switch (tree.getKind()) {
jjg@1539 170 // HACK: Workaround 8007350
jjg@1539 171 // Some tree nodes do not have endpos set
jjg@1539 172 case ASSIGNMENT:
jjg@1539 173 case BLOCK:
jjg@1539 174 case IDENTIFIER:
jjg@1539 175 case METHOD_INVOCATION:
jjg@1539 176 break;
jjg@1539 177
jjg@1539 178 default:
jjg@1539 179 checkEndPos(getCurrentPath().getCompilationUnit(), tree);
jjg@1539 180 }
jjg@1539 181 }
jjg@1539 182 return super.scan(tree, ignore);
jjg@1539 183 }
jjg@1539 184
jjg@1539 185 @Override
jjg@1539 186 public Void visitClass(ClassTree tree, Void ignore) {
jjg@1539 187 checkComment();
jjg@1539 188 return super.visitClass(tree, ignore);
jjg@1539 189 }
jjg@1539 190
jjg@1539 191 @Override
jjg@1539 192 public Void visitMethod(MethodTree tree, Void ignore) {
jjg@1539 193 checkComment();
jjg@1539 194 return super.visitMethod(tree, ignore);
jjg@1539 195 }
jjg@1539 196
jjg@1539 197 @Override
jjg@1539 198 public Void visitVariable(VariableTree tree, Void ignore) {
jjg@1539 199 checkComment();
jjg@1539 200 return super.visitVariable(tree, ignore);
jjg@1539 201 }
jjg@1539 202
jjg@1539 203 void checkComment() {
jjg@1539 204 DocCommentTree dc = trees.getDocCommentTree(getCurrentPath());
jjg@1539 205 if (dc != null) {
jjg@1539 206 out.println("comment: " + dc.toString().replaceAll("\\s+", " "));
jjg@1539 207 docComments++;
jjg@1539 208 }
jjg@1539 209 }
jjg@1539 210
jjg@1539 211 void checkEndPos(CompilationUnitTree unit, Tree tree) {
jjg@1539 212 long sp = srcPosns.getStartPosition(unit, tree);
jjg@1539 213 long ep = srcPosns.getEndPosition(unit, tree);
jjg@1539 214 if (sp >= 0 && ep == Position.NOPOS) {
jjg@1539 215 error("endpos not set for " + tree.getKind()
jjg@1539 216 + " " + Pretty.toSimpleString(((JCTree) tree))
jjg@1539 217 +", start:" + sp);
jjg@1539 218 }
jjg@1539 219 }
jjg@1539 220
jjg@1539 221 void checkDocComments(int expected) {
jjg@1539 222 if (docComments != expected) {
jjg@1539 223 error("Unexpected number of doc comments received: "
jjg@1539 224 + docComments + ", expected: " + expected);
jjg@1539 225 }
jjg@1539 226 }
jjg@1539 227
jjg@1539 228 }
jjg@1539 229 }

mercurial