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
}
alwaysDemandTrailingComma - Control whether to always check for a trailing
comma, even when an array is inline.
Type is boolean.
Default value is false.
To configure the check:
<module name="ArrayTrailingComma"/>
Which results in the following violations:
int[] numbers = {1, 2, 3}; //no violation
boolean[] bools = {
true,
true,
false
}; //violation
String[][] text = {{},{},}; //no violation
double[][] decimals = {
{0.5, 2.3, 1.1,}, //no violation
{1.7, 1.9, 0.6},
{0.8, 7.4, 6.5}
}; // violation as previous line misses a comma
char[] chars = {'a', 'b', 'c'
}; / /no violation
String[] letters = {
"a", "b", "c"}; // no violation
int[] a1 = new int[]{
1,
2
,
}; // no violation
int[] a2 = new int[]{
1,
2
,}; // no violation
To configure check to always validate trailing comma:
<module name="ArrayTrailingComma"> <property name="alwaysDemandTrailingComma" value="true"/> </module>
Example:
int[] numbers = {1, 2, 3}; // violation
boolean[] bools = {
true,
true,
false // violation
};
String[][] text = {{},{},}; // OK
double[][] decimals = {
{0.5, 2.3, 1.1,}, // OK
{1.7, 1.9, 0.6}, // violation
{0.8, 7.4, 6.5} // violation
}; // violation, previous line misses a comma
char[] chars = {'a', 'b', 'c' // violation
};
String[] letters = {
"a", "b", "c"}; // violation
int[] a1 = new int[]{
1,
2
,
}; // OK
int[] a2 = new int[]{
1,
2
,}; // OK
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
array.trailing.comma
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
ArrayTrailingCommaCheck() |
| Modifier and Type | Method and 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.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final String MSG_KEY
public ArrayTrailingCommaCheck()
public void setAlwaysDemandTrailingComma(boolean alwaysDemandTrailingComma)
alwaysDemandTrailingComma - whether to always check for a trailing comma.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST arrayInit)
AbstractCheckvisitToken in class AbstractCheckarrayInit - the token to processCopyright © 2001–2020. All rights reserved.