3838public 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 ;
0 commit comments