2021-06-07 07:01:30 +00:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @modules jdk.compiler/com.sun.tools.javac.file
|
|
|
|
* jdk.compiler/com.sun.tools.javac.main
|
|
|
|
* jdk.compiler/com.sun.tools.javac.parser
|
|
|
|
* jdk.compiler/com.sun.tools.javac.tree
|
|
|
|
* jdk.compiler/com.sun.tools.javac.util
|
|
|
|
* @compile --enable-preview -source ${jdk.version} DisambiguateParenthesizedPattern.java
|
|
|
|
* @run main/othervm --enable-preview DisambiguateParenthesizedPattern
|
|
|
|
*/
|
|
|
|
|
|
|
|
import com.sun.source.tree.CaseLabelTree;
|
|
|
|
import com.sun.source.tree.ClassTree;
|
|
|
|
import com.sun.source.tree.CompilationUnitTree;
|
|
|
|
import com.sun.source.tree.ExpressionTree;
|
|
|
|
import com.sun.source.tree.MethodTree;
|
|
|
|
import com.sun.source.tree.PatternTree;
|
|
|
|
import com.sun.source.tree.SwitchTree;
|
|
|
|
import com.sun.tools.javac.file.JavacFileManager;
|
|
|
|
import com.sun.tools.javac.parser.JavacParser;
|
|
|
|
import com.sun.tools.javac.parser.ParserFactory;
|
|
|
|
import com.sun.tools.javac.util.Context;
|
|
|
|
import com.sun.tools.javac.main.Option;
|
|
|
|
import com.sun.tools.javac.util.Options;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
|
|
|
public class DisambiguateParenthesizedPattern {
|
|
|
|
|
|
|
|
public static void main(String... args) throws Throwable {
|
|
|
|
DisambiguateParenthesizedPattern test = new DisambiguateParenthesizedPattern();
|
|
|
|
test.disambiguationTest("String s",
|
|
|
|
ExpressionType.PATTERN);
|
|
|
|
test.disambiguationTest("String s && s.isEmpty()",
|
|
|
|
ExpressionType.PATTERN);
|
|
|
|
test.disambiguationTest("(String s)",
|
|
|
|
ExpressionType.PATTERN);
|
|
|
|
test.disambiguationTest("((String s))",
|
|
|
|
ExpressionType.PATTERN);
|
|
|
|
test.disambiguationTest("(String) s",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("((String) s)",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("((0x1))",
|
|
|
|
ExpressionType.EXPRESSION);
|
2021-06-23 10:16:42 +00:00
|
|
|
test.disambiguationTest("(a > b)",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("(a >> b)",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("(a >>> b)",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("(a < b | a > b)",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("(a << b | a >> b)",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("(a << b || a < b | a >>> b)",
|
|
|
|
ExpressionType.EXPRESSION);
|
|
|
|
test.disambiguationTest("(a < c.d > b)",
|
|
|
|
ExpressionType.PATTERN);
|
2021-06-07 07:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private final ParserFactory factory;
|
|
|
|
|
|
|
|
public DisambiguateParenthesizedPattern() {
|
|
|
|
Context context = new Context();
|
|
|
|
JavacFileManager jfm = new JavacFileManager(context, true, Charset.defaultCharset());
|
|
|
|
Options.instance(context).put(Option.PREVIEW, "");
|
|
|
|
factory = ParserFactory.instance(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void disambiguationTest(String snippet, ExpressionType expectedType) {
|
|
|
|
String code = """
|
|
|
|
public class Test {
|
|
|
|
private void test() {
|
|
|
|
switch (null) {
|
|
|
|
case SNIPPET -> {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""".replace("SNIPPET", snippet);
|
|
|
|
JavacParser parser = factory.newParser(code, false, false, false);
|
|
|
|
CompilationUnitTree result = parser.parseCompilationUnit();
|
|
|
|
ClassTree clazz = (ClassTree) result.getTypeDecls().get(0);
|
|
|
|
MethodTree method = (MethodTree) clazz.getMembers().get(0);
|
|
|
|
SwitchTree st = (SwitchTree) method.getBody().getStatements().get(0);
|
|
|
|
CaseLabelTree label = st.getCases().get(0).getLabels().get(0);
|
|
|
|
ExpressionType actualType = switch (label) {
|
|
|
|
case ExpressionTree et -> ExpressionType.EXPRESSION;
|
|
|
|
case PatternTree pt -> ExpressionType.PATTERN;
|
|
|
|
default -> throw new AssertionError("Unexpected result: " + result);
|
|
|
|
};
|
|
|
|
if (expectedType != actualType) {
|
|
|
|
throw new AssertionError("Expected: " + expectedType + ", actual: " + actualType +
|
|
|
|
", for: " + code + ", parsed: " + result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ExpressionType {
|
|
|
|
PATTERN,
|
|
|
|
EXPRESSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|