Class UseDiamondOperatorRule

  • All Implemented Interfaces:
    AstVisitor<Object,​Object>, JavaParserVisitor, JavaVisitor<Object,​Object>, PropertySource, Rule

    public class UseDiamondOperatorRule
    extends AbstractJavaRulechainRule
    Checks usages of explicity type arguments in a constructor call that may be replaced by a diamond (<>). In order to determine this, we mock a type resolution call site, which is equivalent to the expression as if it had a diamond instead of explicit type arguments. We then perform overload resolution for this fake call site. If overload resolution fails, resolves to another overload, or if the inferred type is not compatible with the expected context type, then the type arguments are unnecessary, and removing them will not break the program.

    Note that type inference in Java 8+ works differently from Java 7. In Java 7, type arguments may be necessary in more places. The specifics are however implemented within the type resolution code, and this rule does not need to know about it.