Skip to content

[java] Fix #7340: UnusedPrivateMethod should not treat arbitrary annotation strings as method uses - #7355

Open
markjoin123 wants to merge 5 commits into
pmd:mainfrom
markjoin123:cursor/unused-private-method-annotation-strings-4063
Open

markjoin123 wants to merge 5 commits into
pmd:mainfrom
markjoin123:cursor/unused-private-method-annotation-strings-4063

Conversation

@markjoin123

Copy link
Copy Markdown
Contributor

Describe the PR

UnusedPrivateMethod treated every string constant in every annotation as a method name. That scan exists for references such as JUnit @MethodSource and Lombok @ObtainVia(method = "..."), but it also applied to unrelated annotations. In the sample from #7340, @SuppressWarnings("helper") hid an unused private void helper(), while the same method was reported when that annotation was absent. A method with parameters was still reported.

String constants are now collected only from annotations that name methods:

  • org.junit.jupiter.params.provider.MethodSource (including a parameterless @MethodSource, and qualified Owner#name / name() references)
  • lombok.Builder.ObtainVia

UnusedPrivateField is unchanged and still treats every annotation string as a possible field use.

Related issues

Testing

  • Before the change, the new @SuppressWarnings("helper") and custom-annotation cases failed; the seed, the parameterized control, and existing @MethodSource cases passed.
  • ./mvnw -pl pmd-java -am -Dtest=UnusedPrivateMethodTest,UnusedPrivateFieldTest -Dsurefire.failIfNoSpecifiedTests=false test: 200 tests, 0 failures, 2 skipped.
  • Checkstyle on pmd-java: 0 violations.

Ready?

  • Added unit tests for fixed bug/feature
  • Passing unit tests for pmd-java (UnusedPrivateMethodTest, UnusedPrivateFieldTest)
  • Complete build ./mvnw clean verify passes (checked automatically by GitHub Actions)
  • Added (in-code) documentation (if needed)

This change was made with AI assistance and reviewed.

…nnotation strings as method uses

UnusedPrivateMethod reused the annotation scan that treats every string
constant as a referenced member. A value such as @SuppressWarnings("helper")
therefore hid an unused no-arg private method of that name.

Collect those strings only from annotations that name methods: JUnit
@MethodSource and Lombok @ObtainVia. UnusedPrivateField still considers
every annotation string.

Fixes pmd#7340
@pmd-actions-helper

pmd-actions-helper Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 1 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 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-11-19 00:06:35+00:00 for 529c364)

@UncleOwen

Copy link
Copy Markdown
Member

There are more frameworks that reference methods by passing their names to annotations: TestNG, Mapstruct, MyBatis. I'm sure there's more.

…s as uses

UnusedPrivateMethod now reads only the attributes that name methods on those
annotations. Other annotation strings, including descriptions, SQL, and
property names, still do not hide unused private methods.

Co-authored-by: markxu <[email protected]>
@markjoin123

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I've expanded the allowlist so UnusedPrivateMethod only reads attributes that actually name methods:

  • TestNG: @Test (dataProvider, dependsOnMethods), @Factory (dataProvider), and the configuration annotations' dependsOnMethods
  • MapStruct: qualifiedByName / conditionQualifiedByName (including the @MapMapping key/value variants), @InheritConfiguration / @InheritInverseConfiguration name, and a method annotated with @Named when one of those qualifier attributes references that name
  • MyBatis: @One / @Many select (simple name or Class.method), and *Provider method

Other strings on those same annotations (SQL, descriptions, property names, Java expressions) are still not treated as uses. @SuppressWarnings and custom annotation strings also stay unused. JUnit @MethodSource and Lombok @ObtainVia are unchanged and still match no-arg methods only.

The rule comment notes that this is the known set and can be extended if more frameworks show up. I added tests for these cases: UnusedPrivateMethodTest 136 run, 0 failures, 2 skipped; UnusedPrivateFieldTest 72 run, 0 failures; checkstyle on pmd-java is clean.

…lected

ReturnEmptyCollectionRatherThanNull flagged attributesFor for returning null.
An empty set now means that annotation contributes no names, while an explicit
empty allowlist entry still reads every string member.
@UncleOwen

Copy link
Copy Markdown
Member

If we fix this, we should fix this for both UnusedPrivateMethod and UnusedPrivateField. But I don't think we should.

This PR introduces unnecessary complexity for what should be a fairly simple rule. I don't like that it adds yet another list of annotations that we need to maintain.

The old code made an assumption: If an annotation-parameter is exactly the name of a method, that method is used somehow. That assumption is reasonable. We shouldn't abandon it, when all we have is one (contrived?) example.

This branch has not been deployed

No deployments
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: any annotation string equal to the method name counts as a use

2 participants