Repository navigation
[java] Fix #7340: UnusedPrivateMethod should not treat arbitrary annotation strings as method uses - #7355
Conversation
…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
|
Compared to main: (comment created at 2026-11-19 00:06:35+00:00 for 529c364) |
|
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]>
|
Thanks for the review. I've expanded the allowlist so
Other strings on those same annotations (SQL, descriptions, property names, Java expressions) are still not treated as uses. 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: |
…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.
|
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. |
Describe the PR
UnusedPrivateMethodtreated every string constant in every annotation as a method name. That scan exists for references such as JUnit@MethodSourceand Lombok@ObtainVia(method = "..."), but it also applied to unrelated annotations. In the sample from #7340,@SuppressWarnings("helper")hid an unusedprivate 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 qualifiedOwner#name/name()references)lombok.Builder.ObtainViaUnusedPrivateFieldis unchanged and still treats every annotation string as a possible field use.Related issues
Testing
@SuppressWarnings("helper")and custom-annotation cases failed; the seed, the parameterized control, and existing@MethodSourcecases passed../mvnw -pl pmd-java -am -Dtest=UnusedPrivateMethodTest,UnusedPrivateFieldTest -Dsurefire.failIfNoSpecifiedTests=false test: 200 tests, 0 failures, 2 skipped.pmd-java: 0 violations.Ready?
pmd-java(UnusedPrivateMethodTest,UnusedPrivateFieldTest)./mvnw clean verifypasses (checked automatically by GitHub Actions)This change was made with AI assistance and reviewed.