Skip to content

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

@davidboden

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

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions