Skip to content

[java] UnusedPrivateMethod: False positive with overloaded private methods called with values returned from methods defined in other classes #6393

Closed

Description

@jjlharrison

Affects PMD Version:
7.20.0

Rule:

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd/pmd_rules_java_bestpractices.html#unusedprivatemethod

Description:
I have an overloaded private method called by default methods in an interface. If I call these methods with literals, then the rule does not fail, however, when calling them with the return value from a method I see a false-positive.

Also, if the class with the getLong method is an inner-class, I don't see the violation. So, I assume there's some failure to get the return type of the method that only happens with top-level classes.

Code Sample demonstrating the issue:

public interface UnusedPrivateMethodFalsePositivePrivateMethod {

    default String publicMethod(final Thing l) {
        return transform(l.getLong());
    }

    default String publicMethod2(final Thing l) {
        return transform(l.getString());
    }

    private String transform(final String l) {
        return switch (l) {
            case "1" -> "one";
            case "2" -> "two";
            default -> l;
        };
    }

    private String transform(final Long l) { // Violation here
        return switch (l.intValue()) {
            case 1 -> "one";
            case 2 -> "two";
            default -> l.toString();
        };
    }
}
public class Thing {
    public String getString() {
        return "thing";
    }

    public Long getLong() {
        return 1L;
    }
}

Expected outcome:

PMD reports a violation for transform(Long) but that's a false positive.

Running PMD through: Maven

Maven Log
[INFO] >>> pmd:3.28.0:check (pmd) > :pmd @ uk-auto-confused-quoting >>>
[INFO] 
[INFO] --- pmd:3.28.0:pmd (pmd) @ uk-auto-confused-quoting ---
[INFO] PMD version: 7.20.0
[WARNING] Unable to find a URL to the parent project. The parent menu will NOT be added.
[INFO] Rendering content with org.apache.maven.skins:maven-fluido-skin:jar:2.0.0-M9 skin
[INFO] 
[INFO] <<< pmd:3.28.0:check (pmd) < :pmd @ uk-auto-confused-quoting <<<
[INFO] 
[INFO] 
[INFO] --- pmd:3.28.0:check (pmd) @ uk-auto-confused-quoting ---
[WARNING] PMD Failure: com.marshmallow.uk_auto_confused_quoting.service.common.UnusedPrivateMethodFalsePositivePrivateMethod:21 Rule:UnusedPrivateMethod Priority:3 Avoid unused private methods such as 'transform(Long)'..

Activity

  1. added
    a:false-positive
    on Jan 8, 2026
  2. changed the title [-][java] UnusedPrivateMethod with overloaded private method in interfacr[/-] [+][java] UnusedPrivateMethod with overloaded private method in interface[/+] on Jan 8, 2026
  3. self-assigned this
    on Jan 8, 2026
  4. jjlharrison commented on Jan 8, 2026

    @jjlharrison
    ContributorAuthor

    I've just found another instance of this problem in a class, so it's not specific to interfaces but just any overloaded private method that is called with a value returned from a method defined outside of the current class.

  5. changed the title [-][java] UnusedPrivateMethod with overloaded private method in interface[/-] [+][java] UnusedPrivateMethod false-positive with overloaded private methods called with values returned from methods defined in other classes[/+] on Jan 8, 2026
  6. oowekyala commented on Jan 8, 2026

    @oowekyala
    Member

    The problem is that you are not including compiled class files for the classes that define the called method (eg here, your Thing class) on the auxclasspath. Otherwise PMD would be able to resolve the method call to one or the other overload. See https://pmd.github.io/pmd/pmd_languages_java.html#providing-the-auxiliary-classpath

    That said, UnusedPrivateMethod has checks in place to avoid false positives in that case, and here they are not working. So there is something we can fix here.

    But fixing your configuration is the only thing that will make the rule work accurately and properly. Since you run the rule in Maven, make sure you are running PMD only after the compile phase has run, so that compiled classes are found by PMD.

  7. jjlharrison commented on Jan 8, 2026

    @jjlharrison
    ContributorAuthor

    @oowekyala : Thank you. I can confirm that changing the Maven phase has resolved the issue for me.

  8. changed the title [-][java] UnusedPrivateMethod false-positive with overloaded private methods called with values returned from methods defined in other classes[/-] [+][java] UnusedPrivateMethod: False positive with overloaded private methods called with values returned from methods defined in other classes[/+] on Jan 16, 2026
  9. added this to the 7.27.0 milestone on Aug 14, 2026
  10. added a commit that references this issue on Aug 14, 2026
    6dcf9f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:false-positive

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions