[jdk17] Add support for guard/parenthesized pattern

This commit is contained in:
Rawi01 committed 2021-08-18 09:37:21 +02:00
1 parent 91859536db
commit df2601f9db
3 files changed
+86 -1

No files matched your search

@@ -1333,7 +1333,7 @@ public class PrettyPrinter extends JCTree.Visitor {
pats = pat == null ? List.<JCTree>nil() : List.of(pat);
}
if (pats.isEmpty() || pats.get(0).getClass().getName().endsWith("$JCDefaultCaseLabel")) {
if (pats.isEmpty() || pats.size() == 1 && pats.head.getClass().getName().endsWith("$JCDefaultCaseLabel")) {
aPrint("default");
} else {
aPrint("case ");
@@ -1424,6 +1424,22 @@ public class PrettyPrinter extends JCTree.Visitor {
print((Name) readObject(var, "name", null));
}
void printDefaultCase(JCTree tree) {
print("default");
}
void printGuardPattern(JCTree tree) {
print((JCTree) readObject(tree, "patt", null));
print(" && ");
print((JCExpression) readObject(tree, "expr", null));
}
void printParenthesizedPattern(JCTree tree) {
print("(");
print((JCTree) readObject(tree, "pattern", null));
print(")");
}
@Override public void visitTry(JCTry tree) {
aPrint("try ");
List<?> resources = readObject(tree, "resources", List.nil());
@@ -1642,6 +1658,12 @@ public class PrettyPrinter extends JCTree.Visitor {
printYieldExpression(tree);
} else if (className.endsWith("$JCBindingPattern")) { // Introduced as preview in JDK14
printBindingPattern(tree);
} else if (className.endsWith("$JCDefaultCaseLabel")) { // Introduced in JDK17
printDefaultCase(tree);
} else if (className.endsWith("$JCGuardPattern")) { // Introduced in JDK17
printGuardPattern(tree);
} else if (className.endsWith("$JCParenthesizedPattern")) { // Introduced in JDK17
printParenthesizedPattern(tree);
} else {
throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree);
}
+31
View File
@@ -0,0 +1,31 @@
public class Switch17 {
String switchPatternMatching(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
default -> o.toString();
};
}
String switchNull(Object o) {
return switch (o) {
case null, default -> "?";
};
}
String switchGuardPattern(Object o) {
return switch (o) {
case String s && s.length() > 1 -> s;
default -> o.toString();
};
}
String switchParenthesizedPattern(Object o) {
return switch (o) {
case (String s) -> s;
default -> o.toString();
};
}
}
+32
View File
@@ -0,0 +1,32 @@
// version 17:
public class Switch17 {
String switchPatternMatching(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
default -> o.toString();
};
}
String switchNull(Object o) {
return switch (o) {
case null, default -> "?";
};
}
String switchGuardPattern(Object o) {
return switch (o) {
case String s && s.length() > 1 -> s;
default -> o.toString();
};
}
String switchParenthesizedPattern(Object o) {
return switch (o) {
case (String s) -> s;
default -> o.toString();
};
}
}