public class NoWhitespaceAfterCheck extends AbstractCheck
Checks that there is no whitespace after a token.
More specifically, it checks that it is not followed by whitespace,
or (if linebreaks are allowed) all characters on the line after are
whitespace. To forbid linebreaks after a token, set property
allowLineBreaks to false.
The check processes ARRAY_DECLARATOR and INDEX_OP tokens specially from other tokens. Actually it is checked that there is no whitespace before this tokens, not after them. Space after the ANNOTATIONS before ARRAY_DECLARATOR and INDEX_OP will be ignored.
allowLineBreaks - Control whether whitespace is allowed
if the token is at a linebreak.
Type is boolean.
Default value is true.
tokens - tokens to check
Type is java.lang.String[].
Validation type is tokenSet.
Default value is:
ARRAY_INIT,
AT,
INC,
DEC,
UNARY_MINUS,
UNARY_PLUS,
BNOT,
LNOT,
DOT,
ARRAY_DECLARATOR,
INDEX_OP.
To configure the check:
<module name="NoWhitespaceAfter"/>
To configure the check to forbid linebreaks after a DOT token:
<module name="NoWhitespaceAfter"> <property name="tokens" value="DOT"/> <property name="allowLineBreaks" value="false"/> </module>
If the annotation is between the type and the array, the check will skip validation for spaces:
public void foo(final char @NotNull [] param) {} // No violation
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
ws.followed
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private boolean |
allowLineBreaks
Control whether whitespace is allowed if the token is at a linebreak.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
NoWhitespaceAfterCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens()
The configurable token set.
|
private static DetailAST |
getArrayDeclaratorPreviousElement(DetailAST ast)
Returns proper argument for getPositionAfter method, it is a token after
ARRAY_DECLARATOR, in can be RBRACK, IDENT or an array type definition (literal). |
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static DetailAST |
getIdentLastToken(DetailAST ast)
Gets leftmost token of identifier.
|
private static DetailAST |
getIndexOpPreviousElement(DetailAST ast)
Gets previous node for
INDEX_OP token
for usage in getPositionAfter method, it is a simplified copy of
getArrayDeclaratorPreviousElement method. |
private static int |
getPositionAfter(DetailAST ast)
Gets position after token (place of possible redundant whitespace).
|
private static DetailAST |
getPrecedingDot(DetailAST leftBracket)
Gets the dot preceding a class member array index operation or class
reference.
|
private static DetailAST |
getPreviousElementOfMultiDimArray(DetailAST leftBracket)
Gets the previous element of a second or higher dimension of an
array declaration or initialization.
|
private static DetailAST |
getPreviousNodeWithParentOfTypeAst(DetailAST ast,
DetailAST parent)
Finds previous node by text order for an array declarator,
which parent type is
TYPE. |
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static DetailAST |
getTypeLastNode(DetailAST ast)
Searches parameter node for a type node.
|
private static DetailAST |
getWhitespaceFollowedNode(DetailAST ast)
For a visited ast node returns node that should be checked
for not being followed by whitespace.
|
private boolean |
hasTrailingWhitespace(DetailAST ast,
int whitespaceColumnNo,
int whitespaceLineNo)
Checks if there is unwanted whitespace after the visited node.
|
void |
setAllowLineBreaks(boolean allowLineBreaks)
Setter to control whether whitespace is allowed if the token is at a linebreak.
|
private static boolean |
shouldCheckWhitespaceAfter(DetailAST ast)
Returns whether whitespace after a visited node should be checked.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private boolean allowLineBreaks
public NoWhitespaceAfterCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void setAllowLineBreaks(boolean allowLineBreaks)
allowLineBreaks - whether whitespace should be
flagged at linebreaks.public void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate static DetailAST getWhitespaceFollowedNode(DetailAST ast)
ast - , visited node.private static boolean shouldCheckWhitespaceAfter(DetailAST ast)
ast - the visited nodeprivate static int getPositionAfter(DetailAST ast)
ast - Node representing token.private boolean hasTrailingWhitespace(DetailAST ast, int whitespaceColumnNo, int whitespaceLineNo)
ast - , visited node.whitespaceColumnNo - , column number of a possible whitespace.whitespaceLineNo - , line number of a possible whitespace.private static DetailAST getArrayDeclaratorPreviousElement(DetailAST ast)
ARRAY_DECLARATOR, in can be RBRACK, IDENT or an array type definition (literal).ast - , ARRAY_DECLARATOR node.java.lang.IllegalStateException - if an unexpected token type is encountered.private static DetailAST getPreviousElementOfMultiDimArray(DetailAST leftBracket)
leftBracket - the token to get previous element ofprivate static DetailAST getIndexOpPreviousElement(DetailAST ast)
INDEX_OP token
for usage in getPositionAfter method, it is a simplified copy of
getArrayDeclaratorPreviousElement method.ast - , INDEX_OP node.private static DetailAST getTypeLastNode(DetailAST ast)
ast - , subject node.private static DetailAST getPreviousNodeWithParentOfTypeAst(DetailAST ast, DetailAST parent)
TYPE.ast - , array declarator node.parent - , its parent node.private static DetailAST getIdentLastToken(DetailAST ast)
ast - , token possibly possessing an identifier.private static DetailAST getPrecedingDot(DetailAST leftBracket)
leftBracket - the ast we are checkingCopyright © 2001-2022. All Rights Reserved.