pmd: fix #1084 NPE at UselessStringValueOfRule.java:36

This commit is contained in:
Andreas Dangel
2013-08-03 18:50:33 +02:00
parent a417f0003f
commit e76947b297
3 changed files with 29 additions and 5 deletions

View File

@ -1,6 +1,7 @@
????? ??, 2013 - 5.0.5:
Fixed bug 991: AvoidSynchronizedAtMethodLevel for static methods
Fixed bug 1084: NPE at UselessStringValueOfRule.java:36
Fixed bug 1104: IdempotentOperation false positive
Fixed bug 1111: False positive: Useless parentheses
Fixed bug 1114: CPD - Tokenizer not initialized with requested properties

View File

@ -10,6 +10,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
public class UselessStringValueOfRule extends AbstractJavaRule {
@ -33,11 +34,14 @@ public class UselessStringValueOfRule extends AbstractJavaRule {
if (args != null) {
ASTName arg = args.getFirstDescendantOfType(ASTName.class);
if (arg != null) {
ASTType argType = arg.getNameDeclaration().getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
if (argType != null
&& argType.jjtGetChild(0) instanceof ASTReferenceType
&& ((ASTReferenceType)argType.jjtGetChild(0)).isArray()) {
return super.visit(node, data);
NameDeclaration declaration = arg.getNameDeclaration();
if (declaration != null) {
ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
if (argType != null
&& argType.jjtGetChild(0) instanceof ASTReferenceType
&& ((ASTReferenceType)argType.jjtGetChild(0)).isArray()) {
return super.visit(node, data);
}
}
}
}

View File

@ -91,6 +91,25 @@ class TestClass {
String abc = "1" + String.valueOf(idFormat);
System.out.println(abc); // Output 100000
}
}
]]></code>
</test-code>
<test-code>
<description>#1084 NPE at UselessStringValueOfRule.java:36</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import android.os.Build;
public class Test {
public String test() {
print(String.valueOf(Build.TIME));
print(String.valueOf(Build.VERSION.SDK_INT));
}
private void print(String s) {
System.out.println(s);
}
}
]]></code>
</test-code>