pmd: fix #1063 False+: ArrayIsStoredDirectly

This commit is contained in:
Andreas Dangel
2013-04-28 09:52:39 +02:00
parent 5fb6daea58
commit dba46f2ebd
3 changed files with 44 additions and 14 deletions

View File

@ -1,6 +1,7 @@
????? ??, 2013 - 5.0.4:
Fixed bug 254: False+ : UnusedImport with Javadoc @throws
Fixed bug 1063: False+: ArrayIsStoredDirectly
Fixed bug 1080: net.sourceforge.pmd.cpd.CPDTest test failing
Fixed bug 1081: Regression: CPD skipping all files when using relative paths
Fixed bug 1082: CPD performance issue on larger projects

View File

@ -19,6 +19,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
@ -63,6 +64,25 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule {
}
}
private String getExpressionVarName(Node e) {
String assignedVar = getFirstNameImage(e);
if (assignedVar == null) {
ASTPrimarySuffix suffix = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (suffix != null) {
assignedVar = suffix.getImage();
ASTPrimaryPrefix prefix = e.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (prefix != null) {
if (prefix.usesThisModifier()) {
assignedVar = "this." + assignedVar;
} else if (prefix.usesSuperModifier()) {
assignedVar = "super." + assignedVar;
}
}
}
}
return assignedVar;
}
/**
* Checks if the variable designed in parameter is written to a field (not local variable) in the statements.
*/
@ -76,13 +96,9 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule {
continue;
}
ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
String assignedVar = getFirstNameImage(pe);
String assignedVar = getExpressionVarName(pe);
if (assignedVar == null) {
ASTPrimarySuffix suffix = se.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (suffix == null) {
continue;
}
assignedVar = suffix.getImage();
continue;
}
Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class);
@ -103,14 +119,7 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule {
if (e.hasDescendantOfType(ASTEqualityExpression.class)) {
continue;
}
String val = getFirstNameImage(e);
if (val == null) {
ASTPrimarySuffix foo = se.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (foo == null) {
continue;
}
val = foo.getImage();
}
String val = getExpressionVarName(e);
if (val == null) {
continue;
}

View File

@ -133,4 +133,24 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>#1063 False+: ArrayIsStoredDirectly</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.Arrays;
public class TestClass {
private final Object[] obj;
public TestClass(Object[] obj) {
if (obj == null) {
this.obj = new Object[]{};
} else {
this.obj = Arrays.copyOf(obj, obj.length);
}
}
}
]]></code>
</test-code>
</test-data>