Fixed InsufficientStringBufferDeclaration error detection.

Summary:
Added new method to obtaint the lenght of strings added after a constructor.
Added new test for this case.

Fix: http://sourceforge.net/p/pmd/bugs/1371/

Test Plan: Run tests.

Reviewers: jmsotuyo

Reviewed By: jmsotuyo

Differential Revision: http://ph.monits.com/D12322
This commit is contained in:
José Manuel Rolón
2015-08-31 12:12:09 -03:00
committed by Andreas Dangel
parent 203e39b9b3
commit abe800dfa9
2 changed files with 36 additions and 2 deletions

View File

@ -25,7 +25,9 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
@ -60,6 +62,9 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
constructorLength = getConstructorLength(node, constructorLength);
anticipatedLength = getInitialLength(node);
anticipatedLength += getConstructorAppendsLength(node);
List<NameOccurrence> usage = node.getUsages();
Map<Node, Map<Node, Integer>> blocks = new HashMap<Node, Map<Node, Integer>>();
for (NameOccurrence no : usage) {
@ -259,8 +264,6 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
} else {
iConstructorLength = Integer.parseInt(str);
}
} else {
iConstructorLength = -1;
}
if (iConstructorLength == 0) {
@ -297,6 +300,23 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
return 0;
}
private int getConstructorAppendsLength(final Node node) {
final Node parent = node.getFirstParentOfType(ASTVariableDeclarator.class);
int size = 0;
if (parent != null) {
final Node initializer = parent.getFirstChildOfType(ASTVariableInitializer.class);
final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
for (int i = 0; i < primExp.jjtGetNumChildren(); i++) {
final Node sn = primExp.jjtGetChild(i);
if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
continue;
}
size += processNode(sn);
}
}
return size;
}
private boolean isAdditive(Node n) {
return n.hasDescendantOfType(ASTAdditiveExpression.class);
}

View File

@ -979,6 +979,20 @@ public class Test {
myVar2.append(0xdeadbeef);
myVar2.append(0xdeadbeef);
}
}
]]></code>
</test-code>
<test-code>
<description>
#1371 InsufficientStringBufferDeclaration not detected properly on StringBuffer
</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class StringBufferTest {
public void test() {
final StringBuffer stringBuffer = new StringBuffer().append("Added ").append(" a ");
stringBuffer.append("string ");
}
}
]]></code>
</test-code>