public class NeedBracesCheck extends AbstractCheck
Checks for braces around code blocks.
By default the check will check the following blocks:
LITERAL_DO
,
LITERAL_ELSE
,
LITERAL_FOR
,
LITERAL_IF
,
LITERAL_WHILE
.
An example of how to configure the check is:
<module name="NeedBraces"/>
An example of how to configure the check for if
and
else
blocks is:
<module name="NeedBraces"> <property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/> </module>Check has the following options:
allowSingleLineStatement which allows single-line statements without braces, e.g.:
if (obj.isValid()) return true;
while (obj.isValid()) return true;
do this.notify(); while (o != null);
for (int i = 0; ; ) this.notify();
allowEmptyLoopBody which allows loops with empty bodies, e.g.:
while (value.incrementValue() < 5);
for(int i = 0; i < 10; value.incrementValue());
Default value for allowEmptyLoopBody option is false.
To configure the Check to allow case, default
single-line statements
without braces:
<module name="NeedBraces"> <property name="tokens" value="LITERAL_CASE, LITERAL_DEFAULT"/> <property name="allowSingleLineStatement" value="true"/> </module>
Such statements would be allowed:
switch (num) {
case 1: counter++; break; // OK
case 6: counter += 10; break; // OK
default: counter = 100; break; // OK
}
To configure the Check to allow while, for
loops with empty bodies:
<module name="NeedBraces"> <property name="allowEmptyLoopBody" value="true"/> </module>
Such statements would be allowed:
while (value.incrementValue() < 5); // OK
for(int i = 0; i < 10; value.incrementValue()); // OK
Modifier and Type | Field and Description |
---|---|
private boolean |
allowEmptyLoopBody
Check's option for allowing loops with empty body.
|
private boolean |
allowSingleLineStatement
Check's option for skipping single-line statements.
|
static java.lang.String |
MSG_KEY_NEED_BRACES
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
NeedBracesCheck() |
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.
|
private boolean |
isDefaultInAnnotation(DetailAST ast)
Checks if ast is the default token of an annotation field.
|
private static boolean |
isEmptyLoopBody(DetailAST ast)
Checks if current loop statement does not have body, e.g.:
|
private static boolean |
isSingleLineCase(DetailAST literalCase)
Checks if current case statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineDefault(DetailAST literalDefault)
Checks if current default statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineDoWhile(DetailAST literalDo)
Checks if current do-while statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineElse(DetailAST literalElse)
Checks if current else statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineFor(DetailAST literalFor)
Checks if current for statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineIf(DetailAST literalIf)
Checks if current if statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineLambda(DetailAST lambda)
Checks if current lambda statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineStatement(DetailAST statement)
Checks if current statement is single-line statement, e.g.:
|
private static boolean |
isSingleLineWhile(DetailAST literalWhile)
Checks if current while statement is single-line statement, e.g.:
|
private boolean |
isSkipStatement(DetailAST statement)
Checks if current statement can be skipped by "need braces" warning.
|
void |
setAllowEmptyLoopBody(boolean allowEmptyLoopBody)
Sets whether to allow empty loop body.
|
void |
setAllowSingleLineStatement(boolean allowSingleLineStatement)
Setter.
|
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_KEY_NEED_BRACES
private boolean allowSingleLineStatement
private boolean allowEmptyLoopBody
public void setAllowSingleLineStatement(boolean allowSingleLineStatement)
allowSingleLineStatement
- Check's option for skipping single-line statementspublic void setAllowEmptyLoopBody(boolean allowEmptyLoopBody)
allowEmptyLoopBody
- Check's option for allowing loops with empty body.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate boolean isDefaultInAnnotation(DetailAST ast)
ast
- ast to test.private boolean isSkipStatement(DetailAST statement)
statement
- if, for, while, do-while, lambda, else, case, default statements.private static boolean isEmptyLoopBody(DetailAST ast)
while (value.incrementValue() < 5);
...
for(int i = 0; i < 10; value.incrementValue());
ast
- ast token.private static boolean isSingleLineStatement(DetailAST statement)
if (obj.isValid()) return true;
while (obj.isValid()) return true;
statement
- if, for, while, do-while, lambda, else, case, default statements.private static boolean isSingleLineWhile(DetailAST literalWhile)
while (obj.isValid()) return true;
literalWhile
- while statement
.private static boolean isSingleLineDoWhile(DetailAST literalDo)
do this.notify(); while (o != null);
literalDo
- do-while statement
.private static boolean isSingleLineFor(DetailAST literalFor)
for (int i = 0; ; ) this.notify();
literalFor
- for statement
.private static boolean isSingleLineIf(DetailAST literalIf)
if (obj.isValid()) return true;
literalIf
- if statement
.private static boolean isSingleLineLambda(DetailAST lambda)
Runnable r = () -> System.out.println("Hello, world!");
lambda
- lambda statement
.private static boolean isSingleLineCase(DetailAST literalCase)
case 1: doSomeStuff(); break;
case 2: doSomeStuff(); break;
case 3: ;
literalCase
- case statement
.private static boolean isSingleLineDefault(DetailAST literalDefault)
default: doSomeStuff();
literalDefault
- default statement
.private static boolean isSingleLineElse(DetailAST literalElse)
else doSomeStuff();
literalElse
- else statement
.