diff --git a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java index 4ea346b7e..d1e399873 100644 --- a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java +++ b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java @@ -91,17 +91,17 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) { new MethodMatcher(JAVA_LANG_STRING + " equals(java.lang.Object)"); private final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + " equalsIgnoreCase(" + JAVA_LANG_STRING + ")"); - private final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING - + " contentEquals(java.lang.CharSequence)"); - - @Override - public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext p) { - J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, p); - if (m.getSelect() != null - && !(m.getSelect() instanceof J.Literal) - && isStringComparisonMethod(m) - && hasCompatibleArgument(m)) { - maybeHandleParentBinary(m, getCursor().getParentTreeCursor().getValue()); + private final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + + " contentEquals(java.lang.CharSequence)"); + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); + if (m.getSelect() != null && + !(m.getSelect() instanceof J.Literal) && + isStringComparisonMethod(m) && + hasCompatibleArgument(m)) { + return firstArgument.getType() == JavaType.Primitive.Null ? + literalsFirstInComparisonsNull(m, firstArgument) : + literalsFirstInComparisons(m, firstArgument); Expression firstArgument = m.getArguments().get(0); return firstArgument.getType() == JavaType.Primitive.Null ? literalsFirstInComparisonsNull(m, firstArgument) @@ -129,18 +129,18 @@ private boolean hasCompatibleArgument(J.MethodInvocation m) { } private boolean isStringComparisonMethod(J.MethodInvocation methodInvocation) { - return EQUALS.matches(methodInvocation) - || !ObjectUtils - .defaultIfNull(cu.getStyle(EqualsAvoidsNullStyle.class), - Checkstyle.equalsAvoidsNull()) - .getIgnoreEqualsIgnoreCase() - && EQUALS_IGNORE_CASE.matches(methodInvocation) - || CONTENT_EQUALS.matches(methodInvocation); - } - - private void maybeHandleParentBinary(J.MethodInvocation m, final Tree parent) { - if (parent instanceof J.Binary) { - if (((J.Binary) parent).getOperator() == J.Binary.Type.And + return EQUALS.matches(methodInvocation) || + !ObjectUtils + .getIgnoreEqualsIgnoreCase() && + EQUALS_IGNORE_CASE.matches(methodInvocation) || + CONTENT_EQUALS.matches(methodInvocation); + if (((J.Binary) parent).getOperator() == J.Binary.Type.And && + ((J.Binary) parent).getLeft() instanceof J.Binary) { + if (isNullLiteral(potentialNullCheck.getLeft()) && + matchesSelect(potentialNullCheck.getRight(), + requireNonNull(m.getSelect())) || + isNullLiteral(potentialNullCheck.getRight()) && + matchesSelect(potentialNullCheck.getLeft(), && ((J.Binary) parent).getLeft() instanceof J.Binary) { J.Binary potentialNullCheck = (J.Binary) ((J.Binary) parent).getLeft(); if (isNullLiteral(potentialNullCheck.getLeft()) @@ -155,12 +155,12 @@ && matchesSelect(potentialNullCheck.getLeft(), private boolean done; @Override - public @Nullable J visit(@Nullable Tree tree, ExecutionContext p) { - return done - ? (J) tree - : super.visit(tree, p); - } - + public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) { + return done ? + (J) tree : + super.visit(tree, ctx); + public J visitBinary(J.Binary binary, ExecutionContext ctx) { + return super.visitBinary(binary, ctx); @Override public J visitBinary(J.Binary binary, ExecutionContext p) { if (scope.isScope(binary)) {