Skip to content

Commit 4abfa6c

Browse files
Handling parentheses sometimes needed around instanceof (#521)
* Handling parentheses sometimes needed around instanceof * Indent Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent b88c9bf commit 4abfa6c

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/main/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceof.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openrewrite.java.tree.JavaType;
3434

3535
import java.util.Collections;
36+
import java.util.function.Function;
3637
import java.util.Set;
3738

3839
public class ReplaceClassIsInstanceWithInstanceof extends Recipe {
@@ -68,15 +69,29 @@ public J visitMethodInvocation(MethodInvocation method, ExecutionContext ctx) {
6869
FieldAccess fieldAccessPart = (FieldAccess) method.getSelect();
6970
// upcast to type J, so J.MethodInvocation can be replaced by J.InstanceOf
7071
JavaCoordinates coordinates = method.getCoordinates().replace();
71-
J.InstanceOf instanceOf = JavaTemplate.builder("#{any()} instanceof Object")
72+
J updated = JavaTemplate.builder("#{any()} instanceof Object")
7273
.build()
7374
.apply(getCursor(), coordinates, objectExpression);
74-
instanceOf = instanceOf.withClazz(fieldAccessPart.getTarget().withPrefix(instanceOf.getClazz().getPrefix()));
75-
return maybeAutoFormat(method, instanceOf, ctx);
75+
updated = mapInstanceOf(updated,
76+
instanceOf -> instanceOf.withClazz(fieldAccessPart.getTarget().withPrefix(instanceOf.getClazz().getPrefix())));
77+
return maybeAutoFormat(method, updated, ctx);
7678
}
7779
return super.visitMethodInvocation(method, ctx);
7880
}
7981

82+
private J mapInstanceOf(J tree, Function<J.InstanceOf, J.InstanceOf> fun) {
83+
if (tree instanceof J.InstanceOf) {
84+
return fun.apply((J.InstanceOf) tree);
85+
}
86+
if (tree instanceof J.Parentheses) {
87+
J.Parentheses<J> par = (J.Parentheses<J>) tree;
88+
J inner = mapInstanceOf(par.getTree(), fun);
89+
return par.withTree(inner);
90+
} else {
91+
throw new IllegalArgumentException("Expected J.InstanceOf or J.Parentheses, but got: " + tree.getClass());
92+
}
93+
}
94+
8095
private boolean isObjectClass(@Nullable Expression expression) {
8196
if (expression instanceof J.FieldAccess) {
8297
J.FieldAccess fieldAccess = (J.FieldAccess) expression;

src/test/java/org/openrewrite/staticanalysis/ReplaceClassIsInstanceWithInstanceofTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,32 @@ boolean foo(Object obj) {
282282
);
283283
}
284284

285+
@Test
286+
void parensAroundInstanceOf() {
287+
rewriteRun(
288+
//language=java
289+
java(
290+
"""
291+
class A {
292+
boolean foo(Object one, Object two) {
293+
if (one == null || !String.class.isInstance(two)) {
294+
return false;
295+
}
296+
return true;
297+
}
298+
}
299+
""",
300+
"""
301+
class A {
302+
boolean foo(Object one, Object two) {
303+
if (one == null || !(two instanceof String)) {
304+
return false;
305+
}
306+
return true;
307+
}
308+
}
309+
"""
310+
)
311+
);
312+
}
285313
}

0 commit comments

Comments
 (0)