[jdk19] Add support for new guard pattern

This commit is contained in:
Rawi01 committed 2022-08-26 00:53:21 +02:00
1 parent 646723f6d3
commit 957adfae2e
4 files changed
+77 -1

No files matched your search

@@ -1452,6 +1452,15 @@ public class PrettyPrinter extends JCTree.Visitor {
print((JCTree) readObject(tree, "expr", null));
}
void printPatternCaseLabel(JCTree tree) {
print((JCTree) readObject(tree, "pat", null));
JCTree guard = readObject(tree, "guard", null);
if (guard != null) {
print(" when ");
print(guard);
}
}
@Override public void visitTry(JCTry tree) {
aPrint("try ");
List<?> resources = readObject(tree, "resources", List.nil());
@@ -1678,6 +1687,8 @@ public class PrettyPrinter extends JCTree.Visitor {
printParenthesizedPattern(tree);
} else if (className.endsWith("$JCConstantCaseLabel")) { // Introduced in JDK19
printConstantCaseLabel(tree);
} else if (className.endsWith("$JCPatternCaseLabel")) { // Introduced in JDK19
printPatternCaseLabel(tree);
} else {
throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree);
}
+32
View File
@@ -0,0 +1,32 @@
public class Switch19 {
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 when s.length() > 1 -> s;
case String s -> s;
default -> o.toString();
};
}
String switchParenthesizedPattern(Object o) {
return switch (o) {
case (String s) -> s;
default -> o.toString();
};
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
// version 17:
// version 17:18
public class Switch17 {
String switchPatternMatching(Object o) {
return switch (o) {
+33
View File
@@ -0,0 +1,33 @@
// version 19:
public class Switch19 {
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 when s.length() > 1 -> s;
case String s -> s;
default -> o.toString();
};
}
String switchParenthesizedPattern(Object o) {
return switch (o) {
case (String s) -> s;
default -> o.toString();
};
}
}