6

PMD Failure: ... Rule:UnusedPrivateMethod Priority:3 Avoid unused private methods such as 'printMyString(String)'

private void anyMethod() {
    var myString = "a String";
    printMyString(myString);
}

private void printMyString(String string) {
    System.out.println(string);
}

Using this plugin to maven

            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>3.12.0</version>
fedup
  • 1,209
  • 11
  • 26
  • I can't reproduce this. Is your code somewhere available? Could you create a small sample project that reproduces this issue? – adangel Dec 06 '19 at 09:34
  • I've prepared a simple test project: https://github.com/adangel/scratchpad/tree/master/pmd-issue-2143 - which does not fail for the method processDocument. The issue was shortly tracked at PMD at https://github.com/pmd/pmd/issues/2143 but since we can't reproduce it, it's already closed. – adangel Dec 06 '19 at 09:46
  • I updated the question to have a more concise example of the issue – fedup Dec 11 '19 at 19:59
  • Thanks for the updated example. Unfortunately, I still can't reproduce it: https://github.com/adangel/scratchpad/blob/master/pmd-issue-2143/src/main/java/com/example/Example.java - of course, I needed to add a method to use "anyMethod", otherwise this would be flagged as unused. But printMyString is correctly detected as used.... – adangel Dec 13 '19 at 19:51

1 Answers1

2

It appears to be a bug in PMD in that it has an issue following the variable type through the inferred "var". The target method has the parameters specifically defined.

I can get around this with disabling the specific PMD rule. In the pom.xml I modify the PMD plugin to use a local rule file.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>3.12.0</version>
            <configuration>
                <linkXRef>false</linkXRef>
                <printFailingErrors>true</printFailingErrors>
                <failOnViolation>true</failOnViolation>
                <rulesets>
                    <ruleset>${basedir}/PMD.xml</ruleset>
                </rulesets>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>check</goal>
                        <goal>cpd-check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

And the PMD.xml file (in the root of the project).

<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">
    <description>
        Excluding rules.
    </description>
    <rule ref="category/java/bestpractices.xml">
        <exclude name="UnusedPrivateMethod"/>
    </rule>
</ruleset>
fedup
  • 1,209
  • 11
  • 26
  • Please note, that with the adjusted ruleset PMD.xml, you'll execute far less rules than before. By default, the maven-pmd-plugin uses this ruleset: /rulesets/java/maven-pmd-plugin-default.xml - It is available here: http://maven.apache.org/plugins/maven-pmd-plugin/examples/usingRuleSets.html (Section "The default ruleset"). Start by copying this locally and comment out just the rule, you don't want. Instead of disabling a rule completely, you can also suppress: See https://pmd.github.io/latest/pmd_userdocs_suppressing_warnings.html – adangel Dec 06 '19 at 09:36
  • I'm experiencing the same error but without using var – andrestascon Aug 08 '23 at 09:51