test/tools/javac/T6406771.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * @test /nodynamiccopyright/
aoqi@0 3 * @bug 6406771
aoqi@0 4 * @summary CompilationUnitTree needs access to a line map
aoqi@0 5 */
aoqi@0 6
aoqi@0 7 // WARNING: White-space and layout is important in this file, especially tab characters.
aoqi@0 8
aoqi@0 9 import java.io.*;
aoqi@0 10 import java.util.*;
aoqi@0 11 import javax.annotation.processing.*;
aoqi@0 12 import javax.lang.model.*;
aoqi@0 13 import javax.lang.model.element.*;
aoqi@0 14 import javax.tools.*;
aoqi@0 15 import com.sun.tools.javac.api.*;
aoqi@0 16 import com.sun.source.tree.*;
aoqi@0 17 import com.sun.source.util.*;
aoqi@0 18 import com.sun.tools.javac.tree.JCTree;
aoqi@0 19
aoqi@0 20
aoqi@0 21 @SupportedAnnotationTypes("*")
aoqi@0 22 public class T6406771 extends AbstractProcessor {
aoqi@0 23 String[] tests = {
aoqi@0 24 "line:24",
aoqi@0 25 "line:25",
aoqi@0 26 "line:26", "line:26",
aoqi@0 27 // 1 2 3 4 5 6
aoqi@0 28 //3456789012345678901234567890123456789012345678901234567890
aoqi@0 29 "col:7", "col:16", "col:26", // this line uses spaces
aoqi@0 30 "col:9", "col:25", "col:41", // this line uses tabs
aoqi@0 31 "col:20", "col:43" // this line uses a mixture
aoqi@0 32 };
aoqi@0 33
aoqi@0 34 // White-space after this point does not matter
aoqi@0 35
aoqi@0 36 public static void main(String[] args) {
aoqi@0 37 String self = T6406771.class.getName();
aoqi@0 38 String testSrc = System.getProperty("test.src");
aoqi@0 39 String testClasses = System.getProperty("test.classes");
aoqi@0 40
aoqi@0 41 JavacTool tool = JavacTool.create();
aoqi@0 42 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
aoqi@0 43 JavaFileObject f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, self+".java"))).iterator().next();
aoqi@0 44
aoqi@0 45 List<String> opts = Arrays.asList("-d", ".", "-processorpath", testClasses, "-processor", self, "-proc:only");
aoqi@0 46
aoqi@0 47 JavacTask task = tool.getTask(null, fm, null, opts, null, Arrays.asList(f));
aoqi@0 48
aoqi@0 49 if (!task.call())
aoqi@0 50 throw new AssertionError("failed");
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) {
aoqi@0 54 final String LINE = "line" + ':'; // avoid matching this string
aoqi@0 55 final String COLUMN = "col" + ':';
aoqi@0 56 final Messager messager = processingEnv.getMessager();
aoqi@0 57 final Trees trees = Trees.instance(processingEnv);
aoqi@0 58
aoqi@0 59 TreeScanner<Void,LineMap> s = new TreeScanner<Void,LineMap>() {
aoqi@0 60 public Void visitLiteral(LiteralTree tree, LineMap lineMap) {
aoqi@0 61 if (tree.getKind() == Tree.Kind.STRING_LITERAL) {
aoqi@0 62 String s = (String) tree.getValue();
aoqi@0 63 int pos = ((JCTree) tree).pos; // can't get through public api, bug 6412669 filed
aoqi@0 64 String prefix;
aoqi@0 65 long found;
aoqi@0 66 if (s.startsWith(LINE)) {
aoqi@0 67 prefix = LINE;
aoqi@0 68 found = lineMap.getLineNumber(pos);
aoqi@0 69 }
aoqi@0 70 else if (s.startsWith(COLUMN)) {
aoqi@0 71 prefix = COLUMN;
aoqi@0 72 found = lineMap.getColumnNumber(pos);
aoqi@0 73 }
aoqi@0 74 else
aoqi@0 75 return null;
aoqi@0 76 int expect = Integer.parseInt(s.substring(prefix.length()));
aoqi@0 77 if (expect != found) {
aoqi@0 78 messager.printMessage(Diagnostic.Kind.ERROR,
aoqi@0 79 "Error: " + prefix + " pos=" + pos
aoqi@0 80 + " expect=" + expect + " found=" + found);
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83 return null;
aoqi@0 84 }
aoqi@0 85 };
aoqi@0 86
aoqi@0 87 for (Element e: rEnv.getRootElements()) {
aoqi@0 88 System.err.println(e);
aoqi@0 89 Tree t = trees.getTree(e);
aoqi@0 90 TreePath p = trees.getPath(e);
aoqi@0 91 s.scan(p.getLeaf(), p.getCompilationUnit().getLineMap());
aoqi@0 92
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 return true;
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 @Override
aoqi@0 99 public SourceVersion getSupportedSourceVersion() {
aoqi@0 100 return SourceVersion.latest();
aoqi@0 101 }
aoqi@0 102 }

mercurial