A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/5486 below:

[java] UnusedPrivateMethod detected when class is referenced in another class · Issue #5486 · pmd/pmd · GitHub

Affects PMD Version: 7.9.0

Rule: UnusedPrivateMethod

Rule documentation:
https://docs.pmd-code.org/snapshot/pmd_rules_java_bestpractices.html#unusedprivatemethod

Description:
False positive when a private method is invoked directly over the return value of a method on other class.
The workaround is declaring a variable with the return value, and call the private method over that variable.

Code Sample demonstrating the issue:

package foo;

public class Class1 {

    public void publicMethod() {
        new Class2().getClass1().privateMethod();
    }

    private void privateMethod() { // This method is detected as UnusedPrivateMethod (false positive)
        // do stuff
    }

    public void publicMethod2() {
        // Declaring the variable, makes the PMD works fine (workaround)
        Class1 class1 = new Class2().getClass1();
        class1.privateMethod2();
    }

    private void privateMethod2() {
        // do stuff
    }
}
package foo;

public class Class2 {
    public Class1 getClass1() {
        return new Class1();
    }
}

Expected outcome:
No errors

PMD log:
[INFO] PMD version: 7.9.0
[INFO] PMD Failure: foo.Class1:9 Rule:UnusedPrivateMethod Priority:3 Avoid unused private methods such as 'privateMethod()'..

Running PMD through: Maven


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4