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.6.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic 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.static final StringA key is pointing to the warning message text in "messages.properties" file.Tokens that are valid after a right bracket without whitespace. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static DetailASTfindBestCandidate(DetailAST candidate, DetailAST rightBracket, DetailAST current) Evaluates whethercurrentis a better next-token candidate than the existingcandidaterelative torightBracket.private static DetailASTfindNextToken(DetailAST rightBracket) Finds the next token after a right bracket by climbing the AST and scanning next-sibling chains at each level.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 booleanChecks whether anARRAY_DECLARATORis immediately preceded by anANNOTATIONSsibling, which happens in constructs likeint @Ann [] x.private static booleanisValidWithoutWhitespace(DetailAST nextToken) Checks if the given token can follow a right bracket without whitespace.private booleanisWhitespaceAt(DetailAST token, int columnNo) Checks if a whitespace character is present at the given column on the same line as the provided token.private voidProcesses a right bracket token and logs violations if it is preceded or followed by whitespace inappropriately.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_NOT_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:
-
VALID_AFTER_RIGHT_BRACKET_TOKENS
Tokens that are valid after a right bracket without whitespace.
-
-
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
-
processRightBracket
Processes a right bracket token and logs violations if it is preceded or followed by whitespace inappropriately.- Parameters:
ast- the right bracket token to process
-
isPrecededByAnnotation
Checks whether anARRAY_DECLARATORis immediately preceded by anANNOTATIONSsibling, which happens in constructs likeint @Ann [] x.- Parameters:
ast- theARRAY_DECLARATORorINDEX_OPtoken- Returns:
- true if the token's previous sibling is an ANNOTATIONS node
-
isWhitespaceAt
Checks if a whitespace character is present at the given column on the same line as the provided token.- Parameters:
token- the token whose line should be checkedcolumnNo- the column number to inspect for whitespace- Returns:
- true if the character at
columnNois a whitespace character
-
findNextToken
Finds the next token after a right bracket by climbing the AST and scanning next-sibling chains at each level. At every level all siblings are visited: siblings on a later line are skipped and set anoutOfLineflag; siblings on the same line are passed tofindBestCandidate(com.puppycrawl.tools.checkstyle.api.DetailAST, com.puppycrawl.tools.checkstyle.api.DetailAST, com.puppycrawl.tools.checkstyle.api.DetailAST). Once a later-line sibling is found at any level the climb stops immediately, since no ancestor sibling can be on the target line either. The candidate with the smallest qualifying column is returned.- Parameters:
rightBracket- the right bracket token whose successor is needed- Returns:
- the closest same-line token that follows the bracket, or
nullif no such token exists on that line
-
findBestCandidate
@Nullable private static DetailAST findBestCandidate(@Nullable DetailAST candidate, DetailAST rightBracket, DetailAST current) Evaluates whethercurrentis a better next-token candidate than the existingcandidaterelative torightBracket. A token qualifies as a better candidate when it sits on the same line as the right bracket, has a greater column number than the bracket, and either no candidate exists yet or its column number is closer to the bracket than the current best. When the criteria are met the new token is returned; otherwise the existing candidate is returned unchanged.- Parameters:
candidate- the current best candidaterightBracket- the right bracket tokencurrent- the current AST node being evaluated- Returns:
- the new best candidate
-
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
-