Skip to content

Commit ed6194c

Browse files
committed
Avoid unnecessary changes seen at scale for RefineSwitchCases
1 parent 97410ab commit ed6194c

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,24 @@ public J.Switch visitSwitch(J.Switch sw, ExecutionContext ctx) {
7373
}
7474
return statement;
7575
})));
76-
return new JavaIsoVisitor<ExecutionContext>() {
77-
@Override
78-
public J.Case visitCase(J.Case case_, ExecutionContext ctx) {
79-
// Remove any trailing new line in empty case body
80-
if (case_.getBody() instanceof J.Block) {
81-
J.Block body = (J.Block) case_.getBody();
82-
if (body.getStatements().isEmpty() && !body.getEnd().isEmpty()) {
83-
return case_.withBody(body.withEnd(Space.EMPTY));
76+
if (mappedSwitch != switch_) {
77+
return new JavaIsoVisitor<ExecutionContext>() {
78+
@Override
79+
public J.Case visitCase(J.Case case_, ExecutionContext ctx) {
80+
// Remove any trailing new line in empty case body
81+
if (case_.getBody() instanceof J.Block) {
82+
J.Block body = (J.Block) case_.getBody();
83+
if (body.getStatements().isEmpty() &&
84+
body.getEnd().getComments().isEmpty() &&
85+
!body.getEnd().isEmpty()) {
86+
return case_.withBody(body.withEnd(Space.EMPTY));
87+
}
8488
}
89+
return case_;
8590
}
86-
return case_;
87-
}
88-
}.visitSwitch(maybeAutoFormat(switch_, mappedSwitch, ctx), ctx);
91+
}.visitSwitch(autoFormat(mappedSwitch, ctx), ctx);
92+
}
93+
return switch_;
8994
}
9095

9196
private Set<String> extractLabelVariables(J.Case case_) {

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

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.migrate.lang;
1717

18+
import org.junit.jupiter.api.Nested;
1819
import org.junit.jupiter.api.Test;
1920
import org.openrewrite.DocumentExample;
2021
import org.openrewrite.test.RecipeSpec;
@@ -55,6 +56,9 @@ else if ("NO".equalsIgnoreCase(s))
5556
else
5657
System.out.println("Sorry?");
5758
}
59+
default -> {
60+
// Comment retained
61+
}
5862
}
5963
}
6064
}
@@ -76,6 +80,9 @@ static void score(Object obj) {
7680
System.out.println("Shame");
7781
case String s ->
7882
System.out.println("Sorry?");
83+
default -> {
84+
// Comment retained
85+
}
7986
}
8087
}
8188
}
@@ -211,28 +218,50 @@ static void score(Object obj) {
211218
);
212219
}
213220

214-
@Test
215-
void noChangeWhenAlreadyGuarded() {
216-
rewriteRun(
217-
//language=java
218-
java(
219-
"""
220-
class Test {
221-
static void score(Object obj) {
222-
switch (obj) {
223-
case Integer i when i == 7 -> {
224-
if (i >= 5 && i <= 10)
225-
System.out.println("You got it");
226-
else if (i >= 0 && i < 5)
227-
System.out.println("Shame");
228-
else
229-
System.out.println("Sorry?");
221+
@Nested
222+
class NoChange{
223+
@Test
224+
void noChangeWhenAlreadyGuarded() {
225+
rewriteRun(
226+
//language=java
227+
java(
228+
"""
229+
class Test {
230+
static void score(Object obj) {
231+
switch (obj) {
232+
case Integer i when i == 7 -> {
233+
if (i >= 5 && i <= 10)
234+
System.out.println("You got it");
235+
else if (i >= 0 && i < 5)
236+
System.out.println("Shame");
237+
else
238+
System.out.println("Sorry?");
239+
}
230240
}
231241
}
232242
}
233-
}
234-
"""
235-
)
236-
);
243+
"""
244+
)
245+
);
246+
}
247+
248+
@Test
249+
void notFormattedWhenNotChanged() {
250+
rewriteRun(
251+
//language=java
252+
java(
253+
"""
254+
class Test {
255+
void score(Object obj) {
256+
switch (obj) {
257+
default -> {
258+
}
259+
}
260+
}
261+
}
262+
"""
263+
)
264+
);
265+
}
237266
}
238267
}

0 commit comments

Comments
 (0)