#1384 NullPointerException in ConsecutiveLiteralAppendsRule

This commit is contained in:
Andreas Dangel
2015-08-01 20:28:38 +02:00
parent c5216fed79
commit 89a3d5dd1e
3 changed files with 29 additions and 1 deletions

View File

@ -90,7 +90,9 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule {
threshold = getProperty(THRESHOLD_DESCRIPTOR);
int concurrentCount = checkConstructor(node, data);
concurrentCount += checkInitializerExpressions(node);
if (hasInitializer(node)) {
concurrentCount += checkInitializerExpressions(node);
}
Node lastBlock = getFirstParentBlock(node);
Node currentBlock = lastBlock;
Map<VariableNameDeclaration, List<NameOccurrence>> decls = node.getScope().getDeclarations(
@ -219,6 +221,10 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule {
return result;
}
private boolean hasInitializer(ASTVariableDeclaratorId node) {
return node.jjtGetParent().hasDescendantOfType(ASTVariableInitializer.class);
}
private int processAdditive(Object data, int concurrentCount, Node sn, Node rootNode) {
ASTAdditiveExpression additive = sn.getFirstDescendantOfType(ASTAdditiveExpression.class);
// The additive expression must of be type String to count

View File

@ -1262,6 +1262,26 @@ public class StringBufferTest {
stringBuffer2.append("string ");
Log.i(TAG, stringBuffer2.toString());
}
}
]]></code>
</test-code>
<test-code>
<description>#1384 NullPointerException in ConsecutiveLiteralAppendsRule - 1</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class ConsecutiveLiteralAppendsRuleTest {
private StringBuilder cdata;
}
]]></code>
</test-code>
<test-code>
<description>#1384 NullPointerException in ConsecutiveLiteralAppendsRule - 2</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class ConsecutiveLiteralAppendsRuleTest {
public void appendChar(final StringBuilder s) {
// do nothing
}
}
]]></code>
</test-code>

View File

@ -12,4 +12,6 @@
**Bugfixes:**
* [#1384](https://sourceforge.net/p/pmd/bugs/1384/): NullPointerException in ConsecutiveLiteralAppendsRule
**API Changes:**