diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java index 1ae3c0e16..e009beff4 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java @@ -19,6 +19,7 @@ import com.alibaba.p3c.pmd.I18nResources; import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule; +import com.alibaba.p3c.pmd.lang.java.util.StringAndCharConstants; import com.alibaba.p3c.pmd.lang.java.util.ViolationUtils; import net.sourceforge.pmd.lang.ast.Node; @@ -38,10 +39,14 @@ public class LowerCamelCaseVariableNamingRule extends AbstractAliRule { private static final String MESSAGE_KEY_PREFIX = "java.naming.LowerCamelCaseVariableNamingRule.violation.msg"; - private Pattern pattern = Pattern.compile("^[a-z|$][a-z0-9]*([A-Z][a-z0-9]*)*(DO|DTO|VO|DAO)?$"); + private Pattern pattern = Pattern.compile("^[a-z][a-z0-9]*([A-Z][a-z0-9]+)*(DO|DTO|VO|DAO|BO|DOList|DTOList|VOList|DAOList|BOList|X|Y|Z|UDF|UDAF|[A-Z])?$"); @Override public Object visit(final ASTVariableDeclaratorId node, Object data) { + //避免与 AvoidStartWithDollarAndUnderLineNamingRule 重复判断(例: $myTest) + if (variableNamingStartOrEndWithDollarAndUnderLine(node.getImage())) { + return super.visit(node, data); + } // Constant named does not apply to this rule ASTTypeDeclaration typeDeclaration = node.getFirstParentOfType(ASTTypeDeclaration.class); Node jjtGetChild = typeDeclaration.jjtGetChild(0); @@ -65,11 +70,12 @@ public Object visit(final ASTVariableDeclaratorId node, Object data) { } @Override - public Object visit(ASTMethodDeclarator node, Object data) { - if (!(pattern.matcher(node.getImage()).matches())) { - ViolationUtils.addViolationWithPrecisePosition(this, node, data, - I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".method", node.getImage())); + if (!variableNamingStartOrEndWithDollarAndUnderLine(node.getImage())) { + if (!(pattern.matcher(node.getImage()).matches())) { + ViolationUtils.addViolationWithPrecisePosition(this, node, data, + I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".method", node.getImage())); + } } return super.visit(node, data); } @@ -79,4 +85,9 @@ public Object visit(ASTAnnotationTypeDeclaration node, Object data) { //对所有注解内的内容不做检查 return null; } + + private boolean variableNamingStartOrEndWithDollarAndUnderLine(String variable) { + return variable.startsWith(StringAndCharConstants.DOLLAR) + || variable.startsWith(StringAndCharConstants.UNDERSCORE); + } } diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/util/StringAndCharConstants.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/util/StringAndCharConstants.java index c7b4757db..6b0303dc3 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/util/StringAndCharConstants.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/util/StringAndCharConstants.java @@ -26,4 +26,6 @@ private StringAndCharConstants(){ } public static final char DOT = '.'; + public static final String DOLLAR = "$"; + public static final String UNDERSCORE = "_"; } diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml index effc1b686..1dc8755ae 100644 --- a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml @@ -1,10 +1,13 @@ - + Variable name should be lowerCamelCase 1 - 3 + @@ -59,8 +62,8 @@ public class PluginConstants { Variable name should be lowerCamelCase3 2 - 2,3 + @@ -116,7 +119,7 @@ public interface BizConstants { public class MockTest{ @Mock void $clinit(){} - } + } ]]> @@ -141,32 +144,27 @@ public interface BizConstants { - + - Variable name should be lowerCamelCase8 + Variable Naming Start Or End With Dollar And UnderLine 0 - + + @@ -178,22 +176,87 @@ public interface BizConstants { Variable name should be lowerCamelCase10 - 4 + 1 + 3 + + + + + + Variable name should be lowerCamelCase11 + 0 + + + + + + + + Variable name should be lowerCamelCase12 + 0 + + + + + + + + Variable name should be lowerCamelCase13 + 0 + + + + + + + + Variable name should be lowerCamelCase14 + 0 + + +