Skip to content

Commit ac9f1e1

Browse files
committed
Do not add a break after a throw
Fixes #785
1 parent 0c6efc9 commit ac9f1e1

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,26 @@ private J.Case createCaseStatement(J.Switch aSwitch, Statement whenNull, J.Case
154154
} else {
155155
statements.add(whenNull);
156156
}
157+
158+
// Check if the last statement is a throw statement
159+
Statement lastStatement = null;
160+
if (whenNull instanceof J.Block) {
161+
List<Statement> blockStatements = ((J.Block) whenNull).getStatements();
162+
if (!blockStatements.isEmpty()) {
163+
lastStatement = blockStatements.get(blockStatements.size() - 1);
164+
}
165+
} else {
166+
lastStatement = whenNull;
167+
}
168+
157169
StringBuilder template = new StringBuilder("switch(#{any()}) {\ncase null:");
158170
for (int i = 1; i < statements.size(); i++) {
159171
template.append("\n#{any()};");
160172
}
161-
template.append("\nbreak;\n}");
173+
if (!(lastStatement instanceof J.Throw)) {
174+
template.append("\nbreak;");
175+
}
176+
template.append("\n}");
162177
J.Switch switchWithNullCase = JavaTemplate.apply(
163178
template.toString(),
164179
new Cursor(getCursor(), aSwitch),

src/test/java/org/openrewrite/java/migrate/lang/NullCheckAsSwitchCaseTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.jupiter.api.Nested;
1919
import org.junit.jupiter.api.Test;
2020
import org.openrewrite.DocumentExample;
21+
import org.openrewrite.Issue;
2122
import org.openrewrite.test.RecipeSpec;
2223
import org.openrewrite.test.RewriteTest;
2324

@@ -926,4 +927,66 @@ void score(Suit obj) {
926927
);
927928
}
928929
}
930+
931+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/785")
932+
@Test
933+
void noBreakAfterThrow() {
934+
rewriteRun(
935+
java(
936+
"""
937+
class Foo {
938+
String bar(String foo) {
939+
if (foo == null) {
940+
throw new RuntimeException("");
941+
}
942+
switch (foo) {
943+
case "hello":
944+
return "world";
945+
default:
946+
return "other";
947+
}
948+
}
949+
}
950+
""",
951+
"""
952+
class Foo {
953+
String bar(String foo) {
954+
switch (foo) {
955+
case null:
956+
throw new RuntimeException("");
957+
case "hello":
958+
return "world";
959+
default:
960+
return "other";
961+
}
962+
}
963+
}
964+
"""
965+
)
966+
);
967+
}
968+
969+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/785")
970+
@Test
971+
void noChangeOnReturn() {
972+
rewriteRun(
973+
java(
974+
"""
975+
class Foo {
976+
String bar(String foo) {
977+
if (foo == null) {
978+
return "";
979+
}
980+
switch (foo) {
981+
case "hello":
982+
return "world";
983+
default:
984+
return "other";
985+
}
986+
}
987+
}
988+
"""
989+
)
990+
);
991+
}
929992
}

0 commit comments

Comments
 (0)