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.
By default the check will check the following operators:
ARRAY_INIT
,
BNOT
,
DEC
,
DOT
,
INC
,
LNOT
,
UNARY_MINUS
,
UNARY_PLUS
,
TYPECAST
,
ARRAY_DECLARATOR
,
INDEX_OP
.
The check processes
ARRAY_DECLARATOR
,
INDEX_OP
specially from other tokens. Actually it is checked that there is
no whitespace before this tokens, not after them.
An example of how to configure the check is:
<module name="NoWhitespaceAfter"/>
An example of how to configure the check to forbid linebreaks after
a DOT
token is:
<module name="NoWhitespaceAfter"> <property name="tokens" value="DOT"/> <property name="allowLineBreaks" value="false"/> </module>
Modifier and Type | Field and Description |
---|---|
private boolean |
allowLineBreaks
Whether whitespace is allowed if the AST 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 |
getFirstNonArrayDeclaratorParent(DetailAST ast)
Get node that owns
ARRAY_DECLARATOR sequence. |
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 |
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)
Control whether whitespace is flagged at linebreaks.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_KEY
private boolean allowLineBreaks
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void setAllowLineBreaks(boolean allowLineBreaks)
allowLineBreaks
- whether whitespace should be
flagged at linebreaks.public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate static DetailAST getWhitespaceFollowedNode(DetailAST ast)
ast
- , visited node.private 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.private 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 getFirstNonArrayDeclaratorParent(DetailAST ast)
ARRAY_DECLARATOR
sequence.ast
- , ARRAY_DECLARATOR
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.