提交 c71f8f32 编写于 作者: 昶乐

同步改动

上级 4d8a8a09
......@@ -19,6 +19,7 @@ import java.util.regex.Pattern;
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 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
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 class LowerCamelCaseVariableNamingRule extends AbstractAliRule {
}
@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 class LowerCamelCaseVariableNamingRule extends AbstractAliRule {
//对所有注解内的内容不做检查
return null;
}
private boolean variableNamingStartOrEndWithDollarAndUnderLine(String variable) {
return variable.startsWith(StringAndCharConstants.DOLLAR)
|| variable.startsWith(StringAndCharConstants.UNDERSCORE);
}
}
......@@ -26,4 +26,6 @@ public final class StringAndCharConstants {
}
public static final char DOT = '.';
public static final String DOLLAR = "$";
public static final String UNDERSCORE = "_";
}
<test-data>
<test-data xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests https://pmd.sourceforge.io/rule-tests_1_0_0.xsd">
<code-fragment id="LowerCamelCaseVariableNamingRuleTest">
<![CDATA[
public class VariableNameRuleTest {
private String abC;
private String AbC;
private String abCd;
private String locationA;
private void f(){
String s = "test";
}
......@@ -14,8 +17,8 @@
<test-code>
<description>Variable name should be lowerCamelCase</description>
<expected-problems>1</expected-problems>
<code-ref id="LowerCamelCaseVariableNamingRuleTest" />
<expected-linenumbers>3</expected-linenumbers>
<code-ref id="LowerCamelCaseVariableNamingRuleTest" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest1">
......@@ -59,8 +62,8 @@ public class PluginConstants {
<test-code>
<description>Variable name should be lowerCamelCase3</description>
<expected-problems>2</expected-problems>
<code-ref id="LowerCamelCaseVariableNamingRuleTest3" />
<expected-linenumbers>2,3</expected-linenumbers>
<code-ref id="LowerCamelCaseVariableNamingRuleTest3" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest4">
......@@ -116,7 +119,7 @@ public interface BizConstants {
public class MockTest{
@Mock
void $clinit(){}
}
}
]]>
</code-fragment>
<test-code>
......@@ -141,32 +144,27 @@ public interface BizConstants {
<code-ref id="LowerCamelCaseVariableNamingRuleTest8" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest8">
<code-fragment id="VariableNamingStartOrEndWithDollarAndUnderLine">
<![CDATA[
public @interface TYPE {
int DO_NO_THING = 0;
int DO_ONE_START_TO_END_ROUTE = 1;
int DO_ONE_CAR_TO_START_ROUTE = 2;
int DO_TWO_ROUTE = 3;
public class Example {
public void test(){
String $myName = "zhangsan";
int _myAge = 18;
}
}
]]>
</code-fragment>
<test-code>
<description>Variable name should be lowerCamelCase8</description>
<description>Variable Naming Start Or End With Dollar And UnderLine</description>
<expected-problems>0</expected-problems>
<code-ref id="LowerCamelCaseVariableNamingRuleTest8" />
<code-ref id="VariableNamingStartOrEndWithDollarAndUnderLine" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest9">
<![CDATA[
@Document
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface TYPE {
int DO_NO_THING = 0;
int DO_ONE_START_TO_END_ROUTE = 1;
int DO_ONE_CAR_TO_START_ROUTE = 2;
int DO_TWO_ROUTE = 3;
public @interface ValueType {
int TYPE_NULL = 0;
}
]]>
</code-fragment>
......@@ -178,22 +176,87 @@ public interface BizConstants {
<code-fragment id="LowerCamelCaseVariableNamingRuleTest10">
<![CDATA[
@Document
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public class Type {
int DO_NO_THING = 0;
int DO_ONE_START_TO_END_ROUTE = 1;
int DO_ONE_CAR_TO_START_ROUTE = 2;
int DO_TWO_ROUTE = 3;
public class Example {
public void test(){
String myNAME = "zhangsan";
}
}
]]>
</code-fragment>
<test-code>
<description>Variable name should be lowerCamelCase10</description>
<expected-problems>4</expected-problems>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code-ref id="LowerCamelCaseVariableNamingRuleTest10" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest11">
<![CDATA[
public class Example {
public void test(){
String myDOList = "DOList";
String myDTOList = "DTOList";
String myVOList = "VOList";
String myDAOList = "DAOList";
String myBOList = "BOList";
}
public void getScrollX(){}
public void getScrollY(){}
public void getScrollZ(){}
}
]]>
</code-fragment>
<test-code>
<description>Variable name should be lowerCamelCase11</description>
<expected-problems>0</expected-problems>
<code-ref id="LowerCamelCaseVariableNamingRuleTest11" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest12">
<![CDATA[
public class Example {
public void test(){
String myBO = "myBO";
}
}
]]>
</code-fragment>
<test-code>
<description>Variable name should be lowerCamelCase12</description>
<expected-problems>0</expected-problems>
<code-ref id="LowerCamelCaseVariableNamingRuleTest12" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest13">
<![CDATA[
public class Example {
public void test(){
String myUDF = "myUDF";
}
}
]]>
</code-fragment>
<test-code>
<description>Variable name should be lowerCamelCase13</description>
<expected-problems>0</expected-problems>
<code-ref id="LowerCamelCaseVariableNamingRuleTest13" />
</test-code>
<code-fragment id="LowerCamelCaseVariableNamingRuleTest14">
<![CDATA[
public class Example {
public void test(){
String myUDAF = "myUDAF";
}
}
]]>
</code-fragment>
<test-code>
<description>Variable name should be lowerCamelCase14</description>
<expected-problems>0</expected-problems>
<code-ref id="LowerCamelCaseVariableNamingRuleTest14" />
</test-code>
</test-data>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册