Class ArrayTrailingCommaCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.coding.ArrayTrailingCommaCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class ArrayTrailingCommaCheck extends AbstractCheck
Checks that array initialization contains a trailing comma.
int[] a = new int[] { 1, 2, 3, };
By default, the check demands a comma at the end if neither left nor right curly braces are on the same line as the last element of the array.
return new int[] { 0 }; return new int[] { 0 }; return new int[] { 0 };
Rationale: Putting this comma in makes it easier to change the order of the elements or add new elements on the end. Main benefit of a trailing comma is that when you add new entry to an array, no surrounding lines are changed.
{ 100000000000000000000, 200000000000000000000, // OK } { 100000000000000000000, 200000000000000000000, 300000000000000000000, // Just this line added, no other changes }
If closing brace is on the same line as trailing comma, this benefit is gone (as the check does not demand a certain location of curly braces the following two cases will not produce a violation):
{100000000000000000000, 200000000000000000000,} // Trailing comma not needed, line needs to be modified anyway {100000000000000000000, 200000000000000000000, // Modified line 300000000000000000000,} // Added line
If opening brace is on the same line as trailing comma there's also (more arguable) problem:
{100000000000000000000, // Line cannot be just duplicated to slightly modify entry } {100000000000000000000, 100000000000000000001, // More work needed to duplicate }
-
Property
alwaysDemandTrailingComma
- Control whether to always check for a trailing comma, even when an array is inline. Type isboolean
. Default value isfalse
.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
array.trailing.comma
- Since:
- 3.2
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
alwaysDemandTrailingComma
Control whether to always check for a trailing comma, even when an array is inline.static String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description ArrayTrailingCommaCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.void
setAlwaysDemandTrailingComma(boolean alwaysDemandTrailingComma)
Setter to control whether to always check for a trailing comma, even when an array is inline.void
visitToken(DetailAST arrayInit)
Called to process a token.-
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
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
alwaysDemandTrailingComma
private boolean alwaysDemandTrailingComma
Control whether to always check for a trailing comma, even when an array is inline.
-
-
Constructor Detail
-
ArrayTrailingCommaCheck
public ArrayTrailingCommaCheck()
-
-
Method Detail
-
setAlwaysDemandTrailingComma
public void setAlwaysDemandTrailingComma(boolean alwaysDemandTrailingComma)
Setter to control whether to always check for a trailing comma, even when an array is inline.- Parameters:
alwaysDemandTrailingComma
- whether to always check for a trailing comma.- Since:
- 8.33
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST arrayInit)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
arrayInit
- the token to process
-
-