diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 93ed23a0f6..cf64c6100c 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java index 62fbd53f7a..ab6d54f558 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java @@ -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; } diff --git a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml index 13560cc641..6928431507 100644 --- a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml +++ b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml @@ -133,4 +133,24 @@ public class Foo { } ]]> + + + #1063 False+: ArrayIsStoredDirectly + 0 + +