Class WhitespaceAroundCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class WhitespaceAroundCheck extends AbstractCheck
Checks that a token is surrounded by whitespace.By default the check will check the following operators:
ASSERT
,ASSIGN
,BAND
,BAND_ASSIGN
,BOR
,BOR_ASSIGN
,BSR
,BSR_ASSIGN
,BXOR
,BXOR_ASSIGN
,COLON
,DIV
,DIV_ASSIGN
,DO_WHILE
,EQUAL
,GE
,GT
,LAND
,LCURLY
,LE
,LITERAL_CATCH
,LITERAL_DO
,LITERAL_ELSE
,LITERAL_FINALLY
,LITERAL_FOR
,LITERAL_IF
,LITERAL_RETURN
,LITERAL_SWITCH
,LITERAL_SYNCHRONIZED
,LITERAL_TRY
,LITERAL_WHILE
,LOR
,LT
,MINUS
,MINUS_ASSIGN
,MOD
,MOD_ASSIGN
,NOT_EQUAL
,PLUS
,PLUS_ASSIGN
,QUESTION
,RCURLY
,SL
,SLIST
,SL_ASSIGN
,SR
,SR_ASSIGN
,STAR
,STAR_ASSIGN
,LITERAL_ASSERT
,TYPE_EXTENSION_AND
.An example of how to configure the check is:
<module name="WhitespaceAround"/>
An example of how to configure the check for whitespace only around assignment operators is:
<module name="WhitespaceAround"> <property name="tokens" value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN, MOD_ASSIGN,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BXOR_ASSIGN, BOR_ASSIGN,BAND_ASSIGN"/> </module>
An example of how to configure the check for whitespace only around curly braces is:
<module name="WhitespaceAround"> <property name="tokens" value="LCURLY,RCURLY"/> </module>
In addition, this check can be configured to allow empty methods, types, for, while, do-while loops, lambdas and constructor bodies. For example:
public MyClass() {} // empty constructor public void func() {} // empty method public interface Foo {} // empty interface public class Foo {} // empty class public enum Foo {} // empty enum MyClass c = new MyClass() {}; // empty anonymous class while (i = 1) {} // empty while loop for (int i = 1; i > 1; i++) {} // empty for loop do {} while (i = 1); // empty do-while loop Runnable noop = () -> {}; // empty lambda public @interface Beta {} // empty annotation type
This check does not flag as violation double brace initialization like:
new Properties() {{ setProperty("key", "value"); }};
To configure the check to allow empty method blocks use
<property name="allowEmptyMethods" value="true" />
To configure the check to allow empty constructor blocks use
<property name="allowEmptyConstructors" value="true" />
To configure the check to allow empty type blocks use
<property name="allowEmptyTypes" value="true" />
To configure the check to allow empty loop blocks use
<property name="allowEmptyLoops" value="true" />
To configure the check to allow empty lambdas blocks use
<property name="allowEmptyLambdas" value="true" />
Also, this check can be configured to ignore the colon in an enhanced for loop. The colon in an enhanced for loop is ignored by default
To configure the check to ignore the colon
<property name="ignoreEnhancedForColon" value="true" />
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
allowEmptyCatches
Whether or not empty catch blocks are allowed.private boolean
allowEmptyConstructors
Whether or not empty constructor bodies are allowed.private boolean
allowEmptyLambdas
Whether or not empty lambda blocks are allowed.private boolean
allowEmptyLoops
Whether or not empty loops are allowed.private boolean
allowEmptyMethods
Whether or not empty method bodies are allowed.private boolean
allowEmptyTypes
Whether or not empty classes, enums and interfaces are allowed.private boolean
ignoreEnhancedForColon
Whether or not to ignore a colon in a enhanced for loop.static java.lang.String
MSG_WS_NOT_FOLLOWED
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_WS_NOT_PRECEDED
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description WhitespaceAroundCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.private static boolean
isAnonymousInnerClassEnd(int currentType, char nextChar)
Check for "})" or "};" or "},".private static boolean
isArrayInitialization(int currentType, int parentType)
Is array initialization.private static boolean
isColonOfCaseOrDefault(int currentType, int parentType)
Whether colon belongs to cases or defaults.private boolean
isColonOfForEach(int currentType, int parentType)
Whether colon belongs to for-each.private boolean
isEmptyBlock(DetailAST ast, int parentType)
Is empty block.private static boolean
isEmptyBlock(DetailAST ast, int parentType, int match)
Tests if a givenDetailAST
is part of an empty block.private boolean
isEmptyCatch(DetailAST ast, int parentType)
Tests if the givenDetailAst
is part of an allowed empty catch block.private boolean
isEmptyCtorBlock(DetailAST ast, int parentType)
Test if the givenDetailAST
is part of an allowed empty constructor (ctor) block.private boolean
isEmptyLambda(DetailAST ast, int parentType)
Test if the givenDetailAST
is part of an allowed empty lambda block.private boolean
isEmptyLoop(DetailAST ast, int parentType)
Checks if loop is empty.private boolean
isEmptyMethodBlock(DetailAST ast, int parentType)
Test if the givenDetailAST
is part of an allowed empty method block.private static boolean
isEmptyType(DetailAST ast)
Test if the givenDetailAST
is part of an empty block.private boolean
isNotRelevantSituation(DetailAST ast, int currentType)
Is ast not a target of Check.private static boolean
isPartOfDoubleBraceInitializerForNextToken(DetailAST ast)
Check if given ast is part of double brace initializer and if it should omit checking if next token is separated by whitespace.private static boolean
isPartOfDoubleBraceInitializerForPreviousToken(DetailAST ast)
Check if given ast is part of double brace initializer and if it should omit checking if previous token is separated by whitespace.void
setAllowEmptyCatches(boolean allow)
Sets whether or not empty catch blocks are allowed.void
setAllowEmptyConstructors(boolean allow)
Sets whether or not empty constructor bodies are allowed.void
setAllowEmptyLambdas(boolean allow)
Sets whether or not empty lambdas bodies are allowed.void
setAllowEmptyLoops(boolean allow)
Sets whether or not empty loop bodies are allowed.void
setAllowEmptyMethods(boolean allow)
Sets whether or not empty method bodies are allowed.void
setAllowEmptyTypes(boolean allow)
Sets whether or not empty type bodies are allowed.void
setIgnoreEnhancedForColon(boolean ignore)
Sets whether or not to ignore the whitespace around the colon in an enhanced for loop.private static boolean
shouldCheckSeparationFromNextToken(DetailAST ast, char nextChar)
Check if it should be checked if next token is separated from current by whitespace.private static boolean
shouldCheckSeparationFromPreviousToken(DetailAST ast)
Check if it should be checked if previous token is separated from current by whitespace.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_WS_NOT_PRECEDED
public static final java.lang.String MSG_WS_NOT_PRECEDED
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_WS_NOT_FOLLOWED
public static final java.lang.String MSG_WS_NOT_FOLLOWED
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
allowEmptyConstructors
private boolean allowEmptyConstructors
Whether or not empty constructor bodies are allowed.
-
allowEmptyMethods
private boolean allowEmptyMethods
Whether or not empty method bodies are allowed.
-
allowEmptyTypes
private boolean allowEmptyTypes
Whether or not empty classes, enums and interfaces are allowed.
-
allowEmptyLoops
private boolean allowEmptyLoops
Whether or not empty loops are allowed.
-
allowEmptyLambdas
private boolean allowEmptyLambdas
Whether or not empty lambda blocks are allowed.
-
allowEmptyCatches
private boolean allowEmptyCatches
Whether or not empty catch blocks are allowed.
-
ignoreEnhancedForColon
private boolean ignoreEnhancedForColon
Whether or not to ignore a colon in a enhanced for loop.
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
setAllowEmptyMethods
public void setAllowEmptyMethods(boolean allow)
Sets whether or not empty method bodies are allowed.- Parameters:
allow
-true
to allow empty method bodies.
-
setAllowEmptyConstructors
public void setAllowEmptyConstructors(boolean allow)
Sets whether or not empty constructor bodies are allowed.- Parameters:
allow
-true
to allow empty constructor bodies.
-
setIgnoreEnhancedForColon
public void setIgnoreEnhancedForColon(boolean ignore)
Sets whether or not to ignore the whitespace around the colon in an enhanced for loop.- Parameters:
ignore
-true
to ignore enhanced for colon.
-
setAllowEmptyTypes
public void setAllowEmptyTypes(boolean allow)
Sets whether or not empty type bodies are allowed.- Parameters:
allow
-true
to allow empty type bodies.
-
setAllowEmptyLoops
public void setAllowEmptyLoops(boolean allow)
Sets whether or not empty loop bodies are allowed.- Parameters:
allow
-true
to allow empty loops bodies.
-
setAllowEmptyLambdas
public void setAllowEmptyLambdas(boolean allow)
Sets whether or not empty lambdas bodies are allowed.- Parameters:
allow
-true
to allow empty lambda expressions.
-
setAllowEmptyCatches
public void setAllowEmptyCatches(boolean allow)
Sets whether or not empty catch blocks are allowed.- Parameters:
allow
-true
to allow empty catch blocks.
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
isNotRelevantSituation
private boolean isNotRelevantSituation(DetailAST ast, int currentType)
Is ast not a target of Check.- Parameters:
ast
- astcurrentType
- type of ast- Returns:
- true is ok to skip validation
-
shouldCheckSeparationFromPreviousToken
private static boolean shouldCheckSeparationFromPreviousToken(DetailAST ast)
Check if it should be checked if previous token is separated from current by whitespace. This function is needed to recognise double brace initialization as valid, unfortunately its not possible to implement this functionality in isNotRelevantSituation method, because in this method when we return true(is not relevant) ast is later doesn't check at all. For example: new Properties() {{setProperty("double curly braces", "are not a style error"); }}; For second left curly brace in first line when we would return true from isNotRelevantSituation it wouldn't later check that the next token(setProperty) is not separated from previous token.- Parameters:
ast
- current AST.- Returns:
- true if it should be checked if previous token is separated by whitespace, false otherwise.
-
shouldCheckSeparationFromNextToken
private static boolean shouldCheckSeparationFromNextToken(DetailAST ast, char nextChar)
Check if it should be checked if next token is separated from current by whitespace. Explanation why this method is needed is identical to one included in shouldCheckSeparationFromPreviousToken method.- Parameters:
ast
- current AST.nextChar
- next character.- Returns:
- true if it should be checked if next token is separated by whitespace, false otherwise.
-
isAnonymousInnerClassEnd
private static boolean isAnonymousInnerClassEnd(int currentType, char nextChar)
Check for "})" or "};" or "},". Happens with anon-inners- Parameters:
currentType
- tokennextChar
- next symbol- Returns:
- true is that is end of anon inner class
-
isEmptyBlock
private boolean isEmptyBlock(DetailAST ast, int parentType)
Is empty block.- Parameters:
ast
- astparentType
- parent- Returns:
- true is block is empty
-
isEmptyBlock
private static boolean isEmptyBlock(DetailAST ast, int parentType, int match)
Tests if a givenDetailAST
is part of an empty block. An example empty block might look like the followingpublic void myMethod(int val) {}
In the above, the method body is an empty block ("{}").- Parameters:
ast
- theDetailAST
to test.parentType
- the token type ofast
's parent.match
- the parent token type we're looking to match.- Returns:
true
ifast
makes up part of an empty block contained under amatch
token type node.
-
isColonOfCaseOrDefault
private static boolean isColonOfCaseOrDefault(int currentType, int parentType)
Whether colon belongs to cases or defaults.- Parameters:
currentType
- currentparentType
- parent- Returns:
- true if current token in colon of case or default tokens
-
isColonOfForEach
private boolean isColonOfForEach(int currentType, int parentType)
Whether colon belongs to for-each.- Parameters:
currentType
- currentparentType
- parent- Returns:
- true if current token in colon of for-each token
-
isArrayInitialization
private static boolean isArrayInitialization(int currentType, int parentType)
Is array initialization.- Parameters:
currentType
- current tokenparentType
- parent token- Returns:
- true is current token inside array initialization
-
isEmptyMethodBlock
private boolean isEmptyMethodBlock(DetailAST ast, int parentType)
Test if the givenDetailAST
is part of an allowed empty method block.- Parameters:
ast
- theDetailAST
to test.parentType
- the token type ofast
's parent.- Returns:
true
ifast
makes up part of an allowed empty method block.
-
isEmptyCtorBlock
private boolean isEmptyCtorBlock(DetailAST ast, int parentType)
Test if the givenDetailAST
is part of an allowed empty constructor (ctor) block.- Parameters:
ast
- theDetailAST
to test.parentType
- the token type ofast
's parent.- Returns:
true
ifast
makes up part of an allowed empty constructor block.
-
isEmptyLoop
private boolean isEmptyLoop(DetailAST ast, int parentType)
Checks if loop is empty.- Parameters:
ast
- ast theDetailAST
to test.parentType
- the token type ofast
's parent.- Returns:
true
ifast
makes up part of an allowed empty loop block.
-
isEmptyLambda
private boolean isEmptyLambda(DetailAST ast, int parentType)
Test if the givenDetailAST
is part of an allowed empty lambda block.- Parameters:
ast
- theDetailAST
to test.parentType
- the token type ofast
's parent.- Returns:
true
ifast
makes up part of an allowed empty lambda block.
-
isEmptyCatch
private boolean isEmptyCatch(DetailAST ast, int parentType)
Tests if the givenDetailAst
is part of an allowed empty catch block.- Parameters:
ast
- theDetailAst
to test.parentType
- the token type ofast
's parent- Returns:
true
ifast
makes up part of an allowed empty catch block.
-
isEmptyType
private static boolean isEmptyType(DetailAST ast)
Test if the givenDetailAST
is part of an empty block. An example empty block might look like the followingclass Foo {}
- Parameters:
ast
- ast theDetailAST
to test.- Returns:
true
ifast
makes up part of an empty block contained under amatch
token type node.
-
isPartOfDoubleBraceInitializerForPreviousToken
private static boolean isPartOfDoubleBraceInitializerForPreviousToken(DetailAST ast)
Check if given ast is part of double brace initializer and if it should omit checking if previous token is separated by whitespace.- Parameters:
ast
- ast to check- Returns:
- true if it should omit checking for previous token, false otherwise
-
isPartOfDoubleBraceInitializerForNextToken
private static boolean isPartOfDoubleBraceInitializerForNextToken(DetailAST ast)
Check if given ast is part of double brace initializer and if it should omit checking if next token is separated by whitespace. See PR#2845 for more information why this function was needed.- Parameters:
ast
- ast to check- Returns:
- true if it should omit checking for next token, false otherwise
-
-