Class DiamondOperatorCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public final class DiamondOperatorCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    Checks if possible to use Diamond operator in generic instances creation.

    Check is performed for variable declarations. Since parameterized types are invariant in generics, Diamond operator should always be used in variable declarations.

    For example,

         private List<Number> numbers = new ArrayList<Integer>(); // error
     
    will return compilation error (because ArrayList<Integer> is not a subclass of List<Number>).

    Hence, the only possible way to create a generic instance is copying type arguments from the variable declaration.

         private List<Number> numbers = new ArrayList<Number>();
     
    In that case, Diamond Operator should always be used.
         private List<Number> numbers = new ArrayList<>();
     

    Exceptions to the rule above are wildcards, with them it's possible to have different type parameters for left and right parts of variable declaration.

         // will compile
         private List<? extends Number> numbers = new ArrayList<Integer>();
         private List<? super Integer> list = new ArrayList<Number>();
    
    Although, this is not considered as good codestyle, so it's better to use diamond operator here either.

    Since:
    0.17
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

        com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST node)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • DiamondOperatorCheck

        public DiamondOperatorCheck()
    • Method Detail

      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST node)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck