Skip to content

Commit 1a0eb1b

Browse files
punkratz312pinguin3245678timtebeek
authored
fix missing ObjectEquals (#464)
* JAVA_LANG_OBJECT * Apply formatter --------- Co-authored-by: Vincent Potucek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent 47dff10 commit 1a0eb1b

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
public class EqualsAvoidsNull extends Recipe {
3939

4040
private static final String JAVA_LANG_STRING = "java.lang.String";
41+
private static final String JAVA_LANG_OBJECT = "java.lang.Object";
4142

42-
private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + " equals(java.lang.Object)");
43+
private static final MethodMatcher EQUALS_STRING = new MethodMatcher(JAVA_LANG_STRING + " equals(" + JAVA_LANG_OBJECT + ")");
44+
private static final MethodMatcher EQUALS_OBJECT = new MethodMatcher(JAVA_LANG_OBJECT + " equals(" + JAVA_LANG_OBJECT + ")");
4345
private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + " equalsIgnoreCase(" + JAVA_LANG_STRING + ")");
4446
private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + " contentEquals(java.lang.CharSequence)");
4547

@@ -66,7 +68,11 @@ public Duration getEstimatedEffortPerOccurrence() {
6668
@Override
6769
public TreeVisitor<?, ExecutionContext> getVisitor() {
6870
return Preconditions.check(
69-
Preconditions.or(new UsesMethod<>(EQUALS), new UsesMethod<>(EQUALS_IGNORE_CASE), new UsesMethod<>(CONTENT_EQUALS)),
71+
Preconditions.or(
72+
new UsesMethod<>(EQUALS_STRING),
73+
new UsesMethod<>(EQUALS_OBJECT),
74+
new UsesMethod<>(EQUALS_IGNORE_CASE),
75+
new UsesMethod<>(CONTENT_EQUALS)),
7076
new JavaVisitor<ExecutionContext>() {
7177
@Override
7278
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
@@ -104,7 +110,8 @@ private boolean hasCompatibleArgument(J.MethodInvocation m) {
104110
}
105111

106112
private boolean isStringComparisonMethod(J.MethodInvocation methodInvocation) {
107-
return EQUALS.matches(methodInvocation) ||
113+
return EQUALS_STRING.matches(methodInvocation) ||
114+
EQUALS_OBJECT.matches(methodInvocation) ||
108115
EQUALS_IGNORE_CASE.matches(methodInvocation) ||
109116
CONTENT_EQUALS.matches(methodInvocation);
110117
}
@@ -116,8 +123,8 @@ private void maybeHandleParentBinary(J.MethodInvocation m, final Tree parent) {
116123
J.Binary potentialNullCheck = (J.Binary) ((J.Binary) parent).getLeft();
117124
if (isNullLiteral(potentialNullCheck.getLeft()) &&
118125
matchesSelect(potentialNullCheck.getRight(), requireNonNull(m.getSelect())) ||
119-
isNullLiteral(potentialNullCheck.getRight()) &&
120-
matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) {
126+
isNullLiteral(potentialNullCheck.getRight()) &&
127+
matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) {
121128
doAfterVisit(new JavaVisitor<ExecutionContext>() {
122129

123130
private final J.Binary scope = (J.Binary) parent;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ void foo(String s) {
113113
);
114114
}
115115

116+
@Test
117+
void ObjectEquals() {
118+
rewriteRun(
119+
//language=java
120+
java(
121+
"""
122+
class A {
123+
void foo(Object s) {
124+
if (s.equals("null")) {
125+
}
126+
}
127+
}
128+
""",
129+
"""
130+
class A {
131+
void foo(Object s) {
132+
if ("null".equals(s)) {
133+
}
134+
}
135+
}
136+
"""
137+
)
138+
);
139+
}
140+
116141
@Nested
117142
class ReplaceConstantMethodArg {
118143

0 commit comments

Comments
 (0)