Skip to content

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

Closed

Description

@davidboden

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. added
    a:false-positive
    on Nov 18, 2025
  2. mrclmh commented on Nov 18, 2025

    @mrclmh
    Contributor

    Other TypeLiterals that may be affected:

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

    @oowekyala
    Member

    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
    Contributor

    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
    Contributor

    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
    Contributor

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

  7. 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
    Member

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

  9. mrclmh commented on May 28, 2026

    @mrclmh
    Contributor

    I can't check but I guess not.

  10. added this to the 7.26.0 milestone on Jun 4, 2026
  11. added a commit that references this issue on Jun 4, 2026
    395080d
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-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions