Skip to content

Commit

Permalink
Merge pull request #28 from godfather1103/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
godfather1103 authored Dec 20, 2023
2 parents 32e8599 + e8cc339 commit 846b79f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,37 @@
# 插件版本号说明

> 迭代版本 + idea版本 + P3C_PMD版本
# namelist.properties说明

```properties
# 常量字段名日志对象字段名
ConstantFieldShouldBeUpperCaseRule_LOG_VARIABLE_TYPE_SET=["Log","Logger"]
# 常量字段名的白名单
ConstantFieldShouldBeUpperCaseRule_WHITE_LIST=["serialVersionUID"]
# 驼峰需要忽略的专有名词
LowerCamelCaseVariableNamingRule_WHITE_LIST=["DAOImpl"]
# pojo的后缀
PojoMustOverrideToStringRule_POJO_SUFFIX_SET=["DO","DTO","VO","BO"]
# 忽略的魔法值列表
UndefineMagicConstantRule_LITERAL_WHITE_LIST=["0","1","\\\"\\\"","0.0","1.0","-1","0L","1L"]
# 包装类映射
MethodReturnWrapperTypeRule_PRIMITIVE_TYPE_TO_WAPPER_TYPE={"int":"Integer","boolean":"Boolean","float":"Float","double":"Double","byte":"Byte","short":"Short","long":"Long","char":"Character"}
# 需要初始化大小的对象
CollectionInitShouldAssignCapacityRule_COLLECTION_TYPE=["HashMap","ConcurrentHashMap"]
# 类名中需要排除的专有名词
ClassNamingShouldBeCamelRule_CLASS_NAMING_WHITE_LIST=["Hbase","HBase","ID"]

```

# 已修复的issues

### 本地

