diff --git a/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseAssignmentsToSwitchExpression.java b/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseAssignmentsToSwitchExpression.java index a4f437afd0..31fcad0a02 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseAssignmentsToSwitchExpression.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseAssignmentsToSwitchExpression.java @@ -70,6 +70,7 @@ public J.Block visitBlock(J.Block originalBlock, ExecutionContext ctx) { return block.withStatements(ListUtils.map(block.getStatements(), (index, statement) -> { if (statement == originalSwitch.getAndSet(null)) { doAfterVisit(new InlineVariable().getVisitor()); + doAfterVisit(new SwitchExpressionYieldToArrow().getVisitor()); // We've already converted the switch/assignments to an assignment with a switch expression. return null; } diff --git a/src/main/java/org/openrewrite/java/migrate/lang/SwitchExpressionYieldToArrow.java b/src/main/java/org/openrewrite/java/migrate/lang/SwitchExpressionYieldToArrow.java new file mode 100644 index 0000000000..691dc37cc4 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/lang/SwitchExpressionYieldToArrow.java @@ -0,0 +1,104 @@ +/* + * Copyright 2025 the original author or authors. + *
+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://docs.moderne.io/licensing/moderne-source-available-license + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.lang;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Preconditions;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.search.UsesJavaVersion;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JContainer;
+import org.openrewrite.java.tree.Space;
+import org.openrewrite.java.tree.Statement;
+import org.openrewrite.staticanalysis.groovy.GroovyFileChecker;
+import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;
+
+import static java.util.Objects.requireNonNull;
+
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class SwitchExpressionYieldToArrow extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Convert switch expression yield to arrow";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Convert switch expressions with colon cases and yield statements to arrow syntax.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ TreeVisitor, ExecutionContext> preconditions = Preconditions.and(
+ new UsesJavaVersion<>(14),
+ Preconditions.not(new KotlinFileChecker<>()),
+ Preconditions.not(new GroovyFileChecker<>())
+ );
+ return Preconditions.check(preconditions, new JavaIsoVisitor
+ * Licensed under the Moderne Source Available License (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://docs.moderne.io/licensing/moderne-source-available-license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.lang;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.Issue;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+import static org.openrewrite.java.Assertions.javaVersion;
+
+class SwitchExpressionYieldToArrowTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new SwitchExpressionYieldToArrow())
+ .allSources(s -> s.markers(javaVersion(17)));
+ }
+
+ @DocumentExample
+ @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/799")
+ @Test
+ void convertSwitchExpressionWithYield() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Test {
+ String format(String str) {
+ String formatted = switch (str) {
+ case "foo": yield "Foo";
+ case "bar": yield "Bar";
+ case null, default: yield "unknown";
+ };
+ return formatted;
+ }
+ }
+ """,
+ """
+ class Test {
+ String format(String str) {
+ String formatted = switch (str) {
+ case "foo" -> "Foo";
+ case "bar" -> "Bar";
+ case null, default -> "unknown";
+ };
+ return formatted;
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void convertSwitchExpressionWithSimpleYields() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Test {
+ int getValue(String str) {
+ return switch (str) {
+ case "one": yield 1;
+ case "two": yield 2;
+ default: yield 0;
+ };
+ }
+ }
+ """,
+ """
+ class Test {
+ int getValue(String str) {
+ return switch (str) {
+ case "one" -> 1;
+ case "two" -> 2;
+ default -> 0;
+ };
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void convertEnumSwitchExpression() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Test {
+ enum Color { RED, GREEN, BLUE }
+
+ String colorName(Color color) {
+ return switch (color) {
+ case RED: yield "Red";
+ case GREEN: yield "Green";
+ case BLUE: yield "Blue";
+ };
+ }
+ }
+ """,
+ """
+ class Test {
+ enum Color { RED, GREEN, BLUE }
+
+ String colorName(Color color) {
+ return switch (color) {
+ case RED -> "Red";
+ case GREEN -> "Green";
+ case BLUE -> "Blue";
+ };
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotConvertArrowCases() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Test {
+ String format(String str) {
+ return switch (str) {
+ case "foo" -> "Foo";
+ case "bar" -> "Bar";
+ default -> "Other";
+ };
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotConvertComplexYieldCases() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Test {
+ String process(String str) {
+ return switch (str) {
+ case "foo":
+ System.out.println("Processing foo");
+ yield "Foo";
+ case "bar": yield "Bar";
+ default: yield "Other";
+ };
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotConvertEmptyCases() {
+ rewriteRun(
+ java(
+ """
+ class Test {
+ enum TrafficLight {
+ RED, GREEN, YELLOW
+ }
+ void doFormat(TrafficLight light) {
+ String status = switch (light) {
+ case RED:
+ case GREEN:
+ case YELLOW: yield "unsure";
+ default: yield "unknown";
+ };
+ }
+ }
+ """
+ )
+ );
+ }
+}