1573591 - NonThreadSafeSingleton throws NullPointerException

Using this keyword messed things up - modified to use Wouter's suggestion, works nice


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4606 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2006-10-10 00:07:26 +00:00
parent 0e9d2882d5
commit 65e1f026c1
3 changed files with 25 additions and 3 deletions

View File

@ -8,6 +8,7 @@ Fixed bug 1571324 - UselessStringValueOf no longer reports a false positive for
Fixed CSVRenderer - had flipped line and priority columns
Applied patch 1551189 - SingularField false + for initialization blocks
Fixed bug 1573795 - PreserveStackTrace doesn't throw CastClassException on exception with 0 args
Fixed bug 1573591 - NonThreadSafeSingleton doesn't throw NPE when using this keyword
October 4, 2006 - 3.8:
New rules:

View File

@ -23,6 +23,7 @@ public class NonThreadSafeSingletonTest extends SimpleAggregatorTst {
new TestDescriptor(TEST5, "failure case, two if statements", 1, rule),
new TestDescriptor(TEST6, "failure case, compound if statement", 1, rule),
new TestDescriptor(TEST7, "failure case 2", 1, rule),
new TestDescriptor(TEST8, "From defect 1573591", 0, rule),
});
}
@ -99,5 +100,17 @@ public class NonThreadSafeSingletonTest extends SimpleAggregatorTst {
" return buz;" + PMD.EOL +
" }" + PMD.EOL +
"}";
public static final String TEST8 =
"public class A {" + PMD.EOL +
"public final static String FOO = \"0\";" + PMD.EOL +
"private String bar;" + PMD.EOL +
"public void bla() {" + PMD.EOL +
"if (this.bar == null) {" + PMD.EOL +
"this.bar = FOO;" + PMD.EOL +
"}" + PMD.EOL +
"}" + PMD.EOL +
"}";
}

View File

@ -13,6 +13,7 @@ import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTNullLiteral;
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.ast.ASTStatementExpression;
import net.sourceforge.pmd.ast.ASTSynchronizedStatement;
@ -75,10 +76,17 @@ public class NonThreadSafeSingleton extends AbstractRule {
ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
if (expr.jjtGetChild(0).getClass().equals(ASTPrimaryExpression.class) && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0).getClass().equals(ASTPrimaryPrefix.class)) {
ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
ASTName name = (ASTName) pp.jjtGetChild(0);
if (fieldDecls.containsKey(name.getImage())) {
violation = true;
String name = null;
if (pp.usesThisModifier()) {
ASTPrimarySuffix priSuf = (ASTPrimarySuffix)expr.getFirstChildOfType(ASTPrimarySuffix.class);
name = priSuf.getImage();
} else {
ASTName astName = (ASTName) pp.jjtGetChild(0);
name = astName.getImage();
}
if (fieldDecls.containsKey(name)) {
violation = true;
}
}
}
if (violation) {