Class ArrayBracketNoWhitespaceCheck
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.whitespace.ArrayBracketNoWhitespaceCheck
- All Implemented Interfaces:
Configurable,Contextualizable
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:
- Since:
- 13.1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringOpen square bracket literal.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.private static final StringClose square bracket literal. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static DetailASTfindClosestTokenAfter(DetailAST node, int line, int column) Recursively scans the AST to find the token closest to and after the given position.private static DetailASTfindNextToken(DetailAST arrayToken, DetailAST rightBracket) Finds the next token after a right bracket by searching in a limited scope.private booleanfollowedByWhitespace(DetailAST token) Checks if a token is followed by whitespace.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private static booleanisBracketOrAccessor(int type) Checks if a token type is a bracket or accessor.private static booleanisDeclarationContainer(int type) Checks if a token type is a declaration/definition container.private static booleanisExpressionContainer(int type) Checks if a token type is an expression container.private static booleanisPostfixOrShift(int type) Checks if a token type is a postfix operator or shift operator.private static booleanisPunctuation(int type) Checks if a token type is punctuation that can follow without whitespace.private static booleanisStructuralToken(int type) Checks if a token type is a structural/container token.private static booleanisTokenAfter(DetailAST token, int line, int column) Checks if a token appears after the given position in the source code.private static booleanisTypeContainer(int type) Checks if a token type is a type-related container.private static booleanisValidTokenToCheck(DetailAST node) Checks if a token is valid to consider as the "next" token.private static booleanisValidWithoutWhitespace(DetailAST nextToken) Checks if the given token can follow a right bracket without whitespace.private booleanprecededByWhitespace(DetailAST token) Checks if a token is preceded by whitespace.voidvisitToken(DetailAST ast) 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, 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
-
Field Details
-
MSG_WS_PRECEDED
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_WS_FOLLOWED
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_WS_NOT_FOLLOWED
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
LEFT_BRACKET
Open square bracket literal.- See Also:
-
RIGHT_BRACKET
Close square bracket literal.- See Also:
-
-
Constructor Details
-
ArrayBracketNoWhitespaceCheck
public ArrayBracketNoWhitespaceCheck()
-
-
Method Details
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
precededByWhitespace
Checks if a token is preceded by whitespace.- Parameters:
token- the token to check- Returns:
- true if preceded by whitespace, false otherwise
-
followedByWhitespace
Checks if a token is followed by whitespace.- Parameters:
token- the token to check- Returns:
- true if followed by whitespace, false otherwise
-
findNextToken
Finds the next token after a right bracket by searching in a limited scope.- Parameters:
arrayToken- the array token (ARRAY_DECLARATOR or INDEX_OP)rightBracket- the right bracket token for position checking- Returns:
- the next token that appears after the bracket, or null if none found
-
findClosestTokenAfter
Recursively scans the AST to find the token closest to and after the given position.- Parameters:
node- the current node to scanline- the line number of the bracketcolumn- the column number of the bracket- Returns:
- the closest token after the position, or null if none found
-
isValidTokenToCheck
Checks if a token is valid to consider as the "next" token. Filters out structural/container tokens that don't represent actual code.- Parameters:
node- the token to check- Returns:
- true if this is a valid token to check
-
isStructuralToken
Checks if a token type is a structural/container token.- Parameters:
type- the token type to check- Returns:
- true if this is a structural token
-
isExpressionContainer
Checks if a token type is an expression container.- Parameters:
type- the token type to check- Returns:
- true if this is an expression container
-
isTypeContainer
Checks if a token type is a type-related container.- Parameters:
type- the token type to check- Returns:
- true if this is a type container
-
isDeclarationContainer
Checks if a token type is a declaration/definition container.- Parameters:
type- the token type to check- Returns:
- true if this is a declaration container
-
isTokenAfter
Checks if a token appears after the given position in the source code.- Parameters:
token- the token to checkline- the line number to compare againstcolumn- the column number to compare against- Returns:
- true if the token is on a later line or same line but later column
-
isValidWithoutWhitespace
Checks if the given token can follow a right bracket without whitespace. Uses TokenTypes to determine valid tokens.- Parameters:
nextToken- the token that follows the right bracket- Returns:
- true if the token can follow without whitespace
-
isBracketOrAccessor
Checks if a token type is a bracket or accessor.- Parameters:
type- the token type to check- Returns:
- true if this is a bracket or accessor
-
isPunctuation
Checks if a token type is punctuation that can follow without whitespace.- Parameters:
type- the token type to check- Returns:
- true if this is valid punctuation
-
isPostfixOrShift
Checks if a token type is a postfix operator or shift operator.- Parameters:
type- the token type to check- Returns:
- true if this is a postfix or shift operator
-