< prev index next >

test/langtools/tools/javac/api/TestGetScopeResult.java

Print this page
rev 60024 : imported patch 8248641

*** 21,41 **** * questions. */ /* * @test ! * @bug 8205418 8207229 8207230 8230847 8245786 8247334 * @summary Test the outcomes from Trees.getScope * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.comp * jdk.compiler/com.sun.tools.javac.tree * jdk.compiler/com.sun.tools.javac.util */ import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; import javax.lang.model.element.Element; import javax.tools.JavaCompiler; import javax.tools.SimpleJavaFileObject; --- 21,42 ---- * questions. */ /* * @test ! * @bug 8205418 8207229 8207230 8230847 8245786 8247334 8248641 * @summary Test the outcomes from Trees.getScope * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.comp * jdk.compiler/com.sun.tools.javac.tree * jdk.compiler/com.sun.tools.javac.util */ import java.io.IOException; import java.net.URI; import java.util.ArrayList; + import java.util.Collections; import java.util.List; import javax.lang.model.element.Element; import javax.tools.JavaCompiler; import javax.tools.SimpleJavaFileObject;
*** 80,89 **** --- 81,91 ---- new TestGetScopeResult().testAnnotations(); new TestGetScopeResult().testAnnotationsLazy(); new TestGetScopeResult().testCircular(); new TestGetScopeResult().testRecord(); new TestGetScopeResult().testLocalRecordAnnotation(); + new TestGetScopeResult().testRuleCases(); } public void run() throws IOException { String[] simpleLambda = { "s:java.lang.String",
*** 634,643 **** --- 636,724 ---- } } } } + void testRuleCases() throws IOException { + JavacTool c = JavacTool.create(); + try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) { + String code = """ + class Test { + void t(int i) { + long local; + System.err.println(switch (i) { + case 0 -> { + String var; + int scopeHere; + yield ""; + } + default -> { + String var; + int scopeHere; + yield ""; + } + }); + switch (i) { + case 0 -> { + String var; + int scopeHere; + } + default -> { + String var; + int scopeHere; + } + }; + } + } + """; + class MyFileObject extends SimpleJavaFileObject { + MyFileObject() { + super(URI.create("myfo:///Test.java"), SOURCE); + } + @Override + public String getCharContent(boolean ignoreEncodingErrors) { + return code; + } + } + Context ctx = new Context(); + TestAnalyzer.preRegister(ctx); + List<String> options = List.of("--enable-preview", + "-source", System.getProperty("java.specification.version")); + JavacTask t = (JavacTask) c.getTask(null, fm, null, options, null, + List.of(new MyFileObject()), ctx); + CompilationUnitTree cut = t.parse().iterator().next(); + t.analyze(); + + List<List<String>> actual = new ArrayList<>(); + + new TreePathScanner<Void, Void>() { + @Override + public Void visitVariable(VariableTree node, Void p) { + if (node.getName().contentEquals("scopeHere")) { + Scope scope = Trees.instance(t).getScope(getCurrentPath()); + actual.add(dumpScope(scope)); + } + return super.visitVariable(node, p); + } + }.scan(cut, null); + + List<List<String>> expected = + Collections.nCopies(4, + List.of("scopeHere:int", + "var:java.lang.String", + "local:long", + "i:int", + "super:java.lang.Object", + "this:Test" + )); + + if (!expected.equals(actual)) { + throw new AssertionError("Unexpected Scope content: " + actual); + } + } + } + private List<String> dumpScope(Scope scope) { List<String> content = new ArrayList<>(); while (scope.getEnclosingClass() != null) { for (Element el : scope.getLocalElements()) { content.add(el.getSimpleName() + ":" +el.asType().toString());
< prev index next >