forked from phoedos/pmd
#1345 UseCollectionIsEmpty throws NullPointerException
This commit is contained in:
@ -104,14 +104,32 @@ public abstract class AbstractInefficientZeroCheck extends AbstractJavaRule {
|
||||
private boolean isCompare(Node equality) {
|
||||
if (isLiteralLeftHand(equality)) {
|
||||
return checkComparison(inverse.get(equality.getImage()), equality, 0);
|
||||
} else {
|
||||
} else if (isLiteralRightHand(equality)) {
|
||||
return checkComparison(equality.getImage(), equality, 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isLiteralLeftHand(Node equality) {
|
||||
return equality.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0
|
||||
&& equality.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTLiteral;
|
||||
return isLiteral(equality, 0);
|
||||
}
|
||||
|
||||
private boolean isLiteralRightHand(Node equality) {
|
||||
return isLiteral(equality, 1);
|
||||
}
|
||||
|
||||
private boolean isLiteral(Node equality, int child) {
|
||||
Node target = equality.jjtGetChild(child);
|
||||
target = getFirstChildOrThis(target);
|
||||
target = getFirstChildOrThis(target);
|
||||
return target instanceof ASTLiteral;
|
||||
}
|
||||
|
||||
private Node getFirstChildOrThis(Node node) {
|
||||
if (node.jjtGetNumChildren() > 0) {
|
||||
return node.jjtGetChild(0);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,7 +144,10 @@ public abstract class AbstractInefficientZeroCheck extends AbstractJavaRule {
|
||||
* @see #getComparisonTargets()
|
||||
*/
|
||||
private boolean checkComparison(String operator, Node equality, int i) {
|
||||
Node target = equality.jjtGetChild(i).jjtGetChild(0).jjtGetChild(0);
|
||||
Node target = equality
|
||||
.jjtGetChild(i)
|
||||
.jjtGetChild(0)
|
||||
.jjtGetChild(0);
|
||||
return target instanceof ASTLiteral && getComparisonTargets().get(operator).contains(target.getImage());
|
||||
}
|
||||
|
||||
|
@ -291,6 +291,29 @@ public class PMDIsEmptyFalsePositive {
|
||||
// do something
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#1345 UseCollectionIsEmpty throws NullPointerException</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
package org.oikarinen.pmdbugbait;
|
||||
import java.util.List;
|
||||
|
||||
public class PmdBugBait {
|
||||
public int getSize() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* "this." before the method call triggers the bug
|
||||
*/
|
||||
public void compareSizeToThisPointMethod(List<String> list) {
|
||||
if (list.size() < this.getSize()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
@ -14,5 +14,6 @@
|
||||
|
||||
* [#1335](https://sourceforge.net/p/pmd/bugs/1335/): GuardLogStatementJavaUtil should not apply to SLF4J Logger
|
||||
* [#1342](https://sourceforge.net/p/pmd/bugs/1342/): UseConcurrentHashMap false positive (with documentation example)
|
||||
* [#1345](https://sourceforge.net/p/pmd/bugs/1345/): UseCollectionIsEmpty throws NullPointerException
|
||||
|
||||
**API Changes:**
|
||||
|
Reference in New Issue
Block a user