|
23 | 23 |
|
24 | 24 | /* |
25 | 25 | * @test |
26 | | - * @bug 8301580 8322159 8333107 8332230 |
| 26 | + * @bug 8301580 8322159 8333107 8332230 8338678 |
27 | 27 | * @summary Verify error recovery w.r.t. Attr |
28 | 28 | * @library /tools/lib |
29 | 29 | * @enablePreview |
|
34 | 34 | * @run main AttrRecovery |
35 | 35 | */ |
36 | 36 |
|
| 37 | +import com.sun.source.tree.VariableTree; |
| 38 | +import com.sun.source.util.TaskEvent; |
| 39 | +import com.sun.source.util.TaskListener; |
| 40 | +import com.sun.source.util.TreePathScanner; |
| 41 | +import com.sun.source.util.Trees; |
37 | 42 | import java.nio.file.Path; |
| 43 | +import java.util.IdentityHashMap; |
38 | 44 | import java.util.List; |
| 45 | +import java.util.Map; |
39 | 46 | import java.util.Objects; |
| 47 | +import java.util.stream.Collectors; |
| 48 | +import javax.lang.model.element.Element; |
| 49 | +import javax.lang.model.element.VariableElement; |
| 50 | +import javax.lang.model.type.DeclaredType; |
| 51 | +import javax.lang.model.type.TypeKind; |
| 52 | +import javax.lang.model.type.TypeMirror; |
| 53 | +import javax.tools.Diagnostic; |
40 | 54 |
|
41 | 55 | import toolbox.JavacTask; |
42 | 56 | import toolbox.Task.Expect; |
@@ -234,4 +248,80 @@ public Undefined g(Undefined u) { |
234 | 248 | } |
235 | 249 | } |
236 | 250 |
|
| 251 | + @Test |
| 252 | + public void testParameterizedErroneousType() throws Exception { |
| 253 | + String code = """ |
| 254 | + public class C { |
| 255 | + Undefined1<Undefined2, Undefined3> variable1; |
| 256 | + } |
| 257 | + """; |
| 258 | + Path curPath = Path.of("."); |
| 259 | + List<String> actual = new JavacTask(tb) |
| 260 | + .options("-XDrawDiagnostics") |
| 261 | + .sources(code) |
| 262 | + .outdir(curPath) |
| 263 | + .callback(task -> { |
| 264 | + task.addTaskListener(new TaskListener() { |
| 265 | + @Override |
| 266 | + public void finished(TaskEvent e) { |
| 267 | + Trees trees = Trees.instance(task); |
| 268 | + |
| 269 | + if (e.getKind() == TaskEvent.Kind.ANALYZE) { |
| 270 | + new TreePathScanner<Void, Void>() { |
| 271 | + @Override |
| 272 | + public Void visitVariable(VariableTree tree, Void p) { |
| 273 | + VariableElement var = (VariableElement) trees.getElement(getCurrentPath()); |
| 274 | + |
| 275 | + trees.printMessage(Diagnostic.Kind.NOTE, type2String(var.asType()), tree, e.getCompilationUnit()); |
| 276 | + |
| 277 | + return super.visitVariable(tree, p); |
| 278 | + } |
| 279 | + }.scan(e.getCompilationUnit(), null); |
| 280 | + } |
| 281 | + } |
| 282 | + Map<Element, Integer> identityRename = new IdentityHashMap<>(); |
| 283 | + String type2String(TypeMirror type) { |
| 284 | + StringBuilder result = new StringBuilder(); |
| 285 | + |
| 286 | + result.append(type.getKind()); |
| 287 | + result.append(":"); |
| 288 | + result.append(type.toString()); |
| 289 | + |
| 290 | + if (type.getKind() == TypeKind.DECLARED || |
| 291 | + type.getKind() == TypeKind.ERROR) { |
| 292 | + DeclaredType dt = (DeclaredType) type; |
| 293 | + Element el = task.getTypes().asElement(dt); |
| 294 | + result.append(":"); |
| 295 | + result.append(el.toString()); |
| 296 | + if (!dt.getTypeArguments().isEmpty()) { |
| 297 | + result.append(dt.getTypeArguments() |
| 298 | + .stream() |
| 299 | + .map(tm -> type2String(tm)) |
| 300 | + .collect(Collectors.joining(", ", "<", ">"))); |
| 301 | + } |
| 302 | + } else { |
| 303 | + throw new AssertionError(type.getKind().name()); |
| 304 | + } |
| 305 | + |
| 306 | + return result.toString(); |
| 307 | + } |
| 308 | + }); |
| 309 | + }) |
| 310 | + .run(Expect.FAIL) |
| 311 | + .writeAll() |
| 312 | + .getOutputLines(OutputKind.DIRECT); |
| 313 | + |
| 314 | + List<String> expected = List.of( |
| 315 | + "C.java:2:5: compiler.err.cant.resolve.location: kindname.class, Undefined1, , , (compiler.misc.location: kindname.class, C, null)", |
| 316 | + "C.java:2:16: compiler.err.cant.resolve.location: kindname.class, Undefined2, , , (compiler.misc.location: kindname.class, C, null)", |
| 317 | + "C.java:2:28: compiler.err.cant.resolve.location: kindname.class, Undefined3, , , (compiler.misc.location: kindname.class, C, null)", |
| 318 | + "C.java:2:40: compiler.note.proc.messager: ERROR:Undefined1<Undefined2,Undefined3>:Undefined1<ERROR:Undefined2:Undefined2, ERROR:Undefined3:Undefined3>", |
| 319 | + "3 errors" |
| 320 | + ); |
| 321 | + |
| 322 | + if (!Objects.equals(actual, expected)) { |
| 323 | + error("Expected: " + expected + ", but got: " + actual); |
| 324 | + } |
| 325 | + } |
| 326 | + |
237 | 327 | } |
0 commit comments