Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public TreeVisitor<?, ExecutionContext> 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)) {
Expand All @@ -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);
}
});
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ boolean test() {
}

@Test
void isEmptyCallOnCollections() {
void comparisonWithZero() {
rewriteRun(
//language=java
java(
Expand All @@ -84,7 +84,7 @@ void isEmptyCallOnCollections() {

class Test {
static void method(List<String> 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
Expand Down Expand Up @@ -117,6 +117,49 @@ static void method(List<String> l) {
);
}

@Test
void comparisonWithOne() {
rewriteRun(
//language=java
java(
"""
import java.util.List;

class Test {
static void method(List<String> 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<String> 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() {
Expand Down