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;
}
NOTE: This check processes LITERAL_CASE and LITERAL_DEFAULT separately.
Verification empty block is done for single most nearest case or default.
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 java.lang.String[].
Validation type is tokenSet.
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"/>
Example:
public class Test {
private void emptyLoop() {
for (int i = 0; i < 10; i++) { // violation
}
try { // violation
} catch (Exception e) {
// ignored
}
}
}
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>
Example:
public class Test {
private void emptyLoop() {
for (int i = 0; i < 10; i++) {
// ignored
}
// violation on next line
try {
} catch (Exception e) {
// ignored
}
}
}
To configure the check for default in switch block:
<module name="EmptyBlock"> <property name="tokens" value="LITERAL_DEFAULT"/> </module>
Example:
public class Test {
private void test(int a) {
switch (a) {
case 1: someMethod();
default: // OK, as there is no block
}
switch (a) {
case 1: someMethod();
default: {} // violation
}
}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
block.empty
block.noStatement
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY_BLOCK_EMPTY
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_KEY_BLOCK_NO_STATEMENT
A key is pointing to the warning message text in "messages.properties"
file.
|
private BlockOption |
option
Specify the policy on block contents.
|
| Constructor and Description |
|---|
EmptyBlockCheck() |
| Modifier and Type | Method and Description |
|---|---|
private static boolean |
checkIsAllLinesAreWhitespace(java.lang.String[] lines,
int lineFrom,
int lineTo)
Checks is all lines in array contain whitespaces only.
|
private static DetailAST |
findLeftCurly(DetailAST ast)
Calculates the left curly corresponding to the block to be checked.
|
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 boolean |
hasText(DetailAST slistAST)
Checks if SLIST token contains any text.
|
void |
setOption(java.lang.String optionStr)
Setter to specify the policy on block contents.
|
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_BLOCK_NO_STATEMENT
public static final java.lang.String MSG_KEY_BLOCK_EMPTY
private BlockOption option
public EmptyBlockCheck()
public void setOption(java.lang.String optionStr)
optionStr - string to decode option fromjava.lang.IllegalArgumentException - 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 processprivate boolean hasText(DetailAST slistAST)
slistAST - a DetailAST valueprivate static boolean checkIsAllLinesAreWhitespace(java.lang.String[] lines, int lineFrom, int lineTo)
lines - array of lineslineFrom - check from this line numberlineTo - check to this line numbersprivate static DetailAST findLeftCurly(DetailAST ast)
ast - a DetailAST valueCopyright © 2001-2022. All Rights Reserved.