Class NeedBracesCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
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
andelse
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
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description NeedBracesCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY_NEED_BRACES
public static final java.lang.String MSG_KEY_NEED_BRACES
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
allowSingleLineStatement
private boolean allowSingleLineStatement
Check's option for skipping single-line statements.
-
allowEmptyLoopBody
private boolean allowEmptyLoopBody
Check's option for allowing loops with empty body.
-
-
Method Detail
-
setAllowSingleLineStatement
public void setAllowSingleLineStatement(boolean allowSingleLineStatement)
Setter.- Parameters:
allowSingleLineStatement
- Check's option for skipping single-line statements
-
setAllowEmptyLoopBody
public void setAllowEmptyLoopBody(boolean allowEmptyLoopBody)
Sets whether to allow empty loop body.- Parameters:
allowEmptyLoopBody
- Check's option for allowing loops with empty body.
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
isDefaultInAnnotation
private boolean isDefaultInAnnotation(DetailAST ast)
Checks if ast is the default token of an annotation field.- Parameters:
ast
- ast to test.- Returns:
- true if current ast is default and it is part of annotation.
-
isSkipStatement
private boolean isSkipStatement(DetailAST statement)
Checks if current statement can be skipped by "need braces" warning.- Parameters:
statement
- if, for, while, do-while, lambda, else, case, default statements.- Returns:
- true if current statement can be skipped by Check.
-
isEmptyLoopBody
private static boolean isEmptyLoopBody(DetailAST ast)
Checks if current loop statement does not have body, e.g.:while (value.incrementValue() < 5); ... for(int i = 0; i < 10; value.incrementValue());
- Parameters:
ast
- ast token.- Returns:
- true if current loop statement does not have body.
-
isSingleLineStatement
private static boolean isSingleLineStatement(DetailAST statement)
Checks if current statement is single-line statement, e.g.:if (obj.isValid()) return true;
while (obj.isValid()) return true;
- Parameters:
statement
- if, for, while, do-while, lambda, else, case, default statements.- Returns:
- true if current statement is single-line statement.
-
isSingleLineWhile
private static boolean isSingleLineWhile(DetailAST literalWhile)
Checks if current while statement is single-line statement, e.g.:while (obj.isValid()) return true;
- Parameters:
literalWhile
-while statement
.- Returns:
- true if current while statement is single-line statement.
-
isSingleLineDoWhile
private static boolean isSingleLineDoWhile(DetailAST literalDo)
Checks if current do-while statement is single-line statement, e.g.:do this.notify(); while (o != null);
- Parameters:
literalDo
-do-while statement
.- Returns:
- true if current do-while statement is single-line statement.
-
isSingleLineFor
private static boolean isSingleLineFor(DetailAST literalFor)
Checks if current for statement is single-line statement, e.g.:for (int i = 0; ; ) this.notify();
- Parameters:
literalFor
-for statement
.- Returns:
- true if current for statement is single-line statement.
-
isSingleLineIf
private static boolean isSingleLineIf(DetailAST literalIf)
Checks if current if statement is single-line statement, e.g.:if (obj.isValid()) return true;
- Parameters:
literalIf
-if statement
.- Returns:
- true if current if statement is single-line statement.
-
isSingleLineLambda
private static boolean isSingleLineLambda(DetailAST lambda)
Checks if current lambda statement is single-line statement, e.g.:Runnable r = () -> System.out.println("Hello, world!");
- Parameters:
lambda
-lambda statement
.- Returns:
- true if current lambda statement is single-line statement.
-
isSingleLineCase
private static boolean isSingleLineCase(DetailAST literalCase)
Checks if current case statement is single-line statement, e.g.:case 1: doSomeStuff(); break; case 2: doSomeStuff(); break; case 3: ;
- Parameters:
literalCase
-case statement
.- Returns:
- true if current case statement is single-line statement.
-
isSingleLineDefault
private static boolean isSingleLineDefault(DetailAST literalDefault)
Checks if current default statement is single-line statement, e.g.:default: doSomeStuff();
- Parameters:
literalDefault
-default statement
.- Returns:
- true if current default statement is single-line statement.
-
isSingleLineElse
private static boolean isSingleLineElse(DetailAST literalElse)
Checks if current else statement is single-line statement, e.g.:else doSomeStuff();
- Parameters:
literalElse
-else statement
.- Returns:
- true if current else statement is single-line statement.
-
-