diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a2f534022..cad68b65e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -48,7 +48,7 @@ jobs: restore-keys: | ${{ runner.os }}-gradle- - name: Set up JDK ${{ matrix.java }} - uses: actions/setup-java@v2 + uses: actions/setup-java@v3 with: java-version: ${{ matrix.java }} distribution: adopt diff --git a/idea-plugin/README.md b/idea-plugin/README.md index adbdaeb92..eadf0df9f 100644 --- a/idea-plugin/README.md +++ b/idea-plugin/README.md @@ -21,7 +21,7 @@ cd p3c-idea ## Use p3c-common as your plugin dependency ```groovy -compile 'com.xenoamess.p3c.idea:p3c-common:2.1.1.2x' +compile 'com.xenoamess.p3c.idea:p3c-common:2.1.1.3x' ``` ## [中文使用手册](README_cn.md) ## Install @@ -157,6 +157,9 @@ Firstly p3c_config.x8l be at "$your_project_path/p3c_config.x8l". > Console + > + + com.xenoamess.cyan_potion.base.steam > EnumConstantsMustHaveCommentRule> @@ -239,6 +242,16 @@ then means in this repo we will not detect anything for all classes whose name b BE ATTENTION, according to PMD interface reason, class names in class_blacklist must be SimpleName. +### package_blacklist + +Node package_blacklist contains global settings for a repo, +means ban packages in this repo globally. + +For example, if package_blacklist contains `com.xenoamess`, +then means in this repo we will not detect anything for all classes whose package under `com.xenoamess`. + +BE ATTENTION, it will also ban packages under `com.xenoamess`, like `com.xenoamess.cyan_potion` + ### rule_class_pair_blacklist Node rule_class_pair_blacklist contains settings for class/rule pairs, @@ -307,7 +320,10 @@ If you really hate x8l you can use json configuration files. ], "class_blacklist": [ "Console" - ], + ], + "package_blacklist": [ + "com.xenoamess.cyan_potion.base.steam" + ], "rule_class_pair_blacklist": { "JamepadGamepadKeyEnum": [ "EnumConstantsMustHaveCommentRule" diff --git a/idea-plugin/README_cn.md b/idea-plugin/README_cn.md index 423927344..6700506cc 100644 --- a/idea-plugin/README_cn.md +++ b/idea-plugin/README_cn.md @@ -193,6 +193,9 @@ Alt+Enter键可呼出Intention菜单,不同的规则会提示不同信息的Qu > Console + > + + com.xenoamess.cyan_potion.base.steam > EnumConstantsMustHaveCommentRule> @@ -267,6 +270,14 @@ class_blacklist节点主要包括对该项目中的某一类禁用所有规则 注意,由于PMD技术原因,class_blacklist中的类必须为SimpleName +### package_blacklist + +package_blacklist节点主要包括对该项目中的某一个父包禁用所有规则。 + +如,package_blacklist中含有`com.xenoamess`包,则该项目中所有`com.xenoamess`包下的类均不会进行任何检测。 + +注意,形如`com.xenoamess.cyan_potion`的,`com.xenoamess`包的子包也会被禁用所有规则。 + ### rule_class_pair_blacklist rule_class_pair_blacklist节点主要包括对该项目中的某一类禁用某数个规则。 @@ -332,7 +343,10 @@ Rule类名既可以是SimpleName,也可以是CanonicalName。 ], "class_blacklist": [ "Console" - ], + ], + "package_blacklist": [ + "com.xenoamess.cyan_potion.base.steam" + ], "rule_class_pair_blacklist": { "JamepadGamepadKeyEnum": [ "EnumConstantsMustHaveCommentRule" diff --git a/idea-plugin/gradle.properties b/idea-plugin/gradle.properties index 3d3bec32c..f154a19da 100644 --- a/idea-plugin/gradle.properties +++ b/idea-plugin/gradle.properties @@ -8,4 +8,4 @@ idea_version=201.7223.91 #idea_version=145.258.11 plugin_name=Alibaba Java Coding Guidelines systemProp.file.encoding=UTF-8 -plugin_version=2.1.1.2x +plugin_version=2.1.1.3x diff --git a/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml b/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml index 0ebd3b312..06abc9e79 100644 --- a/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml +++ b/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml @@ -29,6 +29,11 @@ ]]> 2.1.1.3x +
  • pmd upgrade to 6.44.0
  • +
  • fix bugs of SneakyThrowsWithoutExceptionTypeRule
  • +
  • add com.alibaba.p3c.pmd.config>package_blacklist config. see https://github.com/XenoAmess/p3c/issues/211
  • +
      2.1.1.2x
    • pmd upgrade to 6.41.0
    • now allow use json for configuration too (p3c_config.json). see https://github.com/XenoAmess/p3c/issues/144
    • diff --git a/p3c-pmd/README.md b/p3c-pmd/README.md index 90666b80b..047e2c96b 100644 --- a/p3c-pmd/README.md +++ b/p3c-pmd/README.md @@ -11,7 +11,7 @@ com.xenoamess.p3c p3c-pmd - 2.1.1.2x + 2.1.1.3x ``` ### Gradle diff --git a/p3c-pmd/pom.xml b/p3c-pmd/pom.xml index 70e519cab..19438be5e 100644 --- a/p3c-pmd/pom.xml +++ b/p3c-pmd/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.xenoamess.p3c p3c-pmd - 2.1.1.2x + 2.1.1.3x jar p3c-pmd @@ -23,7 +23,6 @@ 6.44.0 - 1.8 1.3.2 1.6.20 2.3.9 @@ -48,7 +47,7 @@ 3.0.0-M2 3.0.0-M6 1.21 - 0.8.7 + 0.8.8 2.17.2 1.7.36 diff --git a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/config/P3cConfigDataBean.java b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/config/P3cConfigDataBean.java index 785d8146b..8971f536d 100644 --- a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/config/P3cConfigDataBean.java +++ b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/config/P3cConfigDataBean.java @@ -83,6 +83,15 @@ HashMap> getRuleClassPairBlackListMap(@NotNull List ) private Set classBlackListSet; + @X8lDataBeanFieldMark( + paths = { + "com.alibaba.p3c.pmd.config>package_blacklist" + }, + parser = P3cConfigDataBean.class, + functionName = "getContentNodeAsStringSet" + ) + private Set packageBlackListSet; + @X8lDataBeanFieldMark( paths = { "com.alibaba.p3c.pmd.config>rule_class_pair_blacklist" @@ -145,4 +154,13 @@ public Map> getRuleClassPairBlackListMap() { public void setRuleClassPairBlackListMap(Map> ruleClassPairBlackListMap) { this.ruleClassPairBlackListMap = ruleClassPairBlackListMap; } + + public Set getPackageBlackListSet() { + return packageBlackListSet; + } + + public void setPackageBlackListSet(Set packageBlackListSet) { + this.packageBlackListSet = packageBlackListSet; + } + } diff --git a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/SneakyThrowsWithoutExceptionTypeRule.java b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/SneakyThrowsWithoutExceptionTypeRule.java index 5a25b9362..6a89991db 100644 --- a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/SneakyThrowsWithoutExceptionTypeRule.java +++ b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/SneakyThrowsWithoutExceptionTypeRule.java @@ -30,7 +30,6 @@ public Object visit(ASTCompilationUnit rootNode, Object data) { + " not((.//MemberValuePair/@Image = 'value') or (./SingleMemberAnnotation))" + "]" + "]" - + "//MethodDeclaration" ); } catch (JaxenException e) { e.printStackTrace(); diff --git a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/ViolationUtils.java b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/ViolationUtils.java index 821c2e2ab..c8335f638 100644 --- a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/ViolationUtils.java +++ b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/ViolationUtils.java @@ -17,6 +17,7 @@ import com.xenoamess.p3c.pmd.lang.java.util.namelist.NameListConfig; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; @@ -25,6 +26,7 @@ import net.sourceforge.pmd.lang.rule.AbstractRule; import net.sourceforge.pmd.lang.symboltable.Scope; import net.sourceforge.pmd.lang.symboltable.ScopedNode; +import org.jetbrains.annotations.Nullable; /** * @author caikang @@ -67,6 +69,14 @@ public static boolean shouldIgnoreViolation(Class ruleClass, Node node) { return true; } + ASTCompilationUnit nodeRoot = getNodeRoot(node); + if (nodeRoot != null) { + String packageName = nodeRoot.getPackageName(); + if (NameListConfig.getNameListService().ifPackageNameInPackageBlackList(packageName)) { + return true; + } + } + if (node instanceof ScopedNode) { ScopedNode scopedNode = (ScopedNode) node; Scope scope = scopedNode.getScope(); @@ -85,4 +95,16 @@ public static boolean shouldIgnoreViolation(Class ruleClass, Node node) { return false; } + + @Nullable + private static ASTCompilationUnit getNodeRoot(@Nullable Node node) { + while (node != null) { + if (node instanceof ASTCompilationUnit) { + return (ASTCompilationUnit) node; + } + node = node.getParent(); + } + return null; + } + } diff --git a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListService.java b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListService.java index c411af3d8..0434ba748 100644 --- a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListService.java +++ b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListService.java @@ -16,6 +16,7 @@ package com.xenoamess.p3c.pmd.lang.java.util.namelist; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.util.List; @@ -65,6 +66,13 @@ void loadPatchConfigFile( */ boolean ifClassNameInClassBlackList(String className); + /** + * check if package name is in package black list. + * @param packageName package name + * @return true if in package black list + */ + boolean ifPackageNameInPackageBlackList(@Nullable String packageName); + /** * check if class name is in class name pair black list. * @param ruleClass rule class diff --git a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListServiceImpl.java b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListServiceImpl.java index 88c938ba9..57fd3a213 100644 --- a/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListServiceImpl.java +++ b/p3c-pmd/src/main/java/com/xenoamess/p3c/pmd/lang/java/util/namelist/NameListServiceImpl.java @@ -23,6 +23,7 @@ import com.xenoamess.x8l.dealers.X8lDealer; import org.apache.commons.io.IOUtils; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; @@ -114,6 +115,22 @@ public boolean ifClassNameInClassBlackList(String className) { return this.getP3cConfigDataBean().getClassBlackListSet().contains(className); } + @Override + public boolean ifPackageNameInPackageBlackList(@Nullable String packageName) { + if (packageName == null) { + return false; + } + Set packageBlackListSet = this.getP3cConfigDataBean().getPackageBlackListSet(); + if (packageBlackListSet != null) { + for (String packageBlack : packageBlackListSet) { + if (packageName.startsWith(packageBlack)) { + return true; + } + } + } + return false; + } + @Override public boolean ifRuleClassNameClassNamePairInPairIgnoreList(Class ruleClass, String className) { if (getP3cConfigDataBean().getRuleClassPairBlackListMap().containsKey(className)) { diff --git a/p3c-pmd/src/test/resources/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/xml/SneakyThrowsWithoutExceptionTypeRule.xml b/p3c-pmd/src/test/resources/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/xml/SneakyThrowsWithoutExceptionTypeRule.xml index d83a98abe..b8d1a08e3 100644 --- a/p3c-pmd/src/test/resources/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/xml/SneakyThrowsWithoutExceptionTypeRule.xml +++ b/p3c-pmd/src/test/resources/com/xenoamess/p3c/pmd/lang/java/rule/xenoamess/additional/xml/SneakyThrowsWithoutExceptionTypeRule.xml @@ -18,6 +18,7 @@ testFail1 1 + 4 @@ -36,6 +37,7 @@ testFail2 1 + 4 @@ -54,6 +56,7 @@ testFail3 1 + 4 @@ -70,6 +73,7 @@ testFail4 1 + 2