Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Fix-PMD-rule-LiteralsFirstInComp…
Browse files Browse the repository at this point in the history
…arisons-for-compareTo-and-contentEquals' into Fix-PMD-rule-LiteralsFirstInComparisons-for-compareTo-and-contentEquals
  • Loading branch information
Vincent Potucek committed Feb 9, 2025
2 parents 30f6c9b + d3abc1f commit cdf95f7
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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)) {
Expand Down

0 comments on commit cdf95f7

Please sign in to comment.