aoqi@0: /* aoqi@0: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6985205 6986246 aoqi@0: * @summary access to tree positions and doc comments may be lost across annotation processing rounds aoqi@0: * @build TreePosRoundsTest aoqi@0: * @compile -proc:only -processor TreePosRoundsTest TreePosRoundsTest.java aoqi@0: * @run main TreePosRoundsTest aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import javax.annotation.processing.*; aoqi@0: import javax.lang.model.*; aoqi@0: import javax.lang.model.element.*; aoqi@0: import javax.tools.*; aoqi@0: aoqi@0: import com.sun.source.tree.*; aoqi@0: import com.sun.source.util.*; aoqi@0: import javax.tools.JavaCompiler.CompilationTask; aoqi@0: aoqi@0: // This test is an annotation processor that performs multiple rounds of aoqi@0: // processing, and on each round, it checks that source positions are aoqi@0: // available and correct. aoqi@0: // aoqi@0: // The test can be run directly as a processor from the javac command line aoqi@0: // or via JSR 199 by invoking the main program. aoqi@0: aoqi@0: @SupportedAnnotationTypes("*") aoqi@0: public class TreePosRoundsTest extends AbstractProcessor { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: String testSrc = System.getProperty("test.src"); aoqi@0: String testClasses = System.getProperty("test.classes"); aoqi@0: JavaCompiler c = ToolProvider.getSystemJavaCompiler(); aoqi@0: StandardJavaFileManager fm = c.getStandardFileManager(null, null, null); aoqi@0: String thisName = TreePosRoundsTest.class.getName(); aoqi@0: File thisFile = new File(testSrc, thisName + ".java"); aoqi@0: Iterable files = fm.getJavaFileObjects(thisFile); aoqi@0: List options = Arrays.asList( aoqi@0: "-proc:only", aoqi@0: "-processor", thisName, aoqi@0: "-processorpath", testClasses); aoqi@0: CompilationTask t = c.getTask(null, fm, null, options, null, files); aoqi@0: boolean ok = t.call(); aoqi@0: if (!ok) aoqi@0: throw new Exception("processing failed"); aoqi@0: } aoqi@0: aoqi@0: Filer filer; aoqi@0: Messager messager; aoqi@0: Trees trees; aoqi@0: aoqi@0: @Override aoqi@0: public SourceVersion getSupportedSourceVersion() { aoqi@0: return SourceVersion.latest(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void init(ProcessingEnvironment pEnv) { aoqi@0: super.init(pEnv); aoqi@0: filer = pEnv.getFiler(); aoqi@0: messager = pEnv.getMessager(); aoqi@0: trees = Trees.instance(pEnv); aoqi@0: } aoqi@0: aoqi@0: int round = 0; aoqi@0: aoqi@0: @Override aoqi@0: public boolean process(Set annotations, RoundEnvironment roundEnv) { aoqi@0: round++; aoqi@0: aoqi@0: // Scan trees for elements, verifying source tree positions aoqi@0: for (Element e: roundEnv.getRootElements()) { aoqi@0: try { aoqi@0: TreePath p = trees.getPath(e); aoqi@0: new TestTreeScanner(p.getCompilationUnit(), trees).scan(trees.getPath(e), null); aoqi@0: } catch (IOException ex) { aoqi@0: messager.printMessage(Diagnostic.Kind.ERROR, aoqi@0: "Cannot get source: " + ex, e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: final int MAXROUNDS = 3; aoqi@0: if (round < MAXROUNDS) aoqi@0: generateSource("Gen" + round); aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void generateSource(String name) { aoqi@0: StringBuilder text = new StringBuilder(); aoqi@0: text.append("class ").append(name).append("{\n"); aoqi@0: text.append(" int one = 1;\n"); aoqi@0: text.append(" int two = 2;\n"); aoqi@0: text.append(" int three = one + two;\n"); aoqi@0: text.append("}\n"); aoqi@0: aoqi@0: try { aoqi@0: JavaFileObject fo = filer.createSourceFile(name); aoqi@0: Writer out = fo.openWriter(); aoqi@0: try { aoqi@0: out.write(text.toString()); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: } catch (IOException e) { aoqi@0: throw new Error(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class TestTreeScanner extends TreePathScanner { aoqi@0: TestTreeScanner(CompilationUnitTree unit, Trees trees) throws IOException { aoqi@0: this.unit = unit; aoqi@0: JavaFileObject sf = unit.getSourceFile(); aoqi@0: source = sf.getCharContent(true).toString(); aoqi@0: sourcePositions = trees.getSourcePositions(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Void visitVariable(VariableTree tree, Void _) { aoqi@0: check(getCurrentPath()); aoqi@0: return super.visitVariable(tree, _); aoqi@0: } aoqi@0: aoqi@0: void check(TreePath tp) { aoqi@0: Tree tree = tp.getLeaf(); aoqi@0: aoqi@0: String expect = tree.toString(); aoqi@0: if (tree.getKind() == Tree.Kind.VARIABLE) { aoqi@0: // tree.toString() does not know enough context to add ";", aoqi@0: // so deal with that manually... aoqi@0: Tree.Kind enclKind = tp.getParentPath().getLeaf().getKind(); aoqi@0: //System.err.println(" encl: " +enclKind); aoqi@0: if (enclKind == Tree.Kind.CLASS || enclKind == Tree.Kind.BLOCK) aoqi@0: expect += ";"; aoqi@0: } aoqi@0: //System.err.println("expect: " + expect); aoqi@0: aoqi@0: int start = (int)sourcePositions.getStartPosition(unit, tree); aoqi@0: if (start == Diagnostic.NOPOS) { aoqi@0: messager.printMessage(Diagnostic.Kind.ERROR, "start pos not set for " + trim(tree)); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: int end = (int)sourcePositions.getEndPosition(unit, tree); aoqi@0: if (end == Diagnostic.NOPOS) { aoqi@0: messager.printMessage(Diagnostic.Kind.ERROR, "end pos not set for " + trim(tree)); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: String found = source.substring(start, end); aoqi@0: //System.err.println(" found: " + found); aoqi@0: aoqi@0: // allow for long lines, in which case just compare beginning and aoqi@0: // end of the strings aoqi@0: boolean equal; aoqi@0: if (found.contains("\n")) { aoqi@0: String head = found.substring(0, found.indexOf("\n")); aoqi@0: String tail = found.substring(found.lastIndexOf("\n")).trim(); aoqi@0: equal = expect.startsWith(head) && expect.endsWith(tail); aoqi@0: } else { aoqi@0: equal = expect.equals(found); aoqi@0: } aoqi@0: aoqi@0: if (!equal) { aoqi@0: messager.printMessage(Diagnostic.Kind.ERROR, aoqi@0: "unexpected value found: '" + found + "'; expected: '" + expect + "'"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: String trim(Tree tree) { aoqi@0: final int MAXLEN = 32; aoqi@0: String s = tree.toString().replaceAll("\\s+", " ").trim(); aoqi@0: return (s.length() < MAXLEN) ? s : s.substring(0, MAXLEN); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: CompilationUnitTree unit; aoqi@0: SourcePositions sourcePositions; aoqi@0: String source; aoqi@0: } aoqi@0: aoqi@0: }