diff --git a/src/main/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollections.java b/src/main/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollections.java index bbb0d1f239..fb62592932 100755 --- a/src/main/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollections.java +++ b/src/main/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollections.java @@ -68,10 +68,10 @@ public TreeVisitor getVisitor() { public J visitBinary(J.Binary binary, ExecutionContext ctx) { if (isZero(binary.getLeft()) || isZero(binary.getRight())) { boolean zeroRight = isZero(binary.getRight()); - J maybeSizeCall = zeroRight ? binary.getLeft() : binary.getRight(); if (binary.getOperator() == J.Binary.Type.Equal || binary.getOperator() == J.Binary.Type.NotEqual || zeroRight && binary.getOperator() == J.Binary.Type.GreaterThan || !zeroRight && binary.getOperator() == J.Binary.Type.LessThan) { + J maybeSizeCall = zeroRight ? binary.getLeft() : binary.getRight(); if (maybeSizeCall instanceof J.MethodInvocation) { J.MethodInvocation maybeSizeCallMethod = (J.MethodInvocation) maybeSizeCall; if (COLLECTION_SIZE.matches(maybeSizeCallMethod)) { @@ -84,6 +84,28 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) { } } } + if (isOne(binary.getLeft()) || isOne(binary.getRight())) { + boolean oneRight = isOne(binary.getRight()); + if ((oneRight && binary.getOperator() == J.Binary.Type.LessThan) + || (!oneRight && binary.getOperator() == J.Binary.Type.GreaterThan) + || (oneRight && binary.getOperator() == J.Binary.Type.GreaterThanOrEqual) + || (!oneRight && binary.getOperator() == J.Binary.Type.LessThanOrEqual)) { + J maybeSizeCall = oneRight ? binary.getLeft() : binary.getRight(); + if (maybeSizeCall instanceof J.MethodInvocation) { + J.MethodInvocation maybeSizeCallMethod = (J.MethodInvocation) maybeSizeCall; + if (COLLECTION_SIZE.matches(maybeSizeCallMethod)) { + String op = ""; + if (binary.getOperator() == J.Binary.Type.GreaterThanOrEqual || binary.getOperator() == J.Binary.Type.LessThanOrEqual) { + op = "!"; + } + return (maybeSizeCallMethod.getSelect() == null ? + isEmptyNoReceiver.apply(getCursor(), binary.getCoordinates().replace(), op) : + isEmpty.apply(getCursor(), binary.getCoordinates().replace(), op, maybeSizeCallMethod.getSelect()) + ).withPrefix(binary.getPrefix()); + } + } + } + } return super.visitBinary(binary, ctx); } }); @@ -92,4 +114,9 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) { private static boolean isZero(Expression expression) { return expression instanceof J.Literal && Integer.valueOf(0).equals(((J.Literal) expression).getValue()); } + + private static boolean isOne(Expression expression) { + return expression instanceof J.Literal && Integer.valueOf(1).equals(((J.Literal) expression).getValue()); + } + } diff --git a/src/test/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollectionsTest.java b/src/test/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollectionsTest.java index 416d443746..2f952cb568 100644 --- a/src/test/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollectionsTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollectionsTest.java @@ -75,7 +75,7 @@ boolean test() { } @Test - void isEmptyCallOnCollections() { + void comparisonWithZero() { rewriteRun( //language=java java( @@ -84,7 +84,7 @@ void isEmptyCallOnCollections() { class Test { static void method(List l) { - if (l.isEmpty() || 0 == l.size()) { + if (l.size() == 0 || 0 == l.size()) { // empty body } else if (l.size() != 0 || 0 != l.size()) { // empty body @@ -117,6 +117,49 @@ static void method(List l) { ); } + @Test + void comparisonWithOne() { + rewriteRun( + //language=java + java( + """ + import java.util.List; + + class Test { + static void method(List l) { + if (l.size() < 1 || 1 > l.size()) { + // empty body + } else if (l.size() > 1 || 1 < l.size()) { + // empty body + } else if (l.size() >= 1 || 1 <= l.size()) { + // empty body + } else if (l.size() <= 1 || 1 >= l.size()) { + // empty body + } + } + } + """, + """ + import java.util.List; + + class Test { + static void method(List l) { + if (l.isEmpty() || l.isEmpty()) { + // empty body + } else if (l.size() > 1 || 1 < l.size()) { + // empty body + } else if (!l.isEmpty() || !l.isEmpty()) { + // empty body + } else if (l.size() <= 1 || 1 >= l.size()) { + // empty body + } + } + } + """ + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite/issues/1112") @Test void formatting() {