public class FallThroughCheck extends AbstractCheck
The check honors special comments to suppress warnings about the fall through. By default the comments "fallthru", "fall through", "falls through" and "fallthrough" are recognized.
The following fragment of code will NOT trigger the check, because of the comment "fallthru" and absence of any Java code in case 5.
case 3: x = 2; // fallthru case 4: case 5: case 6: break;
The recognized relief comment can be configured with the property
reliefPattern
. Default value of this regular expression
is "fallthru|fall through|fallthrough|falls through".
An example of how to configure the check is:
<module name="FallThrough"> <property name="reliefPattern" value="Fall Through"/> </module>
Modifier and Type | Field and Description |
---|---|
private boolean |
checkLastCaseGroup
Do we need to check last case group.
|
static java.lang.String |
MSG_FALL_THROUGH
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_FALL_THROUGH_LAST
A key is pointing to the warning message text in "messages.properties"
file.
|
private java.util.regex.Pattern |
reliefPattern
Relief regexp to allow fall through to the next case branch.
|
Constructor and Description |
---|
FallThroughCheck() |
Modifier and Type | Method and Description |
---|---|
private boolean |
checkIf(DetailAST ast,
boolean useBreak,
boolean useContinue)
Checks if a given IF terminated by return, throw or,
if allowed break, continue.
|
private boolean |
checkLoop(DetailAST ast)
Checks if a given loop terminated by return, throw or,
if allowed break, continue.
|
private boolean |
checkSlist(DetailAST slistAst,
boolean useBreak,
boolean useContinue)
Checks if a given SLIST terminated by return, throw or,
if allowed break, continue.
|
private boolean |
checkSwitch(DetailAST literalSwitchAst,
boolean useContinue)
Checks if a given switch terminated by return, throw or,
if allowed break, continue.
|
private boolean |
checkTry(DetailAST ast,
boolean useBreak,
boolean useContinue)
Checks if a given try/catch/finally block terminated by return, throw or,
if allowed break, continue.
|
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 |
hasFallThroughComment(DetailAST currentCase,
DetailAST nextCase)
Determines if the fall through case between
currentCase and
nextCase is relieved by a appropriate comment. |
private boolean |
isTerminated(DetailAST ast,
boolean useBreak,
boolean useContinue)
Checks if a given subtree terminated by return, throw or,
if allowed break, continue.
|
private boolean |
matchesComment(java.util.regex.Pattern pattern,
java.lang.String line,
int lineNo)
Does a regular expression match on the given line and checks that a
possible match is within a comment.
|
void |
setCheckLastCaseGroup(boolean value)
Configures whether we need to check last case group or not.
|
void |
setReliefPattern(java.util.regex.Pattern pattern)
Set the relief pattern.
|
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_FALL_THROUGH
public static final java.lang.String MSG_FALL_THROUGH_LAST
private boolean checkLastCaseGroup
private java.util.regex.Pattern reliefPattern
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public void setReliefPattern(java.util.regex.Pattern pattern)
pattern
- The regular expression pattern.public void setCheckLastCaseGroup(boolean value)
value
- new value of the property.public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate boolean isTerminated(DetailAST ast, boolean useBreak, boolean useContinue)
ast
- root of given subtreeuseBreak
- should we consider break as terminator.useContinue
- should we consider continue as terminator.private boolean checkSlist(DetailAST slistAst, boolean useBreak, boolean useContinue)
slistAst
- SLIST to checkuseBreak
- should we consider break as terminator.useContinue
- should we consider continue as terminator.private boolean checkIf(DetailAST ast, boolean useBreak, boolean useContinue)
ast
- IF to checkuseBreak
- should we consider break as terminator.useContinue
- should we consider continue as terminator.private boolean checkLoop(DetailAST ast)
ast
- loop to checkprivate boolean checkTry(DetailAST ast, boolean useBreak, boolean useContinue)
ast
- loop to checkuseBreak
- should we consider break as terminator.useContinue
- should we consider continue as terminator.private boolean checkSwitch(DetailAST literalSwitchAst, boolean useContinue)
literalSwitchAst
- loop to checkuseContinue
- should we consider continue as terminator.private boolean hasFallThroughComment(DetailAST currentCase, DetailAST nextCase)
currentCase
and
nextCase
is relieved by a appropriate comment.currentCase
- AST of the case that falls through to the next case.nextCase
- AST of the next case.private boolean matchesComment(java.util.regex.Pattern pattern, java.lang.String line, int lineNo)
pattern
- The regular expression pattern to use.line
- The line of test to do the match on.lineNo
- The line number in the file.