Skip to content

Commit 5aabf01

Browse files
Move all switch expression recipes to Java 21 migration (#824)
* Move all switch expression recipes to Java 21 migration * Apply suggestion from @github-actions[bot] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestion from @github-actions[bot] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestion from @github-actions[bot] --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 8c6f965 commit 5aabf01

File tree

9 files changed

+343
-380
lines changed

9 files changed

+343
-380
lines changed

src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseAssignmentsToSwitchExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public String getDisplayName() {
4848
@Override
4949
public String getDescription() {
5050
return "Switch statements for which each case is assigning a value to the same variable can be converted to a switch expression that returns the value of the variable. " +
51-
"This is only applicable for Java 17 and later.";
51+
"This recipe is only applicable for Java 21 and later.";
5252
}
5353

5454
@Override
5555
public TreeVisitor<?, ExecutionContext> getVisitor() {
5656
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
57-
new UsesJavaVersion<>(17),
57+
new UsesJavaVersion<>(21),
5858
Preconditions.not(new KotlinFileChecker<>()),
5959
Preconditions.not(new GroovyFileChecker<>())
6060
);

src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseReturnsToSwitchExpression.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ public String getDisplayName() {
4444

4545
@Override
4646
public String getDescription() {
47-
return "Switch statements where each case returns a value can be converted to a switch expression that returns the value directly.";
47+
return "Switch statements where each case returns a value can be converted to a switch expression that returns the value directly. " +
48+
"This recipe is only applicable for Java 21 and later.";
4849
}
4950

5051
@Override
5152
public TreeVisitor<?, ExecutionContext> getVisitor() {
5253
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
53-
new UsesJavaVersion<>(14),
54+
new UsesJavaVersion<>(21),
5455
Preconditions.not(new KotlinFileChecker<>()),
5556
Preconditions.not(new GroovyFileChecker<>())
5657
);
@@ -63,12 +64,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
6364
J.Switch sw = (J.Switch) statement;
6465
if (canConvertToSwitchExpression(sw)) {
6566
J.SwitchExpression switchExpression = convertToSwitchExpression(sw);
66-
return new J.Return(
67-
randomId(),
68-
sw.getPrefix(),
69-
Markers.EMPTY,
70-
switchExpression
71-
);
67+
return new J.Return(randomId(), sw.getPrefix(), Markers.EMPTY, switchExpression);
7268
}
7369
}
7470
return statement;

src/main/java/org/openrewrite/java/migrate/lang/SwitchExpressionYieldToArrow.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public String getDisplayName() {
4343

4444
@Override
4545
public String getDescription() {
46-
return "Convert switch expressions with colon cases and yield statements to arrow syntax.";
46+
return "Convert switch expressions with colon cases and yield statements to arrow syntax. " +
47+
"This recipe is only applicable for Java 21 and later.";
4748
}
4849

4950
@Override
5051
public TreeVisitor<?, ExecutionContext> getVisitor() {
5152
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
52-
new UsesJavaVersion<>(14),
53+
new UsesJavaVersion<>(21),
5354
Preconditions.not(new KotlinFileChecker<>()),
5455
Preconditions.not(new GroovyFileChecker<>())
5556
);

src/main/resources/META-INF/rewrite/java-version-17.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ recipeList:
5959
artifactId: commons-codec
6060
newVersion: 1.17.x
6161
- org.openrewrite.java.migrate.AddLombokMapstructBinding
62-
- org.openrewrite.java.migrate.lang.SwitchCaseAssignmentsToSwitchExpression
63-
- org.openrewrite.java.migrate.lang.SwitchCaseReturnsToSwitchExpression
64-
- org.openrewrite.java.migrate.lang.SwitchExpressionYieldToArrow
6562
- org.openrewrite.java.migrate.UpdateJakartaAnnotationsIfForJavax
6663
- org.openrewrite.java.migrate.UpdateJakartaAnnotationsIfExistsForJakarta
6764

src/main/resources/META-INF/rewrite/java-version-21.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ recipeList:
3939
- org.openrewrite.java.migrate.UpgradePluginsForJava21
4040
- org.openrewrite.java.migrate.DeleteDeprecatedFinalize
4141
- org.openrewrite.java.migrate.RemovedSubjectMethods
42+
# TODO: Remove these recipes from this list once `SwitchPatternMatching` has been enabled
43+
- org.openrewrite.java.migrate.lang.SwitchCaseAssignmentsToSwitchExpression
44+
- org.openrewrite.java.migrate.lang.SwitchCaseReturnsToSwitchExpression
45+
- org.openrewrite.java.migrate.lang.SwitchExpressionYieldToArrow
4246
#- org.openrewrite.java.migrate.SwitchPatternMatching
4347
#- org.openrewrite.java.migrate.lang.NullCheckAsSwitchCase
4448

@@ -142,6 +146,9 @@ description: >-
142146
tags:
143147
- java21
144148
recipeList:
149+
- org.openrewrite.java.migrate.lang.SwitchCaseAssignmentsToSwitchExpression
150+
- org.openrewrite.java.migrate.lang.SwitchCaseReturnsToSwitchExpression
151+
- org.openrewrite.java.migrate.lang.SwitchExpressionYieldToArrow
145152
- org.openrewrite.java.migrate.lang.IfElseIfConstructToSwitch
146153
- org.openrewrite.java.migrate.lang.RefineSwitchCases
147154
- org.openrewrite.java.migrate.lang.SwitchCaseEnumGuardToLabel

0 commit comments

Comments
 (0)