Skip to content

[java] UnusedPrivateMethod FP with raw type missing from the classpath #5097

@wac84s

Description

@wac84s

Affects PMD Version:
7.3.0

Rule: UnusedPrivateMethod

Description:
private method referenced by method reference operators not getting detected as used

this::mymethod

Code Sample demonstrating the issue:

package com.mytest;

import jakarta.validation.ConstraintViolation; //imported from jakarta.validation:jakarta.validation-api:3.0.2
import java.util.List;
import java.util.Set;

public class UnusedPrivateMethodFalsePositive {
    //this does not trigger UnusedPrivateMethod
    private void doWork(List obj) {
        obj.toString();
    }

    public void execute(Set<List<?>> listOfLists) {
        listOfLists.forEach(this::doWork);
    }

    //BUT this does???
    //UnusedPrivateMethod -  this as a false positive - but what is different?
    private void addValidationError(ConstraintViolation constraintViolation) {
        constraintViolation.toString();
    }

    public void addValidationErrors(Set<ConstraintViolation<?>> constraintViolations) {
        constraintViolations.forEach(this::addValidationError);
    }

}

Expected outcome:

It is not an unused private method

PMD reports a violation at line ..., but that's wrong. That's a false positive.

UnusedPrivateMethodFalsePositive.java:19: UnusedPrivateMethod: Avoid unused private methods such as 'addValidationError(ConstraintViolation)'.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Maven AND cli both report it

Metadata

Metadata

Assignees

Labels

a:false-positivePMD flags a piece of code that is not problematicin:type-resolutionAffects the type resolution code

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions