From 72cc961ca83ba16ca3ede2ed39b9ee2fa3e1fd37 Mon Sep 17 00:00:00 2001 From: Romain Pelisse Date: Wed, 28 Jan 2009 20:51:58 +0000 Subject: [PATCH] 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 --- pmd/etc/changelog.txt | 1 + .../rules/naming/xml/VariableNamingConventions.xml | 11 +++++++++++ .../pmd/rules/VariableNamingConventions.java | 14 ++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 9cdd3f8b59..0fbbf7579a 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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: diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/naming/xml/VariableNamingConventions.xml b/pmd/regress/test/net/sourceforge/pmd/rules/naming/xml/VariableNamingConventions.xml index c2c83ce561..62a4a9cc93 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/naming/xml/VariableNamingConventions.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/naming/xml/VariableNamingConventions.xml @@ -115,4 +115,15 @@ public class Foo { } ]]> + + + 1 + + diff --git a/pmd/src/net/sourceforge/pmd/rules/VariableNamingConventions.java b/pmd/src/net/sourceforge/pmd/rules/VariableNamingConventions.java index a108688e3e..d5e6c229cb 100644 --- a/pmd/src/net/sourceforge/pmd/rules/VariableNamingConventions.java +++ b/pmd/src/net/sourceforge/pmd/rules/VariableNamingConventions.java @@ -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) {