|
23 | 23 |
|
24 | 24 | /*
|
25 | 25 | * @test
|
26 |
| - * @bug 8205418 8207229 8207230 8230847 8245786 |
| 26 | + * @bug 8205418 8207229 8207230 8230847 8245786 8247334 |
27 | 27 | * @summary Test the outcomes from Trees.getScope
|
28 | 28 | * @modules jdk.compiler/com.sun.tools.javac.api
|
29 | 29 | * jdk.compiler/com.sun.tools.javac.comp
|
|
42 | 42 | import javax.tools.StandardJavaFileManager;
|
43 | 43 | import javax.tools.ToolProvider;
|
44 | 44 |
|
| 45 | +import com.sun.source.tree.AnnotationTree; |
45 | 46 | import com.sun.source.tree.BlockTree;
|
46 | 47 | import com.sun.source.tree.ClassTree;
|
47 | 48 | import com.sun.source.tree.CompilationUnitTree;
|
@@ -80,6 +81,7 @@ public static void main(String... args) throws IOException {
|
80 | 81 | new TestGetScopeResult().testAnnotationsLazy();
|
81 | 82 | new TestGetScopeResult().testCircular();
|
82 | 83 | new TestGetScopeResult().testRecord();
|
| 84 | + new TestGetScopeResult().testLocalRecordAnnotation(); |
83 | 85 | }
|
84 | 86 |
|
85 | 87 | public void run() throws IOException {
|
@@ -562,6 +564,78 @@ public Void visitClass(ClassTree node, Void p) {
|
562 | 564 | }
|
563 | 565 | }
|
564 | 566 |
|
| 567 | + void testLocalRecordAnnotation() throws IOException { |
| 568 | + JavacTool c = JavacTool.create(); |
| 569 | + try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) { |
| 570 | + class Variant { |
| 571 | + final String code; |
| 572 | + final List<List<String>> expectedScopeContent; |
| 573 | + public Variant(String code, List<List<String>> expectedScopeContent) { |
| 574 | + this.code = code; |
| 575 | + this.expectedScopeContent = expectedScopeContent; |
| 576 | + } |
| 577 | + } |
| 578 | + Variant[] variants = new Variant[] { |
| 579 | + new Variant(""" |
| 580 | + class Test { |
| 581 | + void t() { |
| 582 | + record R(@Annotation int i) { |
| 583 | + void stop () {} |
| 584 | + } |
| 585 | + } |
| 586 | + } |
| 587 | + @interface Annotation {} |
| 588 | + """, |
| 589 | + List.of( |
| 590 | + List.of("super:java.lang.Object", "this:Test"), |
| 591 | + List.of("super:java.lang.Object", "this:Test") |
| 592 | + )), |
| 593 | + new Variant(""" |
| 594 | + record Test(@Annotation int i) {} |
| 595 | + @interface Annotation {} |
| 596 | + """, |
| 597 | + List.of( |
| 598 | + List.of("i:int", "super:java.lang.Record", "this:Test"), |
| 599 | + List.of("super:java.lang.Record", "this:Test") |
| 600 | + )) |
| 601 | + }; |
| 602 | + for (Variant currentVariant : variants) { |
| 603 | + class MyFileObject extends SimpleJavaFileObject { |
| 604 | + MyFileObject() { |
| 605 | + super(URI.create("myfo:///Test.java"), SOURCE); |
| 606 | + } |
| 607 | + @Override |
| 608 | + public String getCharContent(boolean ignoreEncodingErrors) { |
| 609 | + return currentVariant.code; |
| 610 | + } |
| 611 | + } |
| 612 | + Context ctx = new Context(); |
| 613 | + TestAnalyzer.preRegister(ctx); |
| 614 | + List<String> options = List.of("--enable-preview", |
| 615 | + "-source", System.getProperty("java.specification.version")); |
| 616 | + JavacTask t = (JavacTask) c.getTask(null, fm, null, options, null, |
| 617 | + List.of(new MyFileObject()), ctx); |
| 618 | + CompilationUnitTree cut = t.parse().iterator().next(); |
| 619 | + t.analyze(); |
| 620 | + |
| 621 | + List<List<String>> actual = new ArrayList<>(); |
| 622 | + |
| 623 | + new TreePathScanner<Void, Void>() { |
| 624 | + @Override |
| 625 | + public Void visitAnnotation(AnnotationTree node, Void p) { |
| 626 | + Scope scope = Trees.instance(t).getScope(getCurrentPath()); |
| 627 | + actual.add(dumpScope(scope)); |
| 628 | + return super.visitAnnotation(node, p); |
| 629 | + } |
| 630 | + }.scan(cut, null); |
| 631 | + |
| 632 | + if (!currentVariant.expectedScopeContent.equals(actual)) { |
| 633 | + throw new AssertionError("Unexpected Scope content: " + actual); |
| 634 | + } |
| 635 | + } |
| 636 | + } |
| 637 | + } |
| 638 | + |
565 | 639 | private List<String> dumpScope(Scope scope) {
|
566 | 640 | List<String> content = new ArrayList<>();
|
567 | 641 | while (scope.getEnclosingClass() != null) {
|
|
0 commit comments