Package com.qulice.checkstyle
Class DiamondOperatorCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.qulice.checkstyle.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.AbstractCheckChecks 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>(); // errorwill return compilation error (becauseArrayList<Integer>is not a subclass ofList<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
-
-
Constructor Summary
Constructors Constructor Description DiamondOperatorCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]getAcceptableTokens()int[]getDefaultTokens()int[]getRequiredTokens()voidvisitToken(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
-
-
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST node)
- Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-