From a04fddac7ba3d2a79b5049e95ed4aa57b5ab4a60 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Sun, 8 Jan 2023 23:21:45 +0100 Subject: [PATCH] Make `IsEmptyCallOnCollections` also support `<` and `>` operators The expressions `c.size() > 0` and `0 < c.size()` can both also be replaced with `!c.isEmpty()`. --- .../cleanup/IsEmptyCallOnCollectionsTest.java | 8 ++++++++ .../java/cleanup/IsEmptyCallOnCollections.java | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollectionsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollectionsTest.java index 3ead1ae24e9..169656c4160 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollectionsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollectionsTest.java @@ -76,6 +76,10 @@ static void method(List l) { // empty body } else if (l.size() != 0 || 0 != l.size()) { // empty body + } else if (l.size() > 0 || l.size() < 0) { + // empty body + } else if (0 < l.size() || 0 > l.size()) { + // empty body } } } @@ -89,6 +93,10 @@ static void method(List l) { // empty body } else if (!l.isEmpty() || !l.isEmpty()) { // empty body + } else if (!l.isEmpty() || l.size() < 0) { + // empty body + } else if (!l.isEmpty() || 0 > l.size()) { + // empty body } } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollections.java b/rewrite-java/src/main/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollections.java index 1dc6fd9c6d6..ddee1d879fa 100755 --- a/rewrite-java/src/main/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollections.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/cleanup/IsEmptyCallOnCollections.java @@ -68,15 +68,17 @@ public JavaVisitor getVisitor() { @Override public J visitBinary(J.Binary binary, ExecutionContext ctx) { - if (binary.getOperator() == J.Binary.Type.Equal || binary.getOperator() == J.Binary.Type.NotEqual) { - if (COLLECTION_SIZE.matches(binary.getLeft()) || COLLECTION_SIZE.matches(binary.getRight())) { - if (isZero(binary.getLeft()) || isZero(binary.getRight())) { - J.MethodInvocation sizeCall = (J.MethodInvocation) (COLLECTION_SIZE.matches(binary.getLeft()) ? - binary.getLeft() : binary.getRight()); + if (isZero(binary.getLeft()) || isZero(binary.getRight())) { + boolean zeroRight = isZero(binary.getRight()); + J.MethodInvocation maybeSizeCall = (J.MethodInvocation) (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) { + if (COLLECTION_SIZE.matches(maybeSizeCall)) { String op = binary.getOperator() == J.Binary.Type.Equal ? "" : "!"; - return (sizeCall.getSelect() == null ? - sizeCall.withTemplate(isEmptyNoReceiver, sizeCall.getCoordinates().replace(), op) : - sizeCall.withTemplate(isEmpty, sizeCall.getCoordinates().replace(), op, sizeCall.getSelect()) + return (maybeSizeCall.getSelect() == null ? + maybeSizeCall.withTemplate(isEmptyNoReceiver, maybeSizeCall.getCoordinates().replace(), op) : + maybeSizeCall.withTemplate(isEmpty, maybeSizeCall.getCoordinates().replace(), op, maybeSizeCall.getSelect()) ).withPrefix(binary.getPrefix()); } }