diff options
-rwxr-xr-x | src/delombok/lombok/delombok/Delombok.java | 13 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/PrettyPrinter.java | 69 | ||||
-rw-r--r-- | test/pretty/resource/after/Switch11.java | 11 | ||||
-rw-r--r-- | test/pretty/resource/after/Switch12.java | 28 | ||||
-rw-r--r-- | test/pretty/resource/before/Switch11.java | 12 | ||||
-rw-r--r-- | test/pretty/resource/before/Switch12.java | 30 |
6 files changed, 154 insertions, 9 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index 8f4f99e5..76b2715a 100755 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -93,6 +93,7 @@ public class Delombok { private boolean noCopy; private boolean onlyChanged; private boolean force = false; + private boolean disablePreview; private String classpath, sourcepath, bootclasspath, modulepath; private LinkedHashMap<File, File> fileToBase = new LinkedHashMap<File, File>(); private List<File> filesToParse = new ArrayList<File>(); @@ -158,6 +159,10 @@ public class Delombok { @Description("Output only changed files (implies -n)") private boolean onlyChanged; + @Description("By default lombok enables preview features if available (introduced in JDK 12). With this option, lombok won't do that.") + @FullName("disable-preview") + private boolean disablePreview; + private boolean help; } @@ -281,6 +286,7 @@ public class Delombok { if (args.verbose) delombok.setVerbose(true); if (args.nocopy || args.onlyChanged) delombok.setNoCopy(true); + if (args.disablePreview) delombok.setDisablePreview(true); if (args.onlyChanged) delombok.setOnlyChanged(true); if (args.print) { delombok.setOutputToStandardOut(); @@ -516,6 +522,10 @@ public class Delombok { this.noCopy = noCopy; } + public void setDisablePreview(boolean disablePreview) { + this.disablePreview = disablePreview; + } + public void setOnlyChanged(boolean onlyChanged) { this.onlyChanged = onlyChanged; } @@ -684,6 +694,9 @@ public class Delombok { argsList.add("--module-path"); argsList.add(modulepath); } + + if (!disablePreview && Javac.getJavaCompilerVersion() >= 11) argsList.add("--enable-preview"); + String[] argv = argsList.toArray(new String[0]); args.init("javac", argv); options.put("diags.legacy", "TRUE"); diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index 832dbe0a..3477c51c 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -1046,9 +1046,17 @@ public class PrettyPrinter extends JCTree.Visitor { @Override public void visitBreak(JCBreak tree) { aPrint("break"); - if (tree.label != null) { + + JCExpression value = readObject(tree, "value", null); // JDK 12+ + if (value != null) { print(" "); - print(tree.label); + print(value); + } else { + Name label = readObject(tree, "label", null); + if (label != null) { + print(" "); + print(label); + } } println(";", tree); } @@ -1230,16 +1238,41 @@ public class PrettyPrinter extends JCTree.Visitor { } @Override public void visitCase(JCCase tree) { - if (tree.pat == null) { + // Starting with JDK12, switches allow multiple expressions per case, and can take the form of an expression (preview feature). + + List<JCExpression> pats = readObject(tree, "pats", null); // JDK 12+ + if (pats == null) { + JCExpression pat = readObject(tree, "pat", null); // JDK -11 + pats = pat == null ? List.<JCExpression>nil() : List.of(pat); + } + + if (pats.isEmpty()) { aPrint("default"); } else { aPrint("case "); - print(tree.pat); + print(pats, ", "); + } + + Enum<?> caseKind = readObject(tree, "caseKind", null); // JDK 12+ + + if (caseKind != null && caseKind.name().equalsIgnoreCase("RULE")) { + print(" -> "); + if (tree.stats.head instanceof JCBreak) { + JCBreak b = (JCBreak) tree.stats.head; + print((JCExpression) readObject(b, "value", null)); + print(";"); + needsNewLine = true; + needsAlign = true; + } else { + print(tree.stats.head); + if (tree.stats.head instanceof JCBlock) needsNewLine = false; + } + } else { + println(": "); + indent++; + print(tree.stats, ""); + indent--; } - println(": "); - indent++; - print(tree.stats, ""); - indent--; } @Override public void visitCatch(JCCatch tree) { @@ -1259,10 +1292,26 @@ public class PrettyPrinter extends JCTree.Visitor { print(")"); } println(" {"); - print(tree.cases, "\n"); + print(tree.cases, ""); aPrintln("}", tree); } + void printSwitchExpression(JCTree tree) { + aPrint("switch "); + JCExpression selector = readObject(tree, "selector", null); + if (selector instanceof JCParens) { + print(selector); + } else { + print("("); + print(selector); + print(")"); + } + println(" {"); + List<JCCase> cases = readObject(tree, "cases", null); + print(cases, ""); + aPrint("}"); + } + @Override public void visitTry(JCTry tree) { aPrint("try "); List<?> resources = readObject(tree, "resources", List.nil()); @@ -1481,6 +1530,8 @@ public class PrettyPrinter extends JCTree.Visitor { printAnnotatedType0(tree); } else if ("JCPackageDecl".equals(simpleName)) { // 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 { throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree); } diff --git a/test/pretty/resource/after/Switch11.java b/test/pretty/resource/after/Switch11.java new file mode 100644 index 00000000..d24012a2 --- /dev/null +++ b/test/pretty/resource/after/Switch11.java @@ -0,0 +1,11 @@ +public class Switch11 { + public void basic() { + switch (5) { + case 1: + case 2: + System.out.println("OK"); + break; + default: + } + } +} diff --git a/test/pretty/resource/after/Switch12.java b/test/pretty/resource/after/Switch12.java new file mode 100644 index 00000000..89825223 --- /dev/null +++ b/test/pretty/resource/after/Switch12.java @@ -0,0 +1,28 @@ +public class Switch12 { + 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 switchExpr() { + return switch (5) { + case 1, 2 -> 0; + case 3 -> { + break 10; + } + default -> 10; + } + 10; + } +} diff --git a/test/pretty/resource/before/Switch11.java b/test/pretty/resource/before/Switch11.java new file mode 100644 index 00000000..556631f0 --- /dev/null +++ b/test/pretty/resource/before/Switch11.java @@ -0,0 +1,12 @@ +// version :11 +public class Switch11 { + public void basic() { + switch (5) { + case 1: + case 2: + System.out.println("OK"); + break; + default: + } + } +} diff --git a/test/pretty/resource/before/Switch12.java b/test/pretty/resource/before/Switch12.java new file mode 100644 index 00000000..f1bd8a79 --- /dev/null +++ b/test/pretty/resource/before/Switch12.java @@ -0,0 +1,30 @@ +// version 12: +public class Switch12 { + 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 switchExpr() { + return switch (5) { + case 1, 2 -> 0; + case 3 -> { + break 10; + } + default -> 10; + } + 10; + } +} |