Skip to content

Commit f445b48

Browse files
committed
Review comments applied
1 parent e4c9f43 commit f445b48

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

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

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
public class IfElseIfConstructToSwitch extends Recipe {
4141
@Override
4242
public String getDisplayName() {
43-
return "Use pattern matching in switch cases";
43+
return "If-elseIf-else construct to switch";
4444
}
4545

4646
@Override
4747
public String getDescription() {
48-
return "Enhance the Java programming language with pattern matching for switch expressions and statements. " +
49-
"Extending pattern matching to switch allows an expression to be tested against a number of patterns, each with a specific action, so that complex data-oriented queries can be expressed concisely and safely.";
48+
return "Replace if-elseIf-else construct with switch statements. In order to be replaced with a switch, " +
49+
"all conditions must be on the same variable and there must be at least 3 cases.";
5050
}
5151

5252
@Override
@@ -100,50 +100,43 @@ private static class SwitchCandidate {
100100
}
101101
}
102102

103-
J.@Nullable If handleNullCheck(J.If ifPart, Cursor cursor) {
104-
if (ifPart.getIfCondition().getTree() instanceof J.Binary) {
105-
Optional<NullCheck> nullCheck = nullCheck().get(ifPart, cursor);
106-
if (nullCheck.isPresent()) {
107-
nullCheckedParameter = nullCheck.get().getNullCheckedParameter();
108-
nullCheckedStatement = nullCheck.get().whenNull();
109-
Statement elsePart = nullCheck.get().whenNotNull();
110-
if (elsePart instanceof J.If) {
111-
ifPart = (J.If) elsePart;
112-
} else {
113-
elze = elsePart;
114-
ifPart = null;
115-
}
103+
private J.@Nullable If handleNullCheck(J.If ifPart, Cursor cursor) {
104+
Optional<NullCheck> nullCheck = nullCheck().get(ifPart, cursor);
105+
if (nullCheck.isPresent()) {
106+
nullCheckedParameter = nullCheck.get().getNullCheckedParameter();
107+
nullCheckedStatement = nullCheck.get().whenNull();
108+
Statement elsePart = nullCheck.get().whenNotNull();
109+
if (elsePart instanceof J.If) {
110+
ifPart = (J.If) elsePart;
116111
} else {
117-
noPotentialCandidate();
112+
elze = elsePart;
113+
ifPart = null;
118114
}
119-
return ifPart;
115+
} else {
116+
noPotentialCandidate();
120117
}
121-
throw new IllegalArgumentException("Unsupported if type: " + ifPart.getIfCondition().getTree().getClass().getSimpleName());
118+
return ifPart;
122119
}
123120

124-
J.@Nullable If handleInstanceOfCheck(J.If ifPart) {
125-
if (ifPart.getIfCondition().getTree() instanceof J.InstanceOf) {
126-
patternMatchers.put((J.InstanceOf) ifPart.getIfCondition().getTree(), ifPart.getThenPart());
127-
J.If.Else elsePart = ifPart.getElsePart();
128-
if (elsePart != null && elsePart.getBody() instanceof J.If) {
129-
ifPart = (J.If) elsePart.getBody();
130-
} else {
131-
elze = elsePart != null ? elsePart.getBody() : null;
132-
ifPart = null;
133-
}
134-
return ifPart;
121+
private J.@Nullable If handleInstanceOfCheck(J.If ifPart) {
122+
patternMatchers.put((J.InstanceOf) ifPart.getIfCondition().getTree(), ifPart.getThenPart());
123+
J.If.Else elsePart = ifPart.getElsePart();
124+
if (elsePart != null && elsePart.getBody() instanceof J.If) {
125+
ifPart = (J.If) elsePart.getBody();
126+
} else {
127+
elze = elsePart != null ? elsePart.getBody() : null;
128+
ifPart = null;
135129
}
136-
throw new IllegalArgumentException("Unsupported if type: " + ifPart.getIfCondition().getTree().getClass().getSimpleName());
130+
return ifPart;
137131
}
138132

139133
void noPotentialCandidate() {
140134
this.potentialCandidate = false;
141135
}
142136

143137
boolean isValidCandidate() {
144-
// all ifs in the chain must be on the same variable
138+
// all ifs in the chain must be on the same variable in order to be a candidate for switch pattern matching
145139
if (potentialCandidate && patternMatchers.keySet().stream().map(J.InstanceOf::getExpression).map(expression -> ((J.Identifier) expression).getSimpleName()).distinct().count() != 1) {
146-
// pattern matching in a switch can only happen if all if cases are on the same variable.
147140
this.potentialCandidate = false;
148141
return false;
149142
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
public class NullCheckAsSwitchCase extends Recipe {
4040
@Override
4141
public String getDisplayName() {
42-
return "Use pattern matching in switch cases";
42+
return "Add null check to existing switch cases";
4343
}
4444

4545
@Override
4646
public String getDescription() {
47-
return "Enhance the Java programming language with pattern matching for switch expressions and statements. " +
48-
"Extending pattern matching to switch allows an expression to be tested against a number of patterns, each with a specific action, so that complex data-oriented queries can be expressed concisely and safely.";
47+
return "In later java versions, null checks are valid switch cases. " +
48+
"This recipe will only add null checks to existing switch cases if there are no other statements in between them " +
49+
"or if the block in the if statement is not impacting the flow of the switch.";
4950
}
5051

5152
@Override

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,36 @@ else if (obj instanceof Long) {
317317
)
318318
);
319319
}
320+
321+
@Test
322+
void noSwitchBlockWithDifferentVariablesBeingChecked() {
323+
rewriteRun(
324+
//language=java
325+
java(
326+
"""
327+
class Test {
328+
static String formatter(Object obj) {
329+
String formatted = "initialValue";
330+
Object anotherObj = obj;
331+
if (obj == null) {
332+
formatted = "null";
333+
} else if (obj instanceof Integer i)
334+
formatted = String.format("int %d", i);
335+
else if (obj instanceof Long l) {
336+
formatted = String.format("long %d", l);
337+
} else if (obj instanceof Double d) {
338+
formatted = String.format("double %f", d);
339+
} else if (anotherObj instanceof String s) {
340+
String str = "String";
341+
formatted = String.format("%s %s", str, s);
342+
} else {
343+
formatted = "unknown";
344+
}
345+
return formatted;
346+
}
347+
}
348+
"""
349+
)
350+
);
351+
}
320352
}

0 commit comments

Comments
 (0)