public class AvoidNestedBlocksCheck extends AbstractCheck
For example this Check flags confusing code like
public void guessTheOutput()
{
int whichIsWhich = 0;
{
int whichIsWhich = 2;
}
System.out.println("value = " + whichIsWhich);
}
and debugging / refactoring leftovers such as
// if (someOldCondition)
{
System.out.println("unconditional");
}
A case in a switch statement does not implicitly form a block. Thus to be able to introduce local variables that have case scope it is necessary to open a nested block. This is supported, set the allowInSwitchCase property to true and include all statements of the case in the block.
switch (a)
{
case 0:
// Never OK, break outside block
{
x = 1;
}
break;
case 1:
// Never OK, statement outside block
System.out.println("Hello");
{
x = 2;
break;
}
case 1:
// OK if allowInSwitchCase is true
{
System.out.println("Hello");
x = 2;
break;
}
}
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY_BLOCK_NESTED
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
AvoidNestedBlocksCheck() |
| 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 |
setAllowInSwitchCase(boolean allowInSwitchCase)
Setter for allowInSwitchCase property.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setClassLoader, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final String MSG_KEY_BLOCK_NESTED
public AvoidNestedBlocksCheck()
public 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 processpublic void setAllowInSwitchCase(boolean allowInSwitchCase)
allowInSwitchCase - whether nested blocks are allowed
if they are the only child of a switch case.Copyright © 2001–2018. All rights reserved.