Skip to content

[java] Fix #7213: UnusedPrivateMethod/UnusedPrivateField with qualified MethodSource/FieldSource references - #7219

Merged
adangel merged 2 commits into
pmd:mainfrom
leeshengis:fix/7213-qualified-methodsource
Oct 29, 2026
Merged

adangel merged 2 commits into
pmd:mainfrom
leeshengis:fix/7213-qualified-methodsource

Conversation

@leeshengis

Copy link
Copy Markdown
Contributor

Describe the PR

Recognize zero-argument factory methods referenced by JUnit @MethodSource using the owning class's binary name, such as demo.Test#factory or demo.Test#factory().

  • Preserve simple and default method-name references and the existing zero-argument scope.
  • Match the exact owner, including $ for nested classes, so an external class's reference does not suppress an unused local method.
  • Leave the shared annotation helper unchanged to avoid affecting UnusedPrivateField.
  • Add 10 regression cases and a release-note entry.

Related issues

Testing

Using JDK 21 and the project's Maven wrapper:

  • Before the fix, eight of the new regression cases fail; the two new negative controls pass.
  • ./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 positive factory-reference cases were also exercised with JUnit 5.11.4: 7 parameterized invocations pass.

The all-language repository build was not run locally; the full-build checkbox below is intentionally left unchecked.

Ready?

  • Added unit tests for fixed bug/feature
  • Passing unit tests for pmd-java and its required reactor dependencies
  • Complete build ./mvnw clean verify passes (checked automatically by GitHub Actions)
  • Added in-code documentation where needed

AI assistance

This change was developed with AI assistance. The patch and test results were reviewed before submission.

Recognize exact owning binary class names and optional empty parameter lists.
Add regression coverage for nested classes, wrong owners, and overloads.

Fixes pmd#7213
@zbynek

zbynek commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

@leeshengis thanks! Would you also like to solve the same issue for FieldSource and UnusedPrivateField rule here?

@pmd-actions-helper

pmd-actions-helper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(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.
@leeshengis

Copy link
Copy Markdown
Contributor Author

@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.

@adangel adangel changed the title [java] Fix UnusedPrivateMethod with qualified MethodSource references [java] Fix #7213: UnusedPrivateMethod with qualified MethodSource references Oct 29, 2026
@adangel adangel changed the title [java] Fix #7213: UnusedPrivateMethod with qualified MethodSource references [java] Fix #7213: UnusedPrivateMethod/UnusedPrivateField with qualified MethodSource/FieldSource references Oct 29, 2026
Comment thread docs/pages/release_notes.md Resolved
void test(String input) { }

private static Stream<String> arguments() { return Stream.of("unused"); }
static Stream<String> arguments(String value) { return Stream.of(value); }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 adangel added this to the 7.29.0 milestone Oct 29, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

(I'll update the release notes tomorrow, when releasing)

@adangel
adangel merged commit c44b87c into pmd:main Oct 29, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UnusedPrivateMethod / UnusedPrivateField: False positives for fully qualified @MethodSource / @FieldSource references

4 participants