public class EmptyBlockCheck extends AbstractCheck
Checks for empty blocks. This check does not validate sequential blocks.
Sequential blocks won't be checked. Also, no violations for fallthrough:
switch (a) {
case 1: // no violation
case 2: // no violation
case 3: someMethod(); { } // no violation
default: break;
}
This check processes LITERAL_CASE and LITERAL_DEFAULT separately. So, if tokens=LITERAL_DEFAULT, following code will not trigger any violation, as the empty block belongs to LITERAL_CASE:
Configuration:
<module name="EmptyBlock"> <property name="tokens" value="LITERAL_DEFAULT"/> </module>
Result:
switch (a) {
default: // no violation for "default:" as empty block belong to "case 1:"
case 1: { }
}
option - specify the policy on block contents.
Type is com.puppycrawl.tools.checkstyle.checks.blocks.BlockOption.
Default value is statement.
tokens - tokens to check
Type is int[].
Default value is:
LITERAL_WHILE,
LITERAL_TRY,
LITERAL_FINALLY,
LITERAL_DO,
LITERAL_IF,
LITERAL_ELSE,
LITERAL_FOR,
INSTANCE_INIT,
STATIC_INIT,
LITERAL_SWITCH,
LITERAL_SYNCHRONIZED.
To configure the check:
<module name="EmptyBlock"/>
To configure the check for the text policy and only try blocks:
<module name="EmptyBlock"> <property name="option" value="text"/> <property name="tokens" value="LITERAL_TRY"/> </module>
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
block.empty
block.noStatement
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY_BLOCK_EMPTY
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_BLOCK_NO_STATEMENT
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
EmptyBlockCheck() |
| Modifier and Type | Method and 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.
|
void |
setOption(String optionStr)
Setter to specify the policy on block contents.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final String MSG_KEY_BLOCK_NO_STATEMENT
public static final String MSG_KEY_BLOCK_EMPTY
public EmptyBlockCheck()
public void setOption(String optionStr)
optionStr - string to decode option fromIllegalArgumentException - if unable to decodepublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processCopyright © 2001–2020. All rights reserved.