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.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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]int[]int[]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, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Constructor Details
-
DiamondOperatorCheck
public DiamondOperatorCheck()
-
-
Method Details
-
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
-