Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
可爱+小可爱
p3c
提交
c71f8f32
P
p3c
项目概览
可爱+小可爱
/
p3c
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
p3c
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c71f8f32
编写于
12月 05, 2019
作者:
昶
昶乐
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
同步改动
上级
4d8a8a09
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
110 addition
and
34 deletion
+110
-34
p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java
...ng/java/rule/naming/LowerCamelCaseVariableNamingRule.java
+16
-5
p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/util/StringAndCharConstants.java
...libaba/p3c/pmd/lang/java/util/StringAndCharConstants.java
+2
-0
p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml
...java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml
+92
-29
未找到文件。
p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java
浏览文件 @
c71f8f32
...
...
@@ -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
);
}
}
p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/util/StringAndCharConstants.java
浏览文件 @
c71f8f32
...
...
@@ -26,4 +26,6 @@ public final class StringAndCharConstants {
}
public
static
final
char
DOT
=
'.'
;
public
static
final
String
DOLLAR
=
"$"
;
public
static
final
String
UNDERSCORE
=
"_"
;
}
p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml
浏览文件 @
c71f8f32
<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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录