Repository navigation
Description
Affects PMD Version: 7.28.0
Rule: UnusedPrivateMethod, https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#unusedprivatemethod
Description:
seed/WRSuppress.java has an unused private void helper() next to a public helper(int) that is called, and the private one is reported. Adding @SuppressWarnings("helper") to another method (variant/) makes the report go away.
The rule takes every string constant from every annotation in the file (JavaRuleUtil.getMembersUsedByAnnotations) and skips any no-arg private method with that name. I guess it's meant for JUnit's @MethodSource, but it applies to any annotation. Methods with parameters are still reported (control/WRArity.java, @MethodTag("helper") with a private helper(String)).
Could this be limited to the annotations it's meant for, or at least mentioned in the rule docs?
Code Sample demonstrating the issue:
seed/WRSuppress.java:
public class WRSuppress {
public void run() {
helper(1);
}
public void helper(int x) {
System.out.println(x);
}
private void helper() {
System.out.println("h");
}
}variant/WRSuppress.java:
public class WRSuppress {
@SuppressWarnings("helper")
public void run() {
helper(1);
}
public void helper(int x) {
System.out.println(x);
}
private void helper() {
System.out.println("h");
}
}control/WRArity.java:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class WRArity {
@Retention(RetentionPolicy.RUNTIME)
@interface MethodTag {
String value();
}
@MethodTag("helper")
public void run() {
helper(1);
}
public void helper(int x) {
System.out.println(x);
}
private void helper(String s) {
System.out.println(s);
}
}Run:
pmd check -d seed,variant,control \
-R category/java/bestpractices.xml/UnusedPrivateMethod -f text
Expected outcome:
PMD should report UnusedPrivateMethod on helper() in variant/WRSuppress.java, as it does in seed/WRSuppress.java.
Running PMD through: CLI
Output of the commands above:
### PMD
PMD 7.28.0 (79726f0e8f46c1a9954a3f9880f6d15c91bf6387, 2026-09-25T07:52:15Z)
Java version: 25.0.3, vendor: Eclipse Adoptium, runtime: <path> rule: UnusedPrivateMethod
--- SEED (seed) -- expected: reported
seed/WRSuppress.java:10: UnusedPrivateMethod: Avoid unused private methods such as 'helper()'.
[INFO] Found 1 violation.
--- VARIANT (variant) -- expected: reported; actual: not reported
[INFO] Found no violations.
--- CONTROL (control) -- the anonymous-class version, expected: reported
control/WRArity.java:19: UnusedPrivateMethod: Avoid unused private methods such as 'helper(String)'.
[INFO] Found 1 violation.
(PMD exits 4 when it reports violations; empty output above = no violation)
Activity
UncleOwen commentedon Nov 16, 2026
Is this something you've hit on real code?
[java] Fix pmd#7340: Treat TestNG, MapStruct, and MyBatis method name…
[java] Fix pmd#7340: Return an empty set when an annotation is not se…
UncleOwen commentedon Nov 21, 2026
@MarkLee131 Could you please tell us more about how this impacts you? As long as we only have this one contrived example, I'd like to keep the existing heuristic and not change anything at all.