Fix for bug 2225474 - VariableNamingConventions does not work with nonprimitives

- Adding a test case to reproduce the issue ;
- Fix ;
- Changelog updated.
Special thanks to Markus Kling for pointing this fix to me.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6807 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse 2009-01-28 20:51:58 +00:00
parent e26de08062
commit 72cc961ca8
3 changed files with 22 additions and 4 deletions

View File

@ -7,6 +7,7 @@ Fixed bug 2338341 - ArrayIndexOutOfBoundsException in CPD (on Ruby)
Fixed bug 2315599 - False +: UseSingleton with class containing constructor
Fixed bug 1955852 - false positives for UnusedPrivateMethod & UnusedLocalVariable
Fixed bug 2404700 - UseSingleton should not act on enums
Fixed bug 2225474 - VariableNamingConventions does not work with nonprimitives
Fixed bug - JUnitTestsShouldIncludeAssert now detects Junit 4 Assert.assert... constructs
October 12, 2008 - 4.2.4:

View File

@ -115,4 +115,15 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
False - with non primitive fields (Bug 2225474)
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class X {
public final static String foo = "3";
}
]]></code>
</test-code>
</test-data>

View File

@ -8,9 +8,10 @@ import java.util.Map;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.PropertyDescriptor;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTReferenceType;
import net.sourceforge.pmd.ast.ASTPrimitiveType;
import net.sourceforge.pmd.ast.ASTType;
import net.sourceforge.pmd.ast.ASTVariableDeclarator;
@ -71,9 +72,14 @@ public class VariableNamingConventions extends AbstractRule {
private Object checkNames(ASTFieldDeclaration node, Object data) {
ASTType childNodeType = (ASTType) node.jjtGetChild(0);
String varType = "";
if (childNodeType.jjtGetChild(0) instanceof ASTName) {
varType = ((ASTName) childNodeType.jjtGetChild(0)).getImage();
} else if (childNodeType.jjtGetChild(0) instanceof ASTPrimitiveType) {
if (childNodeType.jjtGetChild(0) instanceof ASTReferenceType ) {
ASTReferenceType refType = ((ASTReferenceType) childNodeType.jjtGetChild(0));
if ( refType.jjtGetChild(0) instanceof ASTClassOrInterfaceType ) {
varType = ((ASTClassOrInterfaceType)refType.jjtGetChild(0)).getImage();
} else {
varType = "";
}
} else if (childNodeType.jjtGetChild(0) instanceof ASTPrimitiveType) {
varType = ((ASTPrimitiveType) childNodeType.jjtGetChild(0)).getImage();
}
if (varType != null && varType.length() > 0) {