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