ArrayBracketNoWhitespace
Since Checkstyle 13.1.0
Description
Checks that the whitespace around square-bracket tokens
[ and ]
follows the Google Java Style Guide requirements for array declarations, array creation,
and array indexing.
Left square bracket ("["):
- must not be preceded with whitespace when preceded by a
TYPEorIDENTin array declarations or array access - must not be followed with whitespace
Right square bracket ("]"):
- must not be preceded with whitespace
- must be followed with whitespace in all cases, except when followed by:
- another bracket:
[][] - a dot for member access:
arr[i].length - a comma or semicolon:
arr[i],orarr[i]; - postfix operators:
arr[i]++orarr[i]-- - a right parenthesis or another closing construct:
(arr[i])
- another bracket:
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="ArrayBracketNoWhitespace"/>
</module>
</module>
Example:
class Example1 {
int[] numbersWarn = new int[10 ]; // violation, ']' is preceded with whitespace
int[] numbersGood = new int[10];
String [] dataWarn = {"a", "b"}; // violation, '[' is preceded with whitespace
String[] dataGood = {"a", "b"};
byte bufferWarn[ ];
// 2 violations above
// '[' is followed by whitespace
// ']' is preceded with whitespace
byte bufferGood[];
int[] matrixWarn[] ; // violation, ']' is followed by whitespace
int[] matrixGood[];
void processArray(int[] arr) {
int value = arr [0]; // violation, '[' is preceded with whitespace
int valueGood = arr[0];
}
void calculateWarn(int values[ ]) {
// 2 violations above
// '[' is followed by whitespace
// ']' is preceded with whitespace
}
void calculateGood(int values[]) {
}
void processData(int[] values, String[] labels) {
for (int i = 0; i < values.length; i++) {
values[i] = values[i] * 2;
}
}
void initializeData() {
String[] namesWarn = new String[ 5 ];
// 2 violations above
// '[' is followed by whitespace
// ']' is preceded with whitespace
String[] namesGood = new String[5];
int[][] gridWarn = new int[3 ][4]; // violation, ']' is preceded with whitespace
int[][] gridGood = new int[3][4];
int[][] correctGrid = new int[5][5];
correctGrid[0][0] = 1;
}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.whitespace






