1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/T6406771.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,102 @@ 1.4 +/* 1.5 + * @test /nodynamiccopyright/ 1.6 + * @bug 6406771 1.7 + * @summary CompilationUnitTree needs access to a line map 1.8 + */ 1.9 + 1.10 +// WARNING: White-space and layout is important in this file, especially tab characters. 1.11 + 1.12 +import java.io.*; 1.13 +import java.util.*; 1.14 +import javax.annotation.processing.*; 1.15 +import javax.lang.model.*; 1.16 +import javax.lang.model.element.*; 1.17 +import javax.tools.*; 1.18 +import com.sun.tools.javac.api.*; 1.19 +import com.sun.source.tree.*; 1.20 +import com.sun.source.util.*; 1.21 +import com.sun.tools.javac.tree.JCTree; 1.22 + 1.23 + 1.24 +@SupportedAnnotationTypes("*") 1.25 +public class T6406771 extends AbstractProcessor { 1.26 + String[] tests = { 1.27 + "line:24", 1.28 + "line:25", 1.29 + "line:26", "line:26", 1.30 +// 1 2 3 4 5 6 1.31 +//3456789012345678901234567890123456789012345678901234567890 1.32 + "col:7", "col:16", "col:26", // this line uses spaces 1.33 + "col:9", "col:25", "col:41", // this line uses tabs 1.34 + "col:20", "col:43" // this line uses a mixture 1.35 + }; 1.36 + 1.37 + // White-space after this point does not matter 1.38 + 1.39 + public static void main(String[] args) { 1.40 + String self = T6406771.class.getName(); 1.41 + String testSrc = System.getProperty("test.src"); 1.42 + String testClasses = System.getProperty("test.classes"); 1.43 + 1.44 + JavacTool tool = JavacTool.create(); 1.45 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.46 + JavaFileObject f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, self+".java"))).iterator().next(); 1.47 + 1.48 + List<String> opts = Arrays.asList("-d", ".", "-processorpath", testClasses, "-processor", self, "-proc:only"); 1.49 + 1.50 + JavacTask task = tool.getTask(null, fm, null, opts, null, Arrays.asList(f)); 1.51 + 1.52 + if (!task.call()) 1.53 + throw new AssertionError("failed"); 1.54 + } 1.55 + 1.56 + public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) { 1.57 + final String LINE = "line" + ':'; // avoid matching this string 1.58 + final String COLUMN = "col" + ':'; 1.59 + final Messager messager = processingEnv.getMessager(); 1.60 + final Trees trees = Trees.instance(processingEnv); 1.61 + 1.62 + TreeScanner<Void,LineMap> s = new TreeScanner<Void,LineMap>() { 1.63 + public Void visitLiteral(LiteralTree tree, LineMap lineMap) { 1.64 + if (tree.getKind() == Tree.Kind.STRING_LITERAL) { 1.65 + String s = (String) tree.getValue(); 1.66 + int pos = ((JCTree) tree).pos; // can't get through public api, bug 6412669 filed 1.67 + String prefix; 1.68 + long found; 1.69 + if (s.startsWith(LINE)) { 1.70 + prefix = LINE; 1.71 + found = lineMap.getLineNumber(pos); 1.72 + } 1.73 + else if (s.startsWith(COLUMN)) { 1.74 + prefix = COLUMN; 1.75 + found = lineMap.getColumnNumber(pos); 1.76 + } 1.77 + else 1.78 + return null; 1.79 + int expect = Integer.parseInt(s.substring(prefix.length())); 1.80 + if (expect != found) { 1.81 + messager.printMessage(Diagnostic.Kind.ERROR, 1.82 + "Error: " + prefix + " pos=" + pos 1.83 + + " expect=" + expect + " found=" + found); 1.84 + } 1.85 + } 1.86 + return null; 1.87 + } 1.88 + }; 1.89 + 1.90 + for (Element e: rEnv.getRootElements()) { 1.91 + System.err.println(e); 1.92 + Tree t = trees.getTree(e); 1.93 + TreePath p = trees.getPath(e); 1.94 + s.scan(p.getLeaf(), p.getCompilationUnit().getLineMap()); 1.95 + 1.96 + } 1.97 + 1.98 + return true; 1.99 + } 1.100 + 1.101 + @Override 1.102 + public SourceVersion getSupportedSourceVersion() { 1.103 + return SourceVersion.latest(); 1.104 + } 1.105 +}