#1409 NullPointerException in InsufficientStringBufferRule

This commit is contained in:
Andreas Dangel 2015-09-20 20:51:09 +02:00
parent 365e7a1f68
commit d8d8a9539f
3 changed files with 26 additions and 7 deletions

View File

@ -305,13 +305,17 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
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);
if (initializer != null) {
final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
if (primExp != null) {
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;

View File

@ -1023,6 +1023,19 @@ public class StringBuilderWithMessageRetrieval {
}
}
}
]]></code>
</test-code>
<test-code>
<description>#1409 NullPointerException in InsufficientStringBufferRule</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class NullPointer {
public void run(String[] strings) {
StringBuilder builder;
builder = new StringBuilder();
}
}
]]></code>
</test-code>

View File

@ -18,6 +18,8 @@
* java-controversial/DefaultPackage:
[#1410](https://sourceforge.net/p/pmd/bugs/1410/): DefaultPackage triggers on field annotated with @VisibleForTesting
* java-strings/InsufficientStringBufferDeclaration:
[#1409](https://sourceforge.net/p/pmd/bugs/1409/): NullPointerException in InsufficientStringBufferRule
**API Changes:**