Skip to content
Open
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 @@ -75,7 +75,7 @@ public void visit(BinaryExpr n, VisitorContext ctx) {

if (Collection.class.isAssignableFrom(msd.getMethod().getDeclaringClass())) {
Expression newExpr = new MethodCallExpr(mce.getScope(), "isEmpty");
if (n.getOperator().equals(BinaryExpr.Operator.notEquals)) {
if (n.getOperator().equals(BinaryExpr.Operator.notEquals) || n.getOperator().equals(BinaryExpr.Operator.greater)) {
newExpr = new UnaryExpr(newExpr, UnaryExpr.Operator.not);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.walkmod.javalang.ast.expr.BinaryExpr;
import org.walkmod.javalang.ast.expr.EnclosedExpr;
import org.walkmod.javalang.ast.expr.MethodCallExpr;
import org.walkmod.javalang.ast.expr.UnaryExpr;
import org.walkmod.javalang.ast.stmt.BlockStmt;
import org.walkmod.javalang.ast.stmt.ReturnStmt;
import org.walkmod.javalang.test.SemanticTest;
Expand All @@ -32,6 +33,25 @@ public void testEqualsToZero() throws Exception {

Assert.assertEquals("isEmpty", mce.getName());
}

@Test
public void testMoreThanZero() throws Exception {
CompilationUnit cu = compile(
"import java.util.List; public class Foo { public boolean testIsEmpty(List list){ return list.size() > 0; }}");

UseCollectionIsEmpty visitor = new UseCollectionIsEmpty();
cu.accept(visitor, null);

MethodDeclaration md = (MethodDeclaration) cu.getTypes().get(0).getMembers().get(0);

BlockStmt block = md.getBody();
ReturnStmt returnStmt = (ReturnStmt) block.getStmts().get(0);

UnaryExpr ue = (UnaryExpr)returnStmt.getExpr();
MethodCallExpr mce = (MethodCallExpr)ue.getExpr();
Assert.assertTrue(ue.getOperator() == UnaryExpr.Operator.not);
Assert.assertEquals("isEmpty", mce.getName());
}

@Test
public void testEqualsToZeroWithOtherBinOp() throws Exception {
Expand Down