maven-pmd-plugin 中的 UnusedFormalParameter 与 AvoidDuplicateLiterals

UnusedFormalParameter vs. AvoidDuplicateLiterals in maven-pmd-plugin

我正在使用内置规则集 strings.xmlunusedcode.xml 以及

<?xml version="1.0"?>
<ruleset name="Custom ruleset"
        xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>
        The default ruleset
    </description>
    <rule ref="rulesets/java/strings.xml"/>
    <rule ref="rulesets/java/unusedcode.xml"/>
</ruleset>

maven-pmd-plugin如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.8</version>
    <executions>
        <execution>
            <id>pmd-check</id>
            <phase>validate</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <printFailingErrors>true</printFailingErrors>
        <detail>true</detail>
        <rulesets>
            <ruleset>${basedir}/src/main/resources/ruleset.xml</ruleset>
        </rulesets>
    </configuration>
</plugin>

所以我在以下用例中遇到以下问题:

public class NewClass {
    private final static String PMD_UNUSED_FORMAL_PARAMETER = "PMD.UnusedFormalParameter";

    @SuppressWarnings(PMD_UNUSED_FORMAL_PARAMETER)
    private void someMethod1(Object parameter1) {
        System.out.println("someMethod1");
    }

    @SuppressWarnings("PMD.UnusedFormalParameter")
    private void someMethod2(Object parameter1) {
        System.out.println("someMethod2");
    }

    @SuppressWarnings("PMD.UnusedFormalParameter")
    private void someMethod3(Object parameter1) {
        System.out.println("someMethod3");
    }

    @SuppressWarnings("PMD.UnusedFormalParameter")
    private void someMethod4(Object parameter1) {
        System.out.println("someMethod4");
    }

    public static void main(String[] args) {
        NewClass newClazz = new NewClass();
        newClazz.someMethod1(null);
        newClazz.someMethod2(null);
        newClazz.someMethod3(null);
    }
}

注释参数值 "PMD.UnusedFormalParameter" 出现 4 次会导致 AvoidDuplicateLiterals 违规。用注释掉的 private final static 常量替换字符串会导致注释无效,从而导致 UnusedFormalParameter 违规。

我不知道 PMD 的内部结构。作为一个开箱即用的用户,PMD 都没有用它的值替换变量似乎很奇怪(即使这可能是一个上下文敏感的任务,它应该在静态代码分析的范围内是可能的,它只是复杂)并且 UnusedFormalParameter 不排除字符串值,这些字符串值显然用于抑制其他检查并且仅由于 PMD 而存在。

我正在使用 maven-pmd-plugin 3.8.

对于您的问题,目前有两种方法可以解决此问题:

  1. AvoidDuplicateLiterals 允许 skipAnnotations 属性 可以设置为 true 以忽略注释中的文字。当然,这会忽略所有注释中的所有文字,而不仅仅是 @SuppressWarnings
  2. 所有规则都允许您配置 violationSuppressXPath 属性 和要忽略的表达式的 XPath 表达式。例如,在这种情况下,您可以将其设置为 AvoidDuplicateLiterals 规则以忽略 @SuppressWarnings 上的文字。我相信正确的表达方式是 //Annotation//Name[@Image = "SuppressWarnings" or @Image = "java.lang.SuppressWarnings"]/..//Literal(您必须对其进行测试以确保)

I don't know about the internals of PMD. As an out-of-the-box user it appears strange that PMD both doesn't substitute the variable with its value (even though that's probably a context sensitive task it should be possible within the scope of static code analysis, it's just complex)

你是对的,目前不支持这种行为。我只是 added an issue on Github 来跟踪请求。随意凑钱。