36
import com.sun.source.tree.AnnotationTree;
37
import com.sun.source.tree.CompilationUnitTree;
38
import com.sun.source.tree.ErroneousTree;
39
import com.sun.source.tree.VariableTree;
40
import com.sun.source.util.TaskEvent;
41
import com.sun.source.util.TaskListener;
42
import com.sun.source.util.TreePath;
43
import com.sun.source.util.TreePathScanner;
44
import com.sun.source.util.Trees;
45
import java.nio.file.Files;
46
import java.nio.file.Path;
47
import java.nio.file.Paths;
49
import java.util.concurrent.atomic.AtomicInteger;
50
import javax.lang.model.element.Element;
51
import javax.lang.model.type.TypeMirror;
53
import toolbox.TestRunner;
54
import toolbox.JavacTask;
56
import toolbox.ToolBox;
58
public class AttrRecoveryTest extends TestRunner {
62
public static void main(String... args) throws Exception {
63
new AttrRecoveryTest().runTests();
71
public void runTests() throws Exception {
72
runTests(m -> new Object[] { Paths.get(m.getName()) });
76
public void testModifiers(Path base) throws Exception {
77
record TestCase(String name, String source, String expectedAnnotation, String... errors) {}
78
TestCase[] tests = new TestCase[] {
82
Object i () { return int strictfp @Deprecated = 0; }
85
"java.lang.Deprecated",
86
"Test.java:2:30: compiler.err.dot.class.expected",
87
"Test.java:2:51: compiler.err.expected4: class, interface, enum, record",
88
"Test.java:2:26: compiler.err.unexpected.type: kindname.value, kindname.class",
93
Object i () { return int strictfp = 0; }
97
"Test.java:2:30: compiler.err.dot.class.expected",
98
"Test.java:2:39: compiler.err.expected4: class, interface, enum, record",
99
"Test.java:2:26: compiler.err.unexpected.type: kindname.value, kindname.class",
102
for (TestCase test : tests) {
103
Path current = base.resolve("" + test.name);
104
Path src = current.resolve("src");
105
Path classes = current.resolve("classes");
106
tb.writeJavaFiles(src,
109
Files.createDirectories(classes);
113
.options("-XDrawDiagnostics",
114
"-XDshould-stop.at=FLOW",
117
.files(tb.findJavaFiles(src))
119
t.addTaskListener(new TaskListener() {
120
CompilationUnitTree parsed;
122
public void finished(TaskEvent e) {
123
switch (e.getKind()) {
124
case PARSE -> parsed = e.getCompilationUnit();
126
checkAnnotationsValid(t, parsed, test.expectedAnnotation);
131
.run(Task.Expect.FAIL, 1)
133
.getOutputLines(Task.OutputKind.DIRECT);
134
if (!List.of(test.errors).equals(log)) {
135
throw new AssertionError("Incorrect errors, expected: " + List.of(test.errors) +
141
private void checkAnnotationsValid(com.sun.source.util.JavacTask task,
142
CompilationUnitTree cut,
144
boolean[] foundAnnotation = new boolean[1];
145
Trees trees = Trees.instance(task);
147
new TreePathScanner<Void, Void>() {
149
public Void visitAnnotation(AnnotationTree node, Void p) {
150
TreePath typePath = new TreePath(getCurrentPath(), node.getAnnotationType());
151
Element el = trees.getElement(typePath);
152
if (el == null || !el.equals(task.getElements().getTypeElement(expected))) {
153
throw new AssertionError();
155
foundAnnotation[0] = true;
156
return super.visitAnnotation(node, p);
160
public Void visitErroneous(ErroneousTree node, Void p) {
161
return scan(node.getErrorTrees(), p);
164
if (foundAnnotation[0] ^ (expected != null)) {
165
throw new AssertionError();
170
public void testVarAssignment2Self(Path base) throws Exception {
172
Path src = current.resolve("src");
173
Path classes = current.resolve("classes");
174
tb.writeJavaFiles(src,
183
Files.createDirectories(classes);
185
AtomicInteger seenVariables = new AtomicInteger();
186
TreePathScanner<Void, Trees> checkTypes = new TreePathScanner<>() {
188
public Void visitVariable(VariableTree node, Trees trees) {
189
if (node.getName().contentEquals("v")) {
190
TypeMirror type = trees.getTypeMirror(getCurrentPath());
192
throw new AssertionError("Unexpected null type!");
194
seenVariables.incrementAndGet();
196
return super.visitVariable(node, trees);
201
.options("-XDrawDiagnostics")
203
.files(tb.findJavaFiles(src))
205
t.addTaskListener(new TaskListener() {
206
CompilationUnitTree parsed;
208
public void finished(TaskEvent e) {
209
switch (e.getKind()) {
210
case PARSE -> parsed = e.getCompilationUnit();
212
checkTypes.scan(parsed, Trees.instance(t));
217
.run(Task.Expect.FAIL)
219
.getOutputLines(Task.OutputKind.DIRECT);
221
if (seenVariables.get() != 1) {
222
throw new AssertionError("Didn't see enough variables: " + seenVariables);