aoqi@0: /* aoqi@0: * @test /nodynamiccopyright/ aoqi@0: * @bug 6406771 aoqi@0: * @summary CompilationUnitTree needs access to a line map aoqi@0: */ aoqi@0: aoqi@0: // WARNING: White-space and layout is important in this file, especially tab characters. 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: import com.sun.tools.javac.api.*; aoqi@0: import com.sun.source.tree.*; aoqi@0: import com.sun.source.util.*; aoqi@0: import com.sun.tools.javac.tree.JCTree; aoqi@0: aoqi@0: aoqi@0: @SupportedAnnotationTypes("*") aoqi@0: public class T6406771 extends AbstractProcessor { aoqi@0: String[] tests = { aoqi@0: "line:24", aoqi@0: "line:25", aoqi@0: "line:26", "line:26", aoqi@0: // 1 2 3 4 5 6 aoqi@0: //3456789012345678901234567890123456789012345678901234567890 aoqi@0: "col:7", "col:16", "col:26", // this line uses spaces aoqi@0: "col:9", "col:25", "col:41", // this line uses tabs aoqi@0: "col:20", "col:43" // this line uses a mixture aoqi@0: }; aoqi@0: aoqi@0: // White-space after this point does not matter aoqi@0: aoqi@0: public static void main(String[] args) { aoqi@0: String self = T6406771.class.getName(); aoqi@0: String testSrc = System.getProperty("test.src"); aoqi@0: String testClasses = System.getProperty("test.classes"); aoqi@0: aoqi@0: JavacTool tool = JavacTool.create(); aoqi@0: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); aoqi@0: JavaFileObject f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, self+".java"))).iterator().next(); aoqi@0: aoqi@0: List opts = Arrays.asList("-d", ".", "-processorpath", testClasses, "-processor", self, "-proc:only"); aoqi@0: aoqi@0: JavacTask task = tool.getTask(null, fm, null, opts, null, Arrays.asList(f)); aoqi@0: aoqi@0: if (!task.call()) aoqi@0: throw new AssertionError("failed"); aoqi@0: } aoqi@0: aoqi@0: public boolean process(Set elems, RoundEnvironment rEnv) { aoqi@0: final String LINE = "line" + ':'; // avoid matching this string aoqi@0: final String COLUMN = "col" + ':'; aoqi@0: final Messager messager = processingEnv.getMessager(); aoqi@0: final Trees trees = Trees.instance(processingEnv); aoqi@0: aoqi@0: TreeScanner s = new TreeScanner() { aoqi@0: public Void visitLiteral(LiteralTree tree, LineMap lineMap) { aoqi@0: if (tree.getKind() == Tree.Kind.STRING_LITERAL) { aoqi@0: String s = (String) tree.getValue(); aoqi@0: int pos = ((JCTree) tree).pos; // can't get through public api, bug 6412669 filed aoqi@0: String prefix; aoqi@0: long found; aoqi@0: if (s.startsWith(LINE)) { aoqi@0: prefix = LINE; aoqi@0: found = lineMap.getLineNumber(pos); aoqi@0: } aoqi@0: else if (s.startsWith(COLUMN)) { aoqi@0: prefix = COLUMN; aoqi@0: found = lineMap.getColumnNumber(pos); aoqi@0: } aoqi@0: else aoqi@0: return null; aoqi@0: int expect = Integer.parseInt(s.substring(prefix.length())); aoqi@0: if (expect != found) { aoqi@0: messager.printMessage(Diagnostic.Kind.ERROR, aoqi@0: "Error: " + prefix + " pos=" + pos aoqi@0: + " expect=" + expect + " found=" + found); aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: for (Element e: rEnv.getRootElements()) { aoqi@0: System.err.println(e); aoqi@0: Tree t = trees.getTree(e); aoqi@0: TreePath p = trees.getPath(e); aoqi@0: s.scan(p.getLeaf(), p.getCompilationUnit().getLineMap()); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public SourceVersion getSupportedSourceVersion() { aoqi@0: return SourceVersion.latest(); aoqi@0: } aoqi@0: }