为什么明明使用了未使用的方法却出现 PMD 违规

PMD 失败:...规则:UnusedPrivateMethod 优先级:3 避免未使用的私有方法,例如“printMyString(String)”


private void anyMethod() {

    var myString = "a String";

    printMyString(myString);

}


private void printMyString(String string) {

    System.out.println(string);

}

使用maven这个插件


            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-pmd-plugin</artifactId>

            <version>3.12.0</version>


慕村9548890
浏览 55回答 1
1回答

开心每一天1111

这似乎是 PMD 中的一个错误,因为它在通过推断的“var”跟踪变量类型时存在问题。目标方法具有明确定义的参数。我可以通过禁用特定的 PMD 规则来解决这个问题。在 pom.xml 中,我修改 PMD 插件以使用本地规则文件。&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-pmd-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.12.0</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <linkXRef>false</linkXRef>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <printFailingErrors>true</printFailingErrors>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <failOnViolation>true</failOnViolation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <rulesets>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ruleset>${basedir}/PMD.xml</ruleset>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </rulesets>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>check</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>cpd-check</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>以及 PMD.xml 文件(位于项目的根目录中)。<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Default Maven PMD Plugin Ruleset" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">&nbsp; &nbsp; <description>&nbsp; &nbsp; &nbsp; &nbsp; Excluding rules.&nbsp; &nbsp; </description>&nbsp; &nbsp; <rule ref="category/java/bestpractices.xml">&nbsp; &nbsp; &nbsp; &nbsp; <exclude name="UnusedPrivateMethod"/>&nbsp; &nbsp; </rule></ruleset>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java