author | lana |
Tue, 25 Apr 2017 07:38:50 +0000 | |
changeset 44909 | b53c770dc04f |
parent 40308 | 274367a99f98 |
child 44573 | 245bb4e6f983 |
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. |
|
36526 | 11 |
// Editing the imports and other leading text may affect the golden text in the tests field. |
12 |
// Also beware of scripts that auto-expand tabs to spaces. |
|
10 | 13 |
|
14 |
import java.io.*; |
|
36526 | 15 |
import java.lang.reflect.Layer; |
16 |
import java.lang.reflect.Module; |
|
10 | 17 |
import java.util.*; |
18 |
import javax.annotation.processing.*; |
|
19 |
import javax.lang.model.*; |
|
20 |
import javax.lang.model.element.*; |
|
21 |
import javax.tools.*; |
|
36526 | 22 |
|
10 | 23 |
import com.sun.source.tree.*; |
24 |
import com.sun.source.util.*; |
|
36526 | 25 |
import com.sun.tools.javac.api.*; |
10 | 26 |
import com.sun.tools.javac.tree.JCTree; |
27 |
||
28 |
@SupportedAnnotationTypes("*") |
|
29 |
public class T6406771 extends AbstractProcessor { |
|
30 |
String[] tests = { |
|
36526 | 31 |
"line:31", |
32 |
"line:32", |
|
33 |
"line:33", "line:33", |
|
10 | 34 |
// 1 2 3 4 5 6 |
35 |
//3456789012345678901234567890123456789012345678901234567890 |
|
36 |
"col:7", "col:16", "col:26", // this line uses spaces |
|
37 |
"col:9", "col:25", "col:41", // this line uses tabs |
|
38 |
"col:20", "col:43" // this line uses a mixture |
|
39 |
}; |
|
40 |
||
41 |
// White-space after this point does not matter |
|
42 |
||
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
4935
diff
changeset
|
43 |
public static void main(String[] args) throws IOException { |
10 | 44 |
String self = T6406771.class.getName(); |
45 |
String testSrc = System.getProperty("test.src"); |
|
46 |
String testClasses = System.getProperty("test.classes"); |
|
47 |
||
36526 | 48 |
JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
4935
diff
changeset
|
49 |
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
4935
diff
changeset
|
50 |
JavaFileObject f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, self+".java"))).iterator().next(); |
10 | 51 |
|
36526 | 52 |
List<String> opts = Arrays.asList( |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37848
diff
changeset
|
53 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37848
diff
changeset
|
54 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", |
36526 | 55 |
"-XDaccessInternalAPI", |
56 |
"-d", ".", |
|
57 |
"-processorpath", testClasses, |
|
58 |
"-processor", self, |
|
59 |
"-proc:only"); |
|
10 | 60 |
|
36526 | 61 |
JavacTask task = (JavacTask)tool.getTask(null, fm, null, opts, null, Arrays.asList(f)); |
10 | 62 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
4935
diff
changeset
|
63 |
if (!task.call()) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
4935
diff
changeset
|
64 |
throw new AssertionError("failed"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
4935
diff
changeset
|
65 |
} |
10 | 66 |
} |
67 |
||
68 |
public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) { |
|
69 |
final String LINE = "line" + ':'; // avoid matching this string |
|
70 |
final String COLUMN = "col" + ':'; |
|
71 |
final Messager messager = processingEnv.getMessager(); |
|
72 |
final Trees trees = Trees.instance(processingEnv); |
|
73 |
||
74 |
TreeScanner<Void,LineMap> s = new TreeScanner<Void,LineMap>() { |
|
75 |
public Void visitLiteral(LiteralTree tree, LineMap lineMap) { |
|
76 |
if (tree.getKind() == Tree.Kind.STRING_LITERAL) { |
|
77 |
String s = (String) tree.getValue(); |
|
78 |
int pos = ((JCTree) tree).pos; // can't get through public api, bug 6412669 filed |
|
79 |
String prefix; |
|
80 |
long found; |
|
81 |
if (s.startsWith(LINE)) { |
|
82 |
prefix = LINE; |
|
83 |
found = lineMap.getLineNumber(pos); |
|
84 |
} |
|
85 |
else if (s.startsWith(COLUMN)) { |
|
86 |
prefix = COLUMN; |
|
87 |
found = lineMap.getColumnNumber(pos); |
|
88 |
} |
|
89 |
else |
|
90 |
return null; |
|
91 |
int expect = Integer.parseInt(s.substring(prefix.length())); |
|
92 |
if (expect != found) { |
|
93 |
messager.printMessage(Diagnostic.Kind.ERROR, |
|
94 |
"Error: " + prefix + " pos=" + pos |
|
95 |
+ " expect=" + expect + " found=" + found); |
|
96 |
} |
|
97 |
} |
|
98 |
return null; |
|
99 |
} |
|
100 |
}; |
|
101 |
||
102 |
for (Element e: rEnv.getRootElements()) { |
|
103 |
System.err.println(e); |
|
104 |
Tree t = trees.getTree(e); |
|
105 |
TreePath p = trees.getPath(e); |
|
106 |
s.scan(p.getLeaf(), p.getCompilationUnit().getLineMap()); |
|
107 |
||
108 |
} |
|
109 |
||
110 |
return true; |
|
111 |
} |
|
112 |
||
4935
ff8adaa7bb8e
6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents:
10
diff
changeset
|
113 |
@Override |
ff8adaa7bb8e
6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents:
10
diff
changeset
|
114 |
public SourceVersion getSupportedSourceVersion() { |
ff8adaa7bb8e
6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents:
10
diff
changeset
|
115 |
return SourceVersion.latest(); |
ff8adaa7bb8e
6926699: Annotation processing regression tests should typically return SourceVersion.latest
darcy
parents:
10
diff
changeset
|
116 |
} |
10 | 117 |
} |