GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] UseDiamondOperator: False positive with interdependent generic vars · Issue #5441 · pmd/pmd

[java] UseDiamondOperator: False positive with interdependent generic vars #5441

New issue
Copy link
New issue
Copy link
Closed
#6934
Closed
[java] UseDiamondOperator: False positive with interdependent generic vars#5441
#6934
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.27.0

Description

@slovdahl
slovdahl
opened on Jan 2, 2025
Issue body actions

Affects PMD Version: 7.10.0 and 7.9.0 at least (but not 6.55.0)

Rule: UseDiamondOperator

Please provide the rule name and a link to the rule documentation: https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#usediamondoperator

Description:

In a slightly more complex chain of calls, PMD reports a UseDiamondOperator that at least Java 21 is unable to infer the generic types for if the suggestion from PMD is followed.

Code Sample demonstrating the issue:

package test;

import java.util.List;

class Test {
    interface CacheLoader<K, V> {
    }

    interface CacheBuilder<K, V> {
        <K1 extends K, V1 extends V> AsyncCache<K1, V1> buildAsync(CacheLoader<? super K1, V1> loader);
    }

    interface Cache<K, V> {
    }

    interface AsyncCache<K, V> {
        Cache<K, V> synchronous();
    }

    abstract static class ConcreteCacheLoader<K, V> implements CacheLoader<K, V> {
        abstract V fetch(K key);
    }

    interface Provider {
        <K, V> CacheBuilder<K, V> newCacheBuilder(String id);
    }

    void test(Provider provider) {
        Cache<String, List<Integer>> cache = provider.<String, List<Integer>>newCacheBuilder("test")
                .buildAsync(new ConcreteCacheLoader<String, List<Integer>>() {
                    @Override
                    List<Integer> fetch(String key) {
                        return List.of(123);
                    }
                })
                .synchronous();
    }
}

Expected outcome:

PMD reports a violation at line 30, but that's wrong. That's a false positive.

Test.java:30:  UseDiamondOperator:     Explicit type arguments can be replaced by a diamond: `new ConcreteCacheLoader<>()`

Running PMD through: Gradle 8.10

Activity

  1. slovdahl
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Jan 2, 2025
  2. oowekyala commented on Feb 18, 2025

    @oowekyala
    oowekyala
    on Feb 18, 2025
    Member
    More actions

    Thanks for the report.

    It seems with a diamond, javac infers the ConcreteCacheLoader<> to CCL<Object, List<Integer>>, while we infer it to CCL<String, List<Integer>>. After investigating I think the issue lies pretty deep within our inference logic. More investigation needed...

    Reacted by Sebastian Lövdahl
  3. oowekyala
    added a commit that references this issue on Feb 18, 2025

    WIP - order of resolved variables is wrong

    b2bb215
  4. slovdahl
    added a commit that references this issue on Aug 6, 2026

    [java] Exhaust lower bounds before upper bounds within an ivar batch

    7824f68
  5. adangel
    changed the title [-][java] UseDiamondOperator reported but Java 21 is unable to infer the type[/-] [+][java] UseDiamondOperator: False positive for anonymous class creation[/+] on Aug 14, 2026
  6. adangel
    changed the title [-][java] UseDiamondOperator: False positive for anonymous class creation[/-] [+][java] UseDiamondOperator: False positive for anonymous class with interdependent generic vars[/+] on Aug 14, 2026
  7. adangel
    changed the title [-][java] UseDiamondOperator: False positive for anonymous class with interdependent generic vars[/-] [+][java] UseDiamondOperator: False positive with interdependent generic vars[/+] on Aug 14, 2026
  8. adangel
    added this to the 7.27.0 milestone on Aug 14, 2026
  9. adangel
    closed this as completedin #6934on Aug 14, 2026
  10. UncleOwen
    added a commit that references this issue on Aug 14, 2026

    [java] Fix pmd#5441: Resolve interdependent inference variables simul…

    This issue will close once commit a9b6520 is merged into the 'main' branch.
    a9b6520
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.27.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.