Skip to content

[java] Fix #6239: UseDiamondOperator: Implement heuristic for Super Type Token Pattern#6745

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
UncleOwen:issue-6239-UseDiamondOperator-false-positive-with-TypeLiterals
Jun 4, 2026
Merged

[java] Fix #6239: UseDiamondOperator: Implement heuristic for Super Type Token Pattern#6745
UncleOwen merged 4 commits into
pmd:mainfrom
UncleOwen:issue-6239-UseDiamondOperator-false-positive-with-TypeLiterals

Conversation

@UncleOwen

Copy link
Copy Markdown
Member

Describe the PR

The way I see it, there are two ways to tackle #6239:

The first: create a list of classes which are exempt from the UseDiamondOperator rule. The issue already mentions four of them. This list could be either hard-coded or user-configurable. In either case, someone has to edit the list when another framework creates their one version of the TypeToken. I don't like this.

The second: attempt to find uses of the super type token pattern programmatically. This is what this PR does.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-actions-helper

pmd-actions-helper Bot commented Jun 1, 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 45 violations, 15 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-06-03 20:20:47+00:00 for ee25c14)

@UncleOwen

Copy link
Copy Markdown
Member Author

And the regression tester kind of proves my point: org.springframework.core.ParameterizedTypeReference would be a fifth example.

@UncleOwen UncleOwen force-pushed the issue-6239-UseDiamondOperator-false-positive-with-TypeLiterals branch from 156a6ab to d988a95 Compare June 1, 2026 18:47

@lukasgraef lukasgraef left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tackling this problem using a heuristic is a great idea!
The implementation also looks clean to me.

One small finding/discussion point from my side: With the current implementation of the heuristic, it seems that two new FNs are introduced in the regression tester report.
Both of which should have flagged the instantiation of reactor.core.publisher.BaseSubscriber:

Maybe we can tighten the heuristic a bit:

  • Check whether the class actually has a field of type java.lang.reflect.Type
  • Check that the class doesn't implement any interface or extend another class (not really sure about this one, probably the first check is enough)

WDYT?

@UncleOwen UncleOwen force-pushed the issue-6239-UseDiamondOperator-false-positive-with-TypeLiterals branch 2 times, most recently from bf27975 to 916a567 Compare June 3, 2026 16:32
@UncleOwen

Copy link
Copy Markdown
Member Author

Nice spot, and nice suggestion! I did your #1 - and ChannelSendOperatorTests is gone from the regression report :)

@UncleOwen UncleOwen force-pushed the issue-6239-UseDiamondOperator-false-positive-with-TypeLiterals branch from 916a567 to ee25c14 Compare June 3, 2026 20:04
@UncleOwen UncleOwen added this to the 7.26.0 milestone Jun 4, 2026
@UncleOwen UncleOwen merged commit 395080d into pmd:main Jun 4, 2026
13 checks passed
@UncleOwen UncleOwen deleted the issue-6239-UseDiamondOperator-false-positive-with-TypeLiterals branch June 4, 2026 10:38
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] UseDiamondOperator: False positive with Guice TypeLiteral

2 participants