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 Guice TypeLiteral · Issue #6239 · pmd/pmd

[java] UseDiamondOperator: False positive with Guice TypeLiteral #6239

New issue
Copy link
New issue
Copy link
Closed
#6745
Closed
[java] UseDiamondOperator: False positive with Guice TypeLiteral#6239
#6745
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.26.0

Description

@davidboden
davidboden
opened on Nov 18, 2025
Issue body actions

Affects PMD Version: 7.18.0

Rule: UseDiamondOperator

Description:

Guice's TypeLiteral exists to capture type information. It forces you to create a subclass and thereby captures the generic type information for use at runtime. Removal of the type information by using the diamond operator changes the behaviour of the code, so PMD should not generate a warning and a suggestion to remove the type information and use the diamond operator.

Code Sample demonstrating the issue:

import com.google.inject.TypeLiteral;

  protected Map<Key<?>, Object> computeBindings() {
    return Map.of(
        Key.get(new TypeLiteral<Optional<Long>>() {}, BusinessId.class),
        Optional.of(businessIdProvider.get()));
  }

Expected outcome:

PMD should allow the TypeLiteral to have the type information without generating a warning.

Running PMD through: Other: PMD plugin in IntelliJ

Activity

  1. davidboden
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Nov 18, 2025
  2. mrclmh commented on Nov 18, 2025

    @mrclmh
    mrclmh
    on Nov 18, 2025
    Contributor
    More actions

    Other TypeLiterals that may be affected:

    • javax.enterprise.util.TypeLiteral
    • org.apache.commons.lang3.reflect.TypeLiteral
  3. oowekyala commented on Nov 20, 2025

    @oowekyala
    oowekyala
    on Nov 20, 2025
    Member
    More actions

    Should we just whitelist the name TypeLiteral? Or should we add a property of the rule with a list of types to ignore?

  4. mrclmh commented on Nov 21, 2025

    @mrclmh
    mrclmh
    on Nov 21, 2025
    Contributor
    More actions

    I overlooked "com.fasterxml.jackson.core.type.TypeReference".
    Another possible hint for detection is the anonymous, empty subclass that you have to create in all variants I found.

  5. mrclmh commented on Dec 5, 2025

    @mrclmh
    mrclmh
    on Dec 5, 2025
    Contributor
    More actions

    Neither the example code above nor this example trigger the rule for me with 7.18 or 7.19:

    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.util.List;
    
    public class JsonDeserializer {
        private static final ObjectMapper MAPPER = new ObjectMapper();
    
        public List<String> deserializeToList(String json) throws JsonProcessingException {
            return MAPPER.readValue(json, new TypeReference<List<String>>() {});
        }
    }
  6. mrclmh commented on Dec 5, 2025

    @mrclmh
    mrclmh
    on Dec 5, 2025
    Contributor
    More actions

    Correction:
    I can now reproduce my example, but only with the PMD plugin in IntelliJ.
    Not via CLI or in a unit test.

  7. adangel
    changed the title [-][java] UseDiamondOperator codestyle rule highlights Guice TypeLiteral[/-] [+][java] UseDiamondOperator: False positive with Guice TypeLiteral[/+] on Jan 16, 2026
  8. UncleOwen commented on May 27, 2026

    @UncleOwen
    UncleOwen
    on May 27, 2026
    Member
    More actions

    @mrclmh Did you provide a complete auxclasspath when trying the CLI?

  9. mrclmh commented on May 28, 2026

    @mrclmh
    mrclmh
    on May 28, 2026
    Contributor
    More actions

    I can't check but I guess not.

  10. UncleOwen
    added 2 commits that reference this issue on Jun 1, 2026

    [doc] Update release notes (pmd#6239)

    d988a95

    [doc] Update release notes (pmd#6239)

    2b6559f
  11. UncleOwen
    added this to the 7.26.0 milestone on Jun 4, 2026
  12. UncleOwen
    added a commit that references this issue on Jun 4, 2026

    [java] Fix #6239: UseDiamondOperator: Implement heuristic for Super T…

    This issue will close once commit 395080d is merged into the 'main' branch.
    395080d
  13. UncleOwen
    closed this as completedin #6745on Jun 4, 2026
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.26.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.