Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JDK 19 #3251

Merged
merged 4 commits into from Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildScripts/ivy.xml
Expand Up @@ -37,7 +37,7 @@
</configurations>
<dependencies>

<dependency org="org.projectlombok" name="lombok.patcher" rev="0.44" conf="build,stripe->default" />
<dependency org="org.projectlombok" name="lombok.patcher" rev="0.46" conf="build,stripe->default" />
<dependency org="zwitserloot.com" name="cmdreader" rev="1.2" conf="build,stripe->runtime" />
<dependency org="org.apache.ant" name="ant" rev="1.10.5" conf="build->default" />
<dependency org="org.apache.ant" name="ant-junit" rev="1.10.5" conf="build->default" />
Expand Down
31 changes: 31 additions & 0 deletions src/delombok/lombok/delombok/PrettyPrinter.java
Expand Up @@ -1448,6 +1448,31 @@ void printParenthesizedPattern(JCTree tree) {
print(")");
}

void printConstantCaseLabel(JCTree tree) {
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);
}
}

void printRecordPattern(JCTree tree) {
print((JCTree) readObject(tree, "deconstructor", null));
print("(");
print(readObject(tree, "nested", List.<JCTree>nil()), ", ");
print(")");
JCVariableDecl var = readObject(tree, "var", null);
if (var != null) {
print(" ");
print(var.name);
}
}

@Override public void visitTry(JCTry tree) {
aPrint("try ");
List<?> resources = readObject(tree, "resources", List.nil());
Expand Down Expand Up @@ -1672,6 +1697,12 @@ public void visitTypeBoundKind(TypeBoundKind tree) {
printGuardPattern(tree);
} else if (className.endsWith("$JCParenthesizedPattern")) { // Introduced in JDK17
printParenthesizedPattern(tree);
} else if (className.endsWith("$JCConstantCaseLabel")) { // Introduced in JDK19
printConstantCaseLabel(tree);
} else if (className.endsWith("$JCPatternCaseLabel")) { // Introduced in JDK19
printPatternCaseLabel(tree);
} else if (className.endsWith("$JCRecordPattern")) { // Introduced in JDK19
printRecordPattern(tree);
} else {
throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree);
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/lombok/javac/JavacTreeMaker.java
Expand Up @@ -610,6 +610,8 @@ public JCCase Case(JCExpression pat, List<JCStatement> stats) {
List<JCTree> labels;
if (pat == null) {
labels = tryResolve(DefaultCaseLabel) ? List.of(DefaultCaseLabel()) : List.<JCTree>nil();
} else if (tryResolve(ConstantCaseLabel)) {
labels = List.<JCTree>of(ConstantCaseLabel(pat));
} else {
labels = List.<JCTree>of(pat);
}
Expand All @@ -622,6 +624,12 @@ public JCTree DefaultCaseLabel() {
return invoke(DefaultCaseLabel);
}

//javac versions: 19
private static final MethodId<JCTree> ConstantCaseLabel = MethodId("ConstantCaseLabel", JCTree.class, JCExpression.class);
public JCTree ConstantCaseLabel(JCExpression expr) {
return invoke(ConstantCaseLabel, expr);
}

//javac versions: 6-8
private static final MethodId<JCSynchronized> Synchronized = MethodId("Synchronized");
public JCSynchronized Synchronized(JCExpression lock, JCBlock body) {
Expand Down
15 changes: 15 additions & 0 deletions test/pretty/resource/after/RecordPattern.java
@@ -0,0 +1,15 @@
record Point(int x, int y) {
}
record Rectangle(Point upperLeft, Point lowerRight) {
}

public class RecordPattern {
void recordPattern(Object o) {
if (o instanceof Point(int x, int y)) {
}
if (o instanceof Point(int x, int y) p) {
}
if (o instanceof Rectangle(Point(int x1, int y1), Point(int x2, int y2))) {
}
}
}
32 changes: 32 additions & 0 deletions test/pretty/resource/after/Switch19.java
@@ -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();
};
}
}
16 changes: 16 additions & 0 deletions test/pretty/resource/before/RecordPattern.java
@@ -0,0 +1,16 @@
// version 19:
record Point(int x, int y) {
}
record Rectangle(Point upperLeft, Point lowerRight) {
}

public class RecordPattern {
void recordPattern(Object o) {
if (o instanceof Point(int x, int y)) {
}
if (o instanceof Point(int x, int y) p) {
}
if (o instanceof Rectangle(Point(int x1, int y1), Point(int x2, int y2))) {
}
}
}
2 changes: 1 addition & 1 deletion test/pretty/resource/before/Switch17.java
@@ -1,4 +1,4 @@
// version 17:
// version 17:18
public class Switch17 {
String switchPatternMatching(Object o) {
return switch (o) {
Expand Down
33 changes: 33 additions & 0 deletions test/pretty/resource/before/Switch19.java
@@ -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();
};
}
}