mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 22:16:11 +00:00
[jdk12] adding support for the new nodes introduced for the improvements to switch statements, and the ‘switch expression’ preview feature, as well as support for the concept of preview features in general.
This commit is contained in:
1 parent
031da25e35
commit
ccaefff69f
6 files changed
+154
-9
No files matched your search
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
public class Switch11 {
|
||||
public void basic() {
|
||||
switch (5) {
|
||||
case 1:
|
||||
case 2:
|
||||
System.out.println("OK");
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// version :11
|
||||
public class Switch11 {
|
||||
public void basic() {
|
||||
switch (5) {
|
||||
case 1:
|
||||
case 2:
|
||||
System.out.println("OK");
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user