Skip to content

Handling parentheses sometimes needed around instanceof #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openrewrite.java.tree.JavaType;

import java.util.Collections;
import java.util.function.Function;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import java.util.function.Function;
import java.util.function.Function;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import java.util.function.Function;
import java.util.function.Function;

import java.util.Set;

public class ReplaceClassIsInstanceWithInstanceof extends Recipe {
Expand Down Expand Up @@ -68,15 +69,29 @@ public J visitMethodInvocation(MethodInvocation method, ExecutionContext ctx) {
FieldAccess fieldAccessPart = (FieldAccess) method.getSelect();
// upcast to type J, so J.MethodInvocation can be replaced by J.InstanceOf
JavaCoordinates coordinates = method.getCoordinates().replace();
J.InstanceOf instanceOf = JavaTemplate.builder("#{any()} instanceof Object")
J updated = JavaTemplate.builder("#{any()} instanceof Object")
.build()
.apply(getCursor(), coordinates, objectExpression);
instanceOf = instanceOf.withClazz(fieldAccessPart.getTarget().withPrefix(instanceOf.getClazz().getPrefix()));
return maybeAutoFormat(method, instanceOf, ctx);
updated = mapInstanceOf(updated,
instanceOf -> instanceOf.withClazz(fieldAccessPart.getTarget().withPrefix(instanceOf.getClazz().getPrefix())));
return maybeAutoFormat(method, updated, ctx);
}
return super.visitMethodInvocation(method, ctx);
}

private J mapInstanceOf(J tree, Function<J.InstanceOf, J.InstanceOf> fun) {
if (tree instanceof J.InstanceOf) {
return fun.apply((J.InstanceOf) tree);
}
if (tree instanceof J.Parentheses) {
J.Parentheses<J> par = (J.Parentheses<J>) tree;
J inner = mapInstanceOf(par.getTree(), fun);
return par.withTree(inner);
} else {
throw new IllegalArgumentException("Expected J.InstanceOf or J.Parentheses, but got: " + tree.getClass());
}
}

private boolean isObjectClass(@Nullable Expression expression) {
if (expression instanceof J.FieldAccess) {
J.FieldAccess fieldAccess = (J.FieldAccess) expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,32 @@ boolean foo(Object obj) {
);
}

@Test
void parensAroundInstanceOf() {
rewriteRun(
//language=java
java(
"""
class A {
boolean foo(Object one, Object two) {
if (one == null || !String.class.isInstance(two)) {
return false;
}
return true;
}
}
""",
"""
class A {
boolean foo(Object one, Object two) {
if (one == null || !(two instanceof String)) {
return false;
}
return true;
}
}
"""
)
);
}
}
Loading