Repository navigation
[java] Fix #7213: UnusedPrivateMethod/UnusedPrivateField with qualified MethodSource/FieldSource references - #7219
Conversation
Recognize exact owning binary class names and optional empty parameter lists. Add regression coverage for nested classes, wrong owners, and overloads. Fixes pmd#7213
|
@leeshengis thanks! Would you also like to solve the same issue for FieldSource and UnusedPrivateField rule here? |
|
Compared to main: (comment created at 2026-10-29 08:47:35+00:00 for 5a2e263) |
Preserve both release-note fixes and match field references by exact binary owner, without method-style parentheses. Add eight regression cases and retain existing MethodSource coverage. Verified with JDK 21: 192 focused tests; clean verify across pmd-java and six dependency modules, 17,073 tests with zero failures or errors.
|
@zbynek Thanks for the suggestion. Qualified @FieldSource support is now included in commit 5a2e263, with eight regression cases covering exact binary owners, nested classes, default-package references, and negative controls. Existing MethodSource coverage is preserved, and the merge conflict is resolved. Java and dependency reactor verification completed with 17,073 tests, zero failures/errors, and 29 skips; Checkstyle passed. The new CI run is awaiting workflow approval. |
| void test(String input) { } | ||
|
|
||
| private static Stream<String> arguments() { return Stream.of("unused"); } | ||
| static Stream<String> arguments(String value) { return Stream.of(value); } |
There was a problem hiding this comment.
There seems to be a gap currently with how we implemented support for MethodSource: In case the factory method declares parameters, PMD will currently ignore them. If you would make this line 11 private, PMD would report a false-positive.
This behavior is not new (we ignored methods with arity != 0 already), so we should fix it outside of this PR (if it is needed - looks like a rare edge case to me).
adangel
left a comment
There was a problem hiding this comment.
Thanks!
(I'll update the release notes tomorrow, when releasing)
Describe the PR
Recognize zero-argument factory methods referenced by JUnit
@MethodSourceusing the owning class's binary name, such asdemo.Test#factoryordemo.Test#factory().$for nested classes, so an external class's reference does not suppress an unused local method.UnusedPrivateField.Related issues
Testing
Using JDK 21 and the project's Maven wrapper:
./mvnw -pl pmd-java -am -Dtest=UnusedPrivateMethodTest,UnusedPrivateFieldTest -Dsurefire.failIfNoSpecifiedTests=false test: 184 tests, no failures or errors, 2 skipped../mvnw -pl pmd-java -am clean verify: all 7 reactor modules succeed; 16,854 tests, no failures or errors, 29 skipped. Checkstyle reports no violations. The PMD dogfood warning in the changed rule was reproduced on the baseline.The all-language repository build was not run locally; the full-build checkbox below is intentionally left unchecked.
Ready?
pmd-javaand its required reactor dependencies./mvnw clean verifypasses (checked automatically by GitHub Actions)AI assistance
This change was developed with AI assistance. The patch and test results were reviewed before submission.