diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-09-25 01:20:21 +0200 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-09-25 01:20:21 +0200 |
commit | 3c7cfc8c548d2c387971431553afed5791fecdad (patch) | |
tree | 496d0d474f15f63b124c5dcc738f396c775a8fbb | |
parent | 69011b90d8e9d15dbf45479df2f4f08658970a16 (diff) | |
download | lombok-3c7cfc8c548d2c387971431553afed5791fecdad.tar.gz lombok-3c7cfc8c548d2c387971431553afed5791fecdad.tar.bz2 lombok-3c7cfc8c548d2c387971431553afed5791fecdad.zip |
[jdk13] support for switch expression's yield keyword.
-rw-r--r-- | src/delombok/lombok/delombok/PrettyPrinter.java | 26 | ||||
-rw-r--r-- | test/pretty/resource/after/Switch13.java | 55 | ||||
-rw-r--r-- | test/pretty/resource/before/Switch12.java | 2 | ||||
-rw-r--r-- | test/pretty/resource/before/Switch13.java | 56 |
4 files changed, 138 insertions, 1 deletions
diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index e2742a27..fc5eaec2 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -1312,6 +1312,11 @@ public class PrettyPrinter extends JCTree.Visitor { print(";"); needsNewLine = true; needsAlign = true; + } else if (tree.stats.head.getClass().getSimpleName().equals("JCYield")) { + print((JCExpression) readObject(tree.stats.head, "value", null)); + print(";"); + needsNewLine = true; + needsAlign = true; } else { print(tree.stats.head); if (tree.stats.head instanceof JCBlock) needsNewLine = false; @@ -1341,7 +1346,10 @@ public class PrettyPrinter extends JCTree.Visitor { print(")"); } println(" {"); + boolean ruleStyle = isCaseRuleStyle(tree.cases.head); + if (ruleStyle) indent++; print(tree.cases, ""); + if (ruleStyle) indent--; aPrintln("}", tree); } @@ -1357,10 +1365,20 @@ public class PrettyPrinter extends JCTree.Visitor { } println(" {"); List<JCCase> cases = readObject(tree, "cases", null); + boolean ruleStyle = isCaseRuleStyle(cases.head); + if (ruleStyle) indent++; print(cases, ""); + if (ruleStyle) indent--; aPrint("}"); } + void printYieldExpression(JCTree tree) { + aPrint("yield "); + JCExpression value = readObject(tree, "value", null); + print(value); + println(";", tree); + } + @Override public void visitTry(JCTry tree) { aPrint("try "); List<?> resources = readObject(tree, "resources", List.nil()); @@ -1581,11 +1599,19 @@ public class PrettyPrinter extends JCTree.Visitor { // Starting with JDK9, this is inside the import list, but we've already printed it. Just ignore it. } else if ("JCSwitchExpression".equals(simpleName)) { // Introduced as preview feature in JDK12 printSwitchExpression(tree); + } else if ("JCYield".equals(simpleName)) { // Introduced as preview feature in JDK13, part of switch expressions. + printYieldExpression(tree); } else { throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree); } } + private boolean isCaseRuleStyle(JCCase tree) { + if (tree == null) return false; + Enum<?> caseKind = readObject(tree, "caseKind", null); // JDK 12+ + return caseKind != null && caseKind.name().equalsIgnoreCase("RULE"); + } + private boolean jcAnnotatedTypeInit = false; private Class<?> jcAnnotatedTypeClass = null; diff --git a/test/pretty/resource/after/Switch13.java b/test/pretty/resource/after/Switch13.java new file mode 100644 index 00000000..363b48dc --- /dev/null +++ b/test/pretty/resource/after/Switch13.java @@ -0,0 +1,55 @@ +public class Switch13 { + public void basic() { + switch (5) { + case 1: + case 2: + System.out.println("OK"); + break; + default: + } + } + + public void multiCase() { + switch (5) { + case 1, 2: + System.out.println("OK"); + default: + } + } + + public int switchExpr1() { + return switch (5) { + case 1, 2 -> 0; + case 3 -> { + yield 10; + } + default -> 10; + } + 10; + } + + public int switchExpr2() { + return switch (5) { + case 1, 2: + System.out.println("Hello"); + case 3: + yield 10; + default: + yield 20; + } + 10; + } + + public void arrowSwitch() { + switch (5) { + case 1, 2 -> System.out.println("Hello"); + case 3 -> { + System.out.println(""); + break; + } + } + } + + public void emptySwitch() { + switch (5) { + } + } +}
\ No newline at end of file diff --git a/test/pretty/resource/before/Switch12.java b/test/pretty/resource/before/Switch12.java index f1bd8a79..d708f93c 100644 --- a/test/pretty/resource/before/Switch12.java +++ b/test/pretty/resource/before/Switch12.java @@ -1,4 +1,4 @@ -// version 12: +// version 12:12 public class Switch12 { public void basic() { switch (5) { diff --git a/test/pretty/resource/before/Switch13.java b/test/pretty/resource/before/Switch13.java new file mode 100644 index 00000000..fe590788 --- /dev/null +++ b/test/pretty/resource/before/Switch13.java @@ -0,0 +1,56 @@ +// version 13: +public class Switch13 { + public void basic() { + switch (5) { + case 1: + case 2: + System.out.println("OK"); + break; + default: + } + } + + public void multiCase() { + switch (5) { + case 1, 2: + System.out.println("OK"); + default: + } + } + + public int switchExpr1() { + return switch (5) { + case 1, 2 -> 0; + case 3 -> { + yield 10; + } + default -> 10; + } + 10; + } + + public int switchExpr2() { + return switch (5) { + case 1, 2: + System.out.println("Hello"); + case 3: + yield 10; + default: + yield 20; + } + 10; + } + + public void arrowSwitch() { + switch (5) { + case 1, 2 -> System.out.println("Hello"); + case 3 -> { + System.out.println(""); + break; + } + } + } + + public void emptySwitch() { + switch (5) { + } + } +} |