From 174d2d698a19b5a56c2ddba7ac4d4489b0c603be Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 27 Dec 2024 14:31:54 +0100 Subject: [PATCH] Slight polish --- .../EqualsAvoidsNullVisitor.java | 21 +++++++------------ .../staticanalysis/EqualsAvoidsNullTest.java | 6 +++--- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java index f443df249..932d2d6dd 100644 --- a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java +++ b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java @@ -47,16 +47,11 @@ public class EqualsAvoidsNullVisitor

extends JavaVisitor

{ private static final String JAVA_LANG_STRING = "java.lang.String "; - private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + - "equals(java.lang.Object)"); - private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + - "equalsIgnoreCase(java.lang.String)"); - private static final MethodMatcher COMPARE_TO = new MethodMatcher(JAVA_LANG_STRING + - "compareTo(java.lang.String)"); - private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + - "compareToIgnoreCase(java.lang.String)"); - private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + - "contentEquals(java.lang.CharSequence)"); + private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + "equals(java.lang.Object)"); + private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "equalsIgnoreCase(java.lang.String)"); + private static final MethodMatcher COMPARE_TO = new MethodMatcher(JAVA_LANG_STRING + "compareTo(java.lang.String)"); + private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "compareToIgnoreCase(java.lang.String)"); + private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + "contentEquals(java.lang.CharSequence)"); EqualsAvoidsNullStyle style; @@ -108,10 +103,8 @@ private void maybeHandleParentBinary(J.MethodInvocation m) { if (parent instanceof J.Binary) { if (((J.Binary) parent).getOperator() == J.Binary.Type.And && ((J.Binary) parent).getLeft() instanceof J.Binary) { 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()))) { + if (isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(), requireNonNull(m.getSelect())) || + isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) { doAfterVisit(new RemoveUnnecessaryNullCheck<>((J.Binary) parent)); } } diff --git a/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java b/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java index 0a33883df..629289ae0 100644 --- a/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java @@ -326,8 +326,7 @@ private void isFoo(List list) { } @Test - @Disabled - // fixme + @Disabled("Not yet supported") void lambdaGenerics() { rewriteRun( //language=java @@ -381,7 +380,8 @@ private void rr(List f, T e) { .forEach(fn -> Constants.FOO.equals(e.get(fn))); } } - """) + """ + ) ); }