6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8205418 8207229 8207230 8230847 8245786 8247334
27 * @summary Test the outcomes from Trees.getScope
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 * jdk.compiler/com.sun.tools.javac.comp
30 * jdk.compiler/com.sun.tools.javac.tree
31 * jdk.compiler/com.sun.tools.javac.util
32 */
33
34 import java.io.IOException;
35 import java.net.URI;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import javax.lang.model.element.Element;
40 import javax.tools.JavaCompiler;
41 import javax.tools.SimpleJavaFileObject;
42 import javax.tools.StandardJavaFileManager;
43 import javax.tools.ToolProvider;
44
45 import com.sun.source.tree.AnnotationTree;
46 import com.sun.source.tree.BlockTree;
47 import com.sun.source.tree.ClassTree;
48 import com.sun.source.tree.CompilationUnitTree;
49 import com.sun.source.tree.ConditionalExpressionTree;
50 import com.sun.source.tree.IdentifierTree;
51 import com.sun.source.tree.LambdaExpressionTree;
52 import com.sun.source.tree.MethodInvocationTree;
53 import com.sun.source.tree.MethodTree;
54 import com.sun.source.tree.Scope;
55 import com.sun.source.tree.Tree;
56 import com.sun.source.tree.VariableTree;
57 import com.sun.source.util.JavacTask;
58 import com.sun.source.util.TaskEvent;
59 import com.sun.source.util.TaskListener;
60 import com.sun.source.util.TreePath;
61 import com.sun.source.util.TreePathScanner;
62 import com.sun.source.util.Trees;
63
64 import com.sun.tools.javac.api.JavacTool;
65 import com.sun.tools.javac.comp.Analyzer;
66 import com.sun.tools.javac.comp.AttrContext;
67 import com.sun.tools.javac.comp.Env;
68 import com.sun.tools.javac.tree.JCTree.JCStatement;
69 import com.sun.tools.javac.util.Context;
70 import com.sun.tools.javac.util.Context.Factory;
71
72 import static javax.tools.JavaFileObject.Kind.SOURCE;
73
74 public class TestGetScopeResult {
75 public static void main(String... args) throws IOException {
76 new TestGetScopeResult().run();
77 new TestGetScopeResult().testAnalyzerDisabled();
78 new TestGetScopeResult().testVariablesInSwitch();
79 new TestGetScopeResult().testMemberRefs();
80 new TestGetScopeResult().testAnnotations();
81 new TestGetScopeResult().testAnnotationsLazy();
82 new TestGetScopeResult().testCircular();
83 new TestGetScopeResult().testRecord();
84 new TestGetScopeResult().testLocalRecordAnnotation();
85 }
86
87 public void run() throws IOException {
88 String[] simpleLambda = {
89 "s:java.lang.String",
90 "i:Test.I",
91 "super:java.lang.Object",
92 "this:Test"
93 };
94 doTest("class Test { void test() { I i = s -> { }; } interface I { public void test(String s); } }",
95 simpleLambda);
96 doTest("class Test { void test() { I i = s -> { }; } interface I { public int test(String s); } }",
97 simpleLambda);
98 doTest("class Test { void test() { I i = s -> { }; } interface I { public String test(String s); } }",
99 simpleLambda);
100 doTest("class Test { void test() { I i; inv(s -> { }); } void inv(I i) { } interface I { public void test(String s); } }",
101 simpleLambda);
102 doTest("class Test { void test() { I i; inv(s -> { }); } void inv(I i) { } interface I { public int test(String s); } }",
103 simpleLambda);
104 doTest("class Test { void test() { I i; inv(s -> { }); } void inv(I i) { } interface I { public String test(String s); } }",
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
639 private List<String> dumpScope(Scope scope) {
640 List<String> content = new ArrayList<>();
641 while (scope.getEnclosingClass() != null) {
642 for (Element el : scope.getLocalElements()) {
643 content.add(el.getSimpleName() + ":" +el.asType().toString());
644 }
645 scope = scope.getEnclosingScope();
646 }
647 return content;
648 }
649 }
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8205418 8207229 8207230 8230847 8245786 8247334 8248641
27 * @summary Test the outcomes from Trees.getScope
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 * jdk.compiler/com.sun.tools.javac.comp
30 * jdk.compiler/com.sun.tools.javac.tree
31 * jdk.compiler/com.sun.tools.javac.util
32 */
33
34 import java.io.IOException;
35 import java.net.URI;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39
40 import javax.lang.model.element.Element;
41 import javax.tools.JavaCompiler;
42 import javax.tools.SimpleJavaFileObject;
43 import javax.tools.StandardJavaFileManager;
44 import javax.tools.ToolProvider;
45
46 import com.sun.source.tree.AnnotationTree;
47 import com.sun.source.tree.BlockTree;
48 import com.sun.source.tree.ClassTree;
49 import com.sun.source.tree.CompilationUnitTree;
50 import com.sun.source.tree.ConditionalExpressionTree;
51 import com.sun.source.tree.IdentifierTree;
52 import com.sun.source.tree.LambdaExpressionTree;
53 import com.sun.source.tree.MethodInvocationTree;
54 import com.sun.source.tree.MethodTree;
55 import com.sun.source.tree.Scope;
56 import com.sun.source.tree.Tree;
57 import com.sun.source.tree.VariableTree;
58 import com.sun.source.util.JavacTask;
59 import com.sun.source.util.TaskEvent;
60 import com.sun.source.util.TaskListener;
61 import com.sun.source.util.TreePath;
62 import com.sun.source.util.TreePathScanner;
63 import com.sun.source.util.Trees;
64 import com.sun.tools.javac.api.JavacScope;
65
66 import com.sun.tools.javac.api.JavacTool;
67 import com.sun.tools.javac.comp.Analyzer;
68 import com.sun.tools.javac.comp.AttrContext;
69 import com.sun.tools.javac.comp.Env;
70 import com.sun.tools.javac.tree.JCTree;
71 import com.sun.tools.javac.tree.JCTree.JCCase;
72 import com.sun.tools.javac.tree.JCTree.JCStatement;
73 import com.sun.tools.javac.util.Context;
74 import com.sun.tools.javac.util.Context.Factory;
75
76 import static javax.tools.JavaFileObject.Kind.SOURCE;
77
78 public class TestGetScopeResult {
79 public static void main(String... args) throws IOException {
80 new TestGetScopeResult().run();
81 new TestGetScopeResult().testAnalyzerDisabled();
82 new TestGetScopeResult().testVariablesInSwitch();
83 new TestGetScopeResult().testMemberRefs();
84 new TestGetScopeResult().testAnnotations();
85 new TestGetScopeResult().testAnnotationsLazy();
86 new TestGetScopeResult().testCircular();
87 new TestGetScopeResult().testRecord();
88 new TestGetScopeResult().testLocalRecordAnnotation();
89 new TestGetScopeResult().testRuleCases();
90 }
91
92 public void run() throws IOException {
93 String[] simpleLambda = {
94 "s:java.lang.String",
95 "i:Test.I",
96 "super:java.lang.Object",
97 "this:Test"
98 };
99 doTest("class Test { void test() { I i = s -> { }; } interface I { public void test(String s); } }",
100 simpleLambda);
101 doTest("class Test { void test() { I i = s -> { }; } interface I { public int test(String s); } }",
102 simpleLambda);
103 doTest("class Test { void test() { I i = s -> { }; } interface I { public String test(String s); } }",
104 simpleLambda);
105 doTest("class Test { void test() { I i; inv(s -> { }); } void inv(I i) { } interface I { public void test(String s); } }",
106 simpleLambda);
107 doTest("class Test { void test() { I i; inv(s -> { }); } void inv(I i) { } interface I { public int test(String s); } }",
108 simpleLambda);
109 doTest("class Test { void test() { I i; inv(s -> { }); } void inv(I i) { } interface I { public String test(String s); } }",
620 "-source", System.getProperty("java.specification.version"));
621 JavacTask t = (JavacTask) c.getTask(null, fm, null, options, null,
622 List.of(new MyFileObject()), ctx);
623 CompilationUnitTree cut = t.parse().iterator().next();
624 t.analyze();
625
626 List<List<String>> actual = new ArrayList<>();
627
628 new TreePathScanner<Void, Void>() {
629 @Override
630 public Void visitAnnotation(AnnotationTree node, Void p) {
631 Scope scope = Trees.instance(t).getScope(getCurrentPath());
632 actual.add(dumpScope(scope));
633 return super.visitAnnotation(node, p);
634 }
635 }.scan(cut, null);
636
637 if (!currentVariant.expectedScopeContent.equals(actual)) {
638 throw new AssertionError("Unexpected Scope content: " + actual);
639 }
640 }
641 }
642 }
643
644 void testRuleCases() throws IOException {
645 JavacTool c = JavacTool.create();
646 try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
647 String code = """
648 class Test {
649 void t(int i) {
650 long local;
651 System.err.println(switch (i) {
652 case 0 -> {
653 String var;
654 int scopeHere;
655 yield "";
656 }
657 default -> {
658 String var;
659 int scopeHere;
660 yield "";
661 }
662 });
663 switch (i) {
664 case 0 -> {
665 String var;
666 int scopeHere;
667 }
668 default -> {
669 String var;
670 int scopeHere;
671 }
672 };
673 switch (i) {
674 case 0: {
675 int checkTree;
676 }
677 }
678 }
679 }
680 """;
681 class MyFileObject extends SimpleJavaFileObject {
682 MyFileObject() {
683 super(URI.create("myfo:///Test.java"), SOURCE);
684 }
685 @Override
686 public String getCharContent(boolean ignoreEncodingErrors) {
687 return code;
688 }
689 }
690 Context ctx = new Context();
691 TestAnalyzer.preRegister(ctx);
692 List<String> options = List.of("--enable-preview",
693 "-source", System.getProperty("java.specification.version"));
694 JavacTask t = (JavacTask) c.getTask(null, fm, null, options, null,
695 List.of(new MyFileObject()), ctx);
696 CompilationUnitTree cut = t.parse().iterator().next();
697 t.analyze();
698
699 List<List<String>> actual = new ArrayList<>();
700
701 new TreePathScanner<Void, Void>() {
702 @Override
703 public Void visitVariable(VariableTree node, Void p) {
704 if (node.getName().contentEquals("scopeHere")) {
705 Scope scope = Trees.instance(t).getScope(getCurrentPath());
706 actual.add(dumpScope(scope));
707 JCTree body = getCaseBody(scope);
708 if (body == null) {
709 throw new AssertionError("Unexpected null body.");
710 }
711 } else if (node.getName().contentEquals("checkTree")) {
712 Scope scope = Trees.instance(t).getScope(getCurrentPath());
713 JCTree body = getCaseBody(scope);
714 if (body != null) {
715 throw new AssertionError("Unexpected body tree: " + body);
716 }
717 }
718 return super.visitVariable(node, p);
719 }
720 JCTree getCaseBody(Scope scope) {
721 return ((JCCase) ((JavacScope) scope).getEnv().next.next.tree).body;
722 }
723 }.scan(cut, null);
724
725 List<List<String>> expected =
726 Collections.nCopies(4,
727 List.of("scopeHere:int",
728 "var:java.lang.String",
729 "local:long",
730 "i:int",
731 "super:java.lang.Object",
732 "this:Test"
733 ));
734
735 if (!expected.equals(actual)) {
736 throw new AssertionError("Unexpected Scope content: " + actual);
737 }
738 }
739 }
740
741 private List<String> dumpScope(Scope scope) {
742 List<String> content = new ArrayList<>();
743 while (scope.getEnclosingClass() != null) {
744 for (Element el : scope.getLocalElements()) {
745 content.add(el.getSimpleName() + ":" +el.asType().toString());
746 }
747 scope = scope.getEnclosingScope();
748 }
749 return content;
750 }
751 }
|