- [issues-27](https://github.com/godfather1103/p3c/issues/27)

### 官方

- [issues-898](https://github.com/alibaba/p3c/issues/898)
- [issues-900](https://github.com/alibaba/p3c/issues/900)

Expand Down
5 changes: 3 additions & 2 deletions idea-plugin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ plugin_name=Alibaba Java Coding Guidelines(Fix Some Bug)
gradle_jetbrains_version=1.13.3
systemProp.file.encoding=UTF-8
# \u63D2\u4EF6\u7248\u672C
plugin_version=1.4
plugin_version=1.5
# \u4F7F\u7528\u7684p3c-pmd\u7248\u672C
p3c_pmd_version=2.1.1-ext-4
p3c_pmd_version=2.1.1-ext-5
publishUsername=Jack Chu
publishToken=
#publishChannel=beta
publishChannel=
pluginSinceBuild=
pluginUntilBuild=
Expand Down
14 changes: 5 additions & 9 deletions idea-plugin/p3c-idea/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ tasks {
changeNotes.set(
"""
<ul>
1.4
<li>pmd工具升级到6.55.0</li>
<li>p3c-pmd改为直接编译,替换官方的版本</li>
<li>p3c-pmd代码优化,清除一些过时的调用</li>
<li>fix(<a href="https://github.com/godfather1103/p3c/issues/9">issues-9</a>): 1、优化Record类的检测;2、优化Switch的default块判断</li>
<li>fix(<a href="https://github.com/godfather1103/p3c/issues/6">issues-6</a>): 增加对Record类的检测</li>
1.5
<li>fix(<a href="https://github.com/godfather1103/p3c/issues/27">issues-27</a>): 优化对于方法名的驼峰校验</li>
</ul>
""".trimIndent()
)
Expand Down Expand Up @@ -80,10 +76,10 @@ tasks {

}

if (publishChannel.isNotEmpty()) {
version = "${property("plugin_version")}-${ideaVersion}-${property("p3c_pmd_version")}-${publishChannel}"
version = if (publishChannel.isNotEmpty()) {
"${property("plugin_version")}-${ideaVersion}-${property("p3c_pmd_version")}-${publishChannel}"
} else {
version = "${property("plugin_version")}-${ideaVersion}-${property("p3c_pmd_version")}"
"${property("plugin_version")}-${ideaVersion}-${property("p3c_pmd_version")}"
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.p3c.pmd.lang.java.util.namelist.NameListConfig;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -41,6 +42,8 @@ public class LowerCamelCaseVariableNamingRule extends AbstractAliRule {
private static final String MESSAGE_KEY_PREFIX = "java.naming.LowerCamelCaseVariableNamingRule.violation.msg";
private static Pattern pattern = null;

private static String clearList = "";

public LowerCamelCaseVariableNamingRule() {
if (pattern == null) {
makePattern(
Expand All @@ -54,6 +57,9 @@ public static void makePattern(List<String> list) {
Set<String> set = new HashSet<>(0);
if (list != null && !list.isEmpty()) {
set.addAll(list);
clearList = String.join("|", list);
} else {
clearList = "";
}
set.addAll(Arrays.asList("DO|DTO|VO|DAO|BO|DOList|DTOList|VOList|DAOList|BOList|X|Y|Z|UDF|UDAF|[A-Z]".split("\\|")));
pattern = Pattern.compile("^[a-z][a-z0-9]*([A-Z][a-z0-9]+)*(" + String.join("|", set) + ")?$");
Expand All @@ -78,21 +84,28 @@ public Object visit(final ASTVariableDeclaratorId node, Object data) {
if (isNotCheck) {
return super.visit(node, data);
}

String name = node.getName();
if (StringUtils.isNotEmpty(clearList)) {
name = name.replaceAll(clearList, "");
}
// variable naming violate lowerCamelCase
if (!(pattern.matcher(node.getImage()).matches())) {
if (!(pattern.matcher(name).matches())) {
ViolationUtils.addViolationWithPrecisePosition(this, node, data,
I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".variable", node.getImage()));
I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".variable", name));
}
return super.visit(node, data);
}

@Override
public Object visit(ASTMethodDeclarator node, Object data) {
if (!variableNamingStartOrEndWithDollarAndUnderLine(node.getImage())) {
if (!(pattern.matcher(node.getImage()).matches())) {
String name = node.getImage();
if (StringUtils.isNotEmpty(clearList)) {
name = name.replaceAll(clearList, "");
}
if (!variableNamingStartOrEndWithDollarAndUnderLine(name)) {
if (!(pattern.matcher(name).matches())) {
ViolationUtils.addViolationWithPrecisePosition(this, node, data,
I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".method", node.getImage()));
I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".method", name));
}
}
return super.visit(node, data);
Expand Down
8 changes: 8 additions & 0 deletions p3c-pmd/src/main/resources/namelist.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# \u5E38\u91CF\u5B57\u6BB5\u540D\u65E5\u5FD7\u5BF9\u8C61\u5B57\u6BB5\u540D
ConstantFieldShouldBeUpperCaseRule_LOG_VARIABLE_TYPE_SET=["Log","Logger"]
# \u5E38\u91CF\u5B57\u6BB5\u540D\u7684\u767D\u540D\u5355
ConstantFieldShouldBeUpperCaseRule_WHITE_LIST=["serialVersionUID"]
# \u9A7C\u5CF0\u9700\u8981\u5FFD\u7565\u7684\u4E13\u6709\u540D\u8BCD
LowerCamelCaseVariableNamingRule_WHITE_LIST=["DAOImpl"]
# pojo\u7684\u540E\u7F00
PojoMustOverrideToStringRule_POJO_SUFFIX_SET=["DO","DTO","VO","BO"]
# \u5FFD\u7565\u7684\u9B54\u6CD5\u503C\u5217\u8868
UndefineMagicConstantRule_LITERAL_WHITE_LIST=["0","1","\\\"\\\"","0.0","1.0","-1","0L","1L"]
# \u5305\u88C5\u7C7B\u6620\u5C04
MethodReturnWrapperTypeRule_PRIMITIVE_TYPE_TO_WAPPER_TYPE={"int":"Integer","boolean":"Boolean","float":"Float","double":"Double","byte":"Byte","short":"Short","long":"Long","char":"Character"}
# \u9700\u8981\u521D\u59CB\u5316\u5927\u5C0F\u7684\u5BF9\u8C61
CollectionInitShouldAssignCapacityRule_COLLECTION_TYPE=["HashMap","ConcurrentHashMap"]
# \u7C7B\u540D\u4E2D\u9700\u8981\u6392\u9664\u7684\u4E13\u6709\u540D\u8BCD
ClassNamingShouldBeCamelRule_CLASS_NAMING_WHITE_LIST=["Hbase","HBase","ID"]

0 comments on commit 846b79f

Please sign in to comment.