Skip to content

Commit

Permalink
fix missing ObjectEquals (#464)
Browse files Browse the repository at this point in the history
* JAVA_LANG_OBJECT

* Apply formatter

---------

Co-authored-by: Vincent Potucek <vincent.potucek@sap.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
3 people authored Feb 13, 2025
1 parent 47dff10 commit 1a0eb1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
public class EqualsAvoidsNull extends Recipe {

private static final String JAVA_LANG_STRING = "java.lang.String";
private static final String JAVA_LANG_OBJECT = "java.lang.Object";

private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + " equals(java.lang.Object)");
private static final MethodMatcher EQUALS_STRING = new MethodMatcher(JAVA_LANG_STRING + " equals(" + JAVA_LANG_OBJECT + ")");
private static final MethodMatcher EQUALS_OBJECT = new MethodMatcher(JAVA_LANG_OBJECT + " equals(" + JAVA_LANG_OBJECT + ")");
private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + " equalsIgnoreCase(" + JAVA_LANG_STRING + ")");
private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + " contentEquals(java.lang.CharSequence)");

Expand All @@ -66,7 +68,11 @@ public Duration getEstimatedEffortPerOccurrence() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Preconditions.or(new UsesMethod<>(EQUALS), new UsesMethod<>(EQUALS_IGNORE_CASE), new UsesMethod<>(CONTENT_EQUALS)),
Preconditions.or(
new UsesMethod<>(EQUALS_STRING),
new UsesMethod<>(EQUALS_OBJECT),
new UsesMethod<>(EQUALS_IGNORE_CASE),
new UsesMethod<>(CONTENT_EQUALS)),
new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
Expand Down Expand Up @@ -104,7 +110,8 @@ private boolean hasCompatibleArgument(J.MethodInvocation m) {
}

private boolean isStringComparisonMethod(J.MethodInvocation methodInvocation) {
return EQUALS.matches(methodInvocation) ||
return EQUALS_STRING.matches(methodInvocation) ||
EQUALS_OBJECT.matches(methodInvocation) ||
EQUALS_IGNORE_CASE.matches(methodInvocation) ||
CONTENT_EQUALS.matches(methodInvocation);
}
Expand All @@ -116,8 +123,8 @@ private void maybeHandleParentBinary(J.MethodInvocation m, final Tree parent) {
J.Binary potentialNullCheck = (J.Binary) ((J.Binary) parent).getLeft();
if (isNullLiteral(potentialNullCheck.getLeft()) &&
matchesSelect(potentialNullCheck.getRight(), requireNonNull(m.getSelect())) ||
isNullLiteral(potentialNullCheck.getRight()) &&
matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) {
isNullLiteral(potentialNullCheck.getRight()) &&
matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) {
doAfterVisit(new JavaVisitor<ExecutionContext>() {

private final J.Binary scope = (J.Binary) parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ void foo(String s) {
);
}

@Test
void ObjectEquals() {
rewriteRun(
//language=java
java(
"""
class A {
void foo(Object s) {
if (s.equals("null")) {
}
}
}
""",
"""
class A {
void foo(Object s) {
if ("null".equals(s)) {
}
}
}
"""
)
);
}

@Nested
class ReplaceConstantMethodArg {

Expand Down

0 comments on commit 1a0eb1b

Please # to comment.