Class NeedBracesCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
Attention: The break in case blocks is not counted to allow compact view.
-
Property
allowEmptyLoopBody
- Allow loops with empty bodies. Type isboolean
. Default value isfalse
. -
Property
allowSingleLineStatement
- Allow single-line statements without braces. Type isboolean
. Default value isfalse
. -
Property
tokens
- tokens to check Type isjava.lang.String[]
. Validation type istokenSet
. Default value is: LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, LITERAL_WHILE.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
needBraces
- Since:
- 3.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
Modifier and TypeFieldDescriptionprivate boolean
Allow loops with empty bodies.private boolean
Allow single-line statements without braces.static final String
A key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint[]
The configurable token set.int[]
Returns the default token a check is interested in.private static DetailAST
getLastLambdaToken
(DetailAST lambda) Looks for the last token in lambda.int[]
The tokens that this check must be registered for.private static boolean
Checks if switch member (case, default statements) has statements without curly braces.private boolean
isBracesNeeded
(DetailAST ast) Checks if token needs braces.private boolean
Checks if current loop has empty body and can be skipped by this check.private static boolean
isInSwitchRule
(DetailAST ast) Checks if current ast's parent is a switch rule, e.g.:private static boolean
Checks if switch member in case group (case or 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
isSingleLineSwitchMember
(DetailAST statement) Checks if switch member (case or default statement) in a switch rule or case group is on a single-line.private static boolean
Checks if switch member in switch rule (case or default 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) Setter to allow loops with empty bodies.void
setAllowSingleLineStatement
(boolean allowSingleLineStatement) Setter to allow single-line statements without braces.void
visitToken
(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY_NEED_BRACES
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
allowSingleLineStatement
Allow single-line statements without braces. -
allowEmptyLoopBody
Allow loops with empty bodies.
-
-
Constructor Details
-
NeedBracesCheck
public NeedBracesCheck()
-
-
Method Details
-
setAllowSingleLineStatement
Setter to allow single-line statements without braces.- Parameters:
allowSingleLineStatement
- Check's option for skipping single-line statements- Since:
- 6.5
-
setAllowEmptyLoopBody
Setter to allow loops with empty bodies.- Parameters:
allowEmptyLoopBody
- Check's option for allowing loops with empty body.- Since:
- 6.12.1
-
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:
-
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:
-
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:
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
isBracesNeeded
Checks if token needs braces. Some tokens have additional conditions:TokenTypes.LITERAL_FOR
TokenTypes.LITERAL_WHILE
TokenTypes.LITERAL_CASE
TokenTypes.LITERAL_DEFAULT
TokenTypes.LITERAL_ELSE
TokenTypes.LAMBDA
true
is returned.- Parameters:
ast
- token to check- Returns:
- result of additional checks for specific token types,
true
if there is no additional checks for token
-
isEmptyLoopBodyAllowed
Checks if current loop has empty body and can be skipped by this check.- Parameters:
ast
- for, while statements.- Returns:
- true if current loop can be skipped by check.
-
hasUnbracedStatements
Checks if switch member (case, default statements) has statements without curly braces.- Parameters:
ast
- case, default statements.- Returns:
- true if switch member has unbraced statements, false otherwise.
-
isSkipStatement
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.
-
isSingleLineStatement
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
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
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
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
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
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.
-
getLastLambdaToken
Looks for the last token in lambda.- Parameters:
lambda
- token to check.- Returns:
- last token in lambda
-
isInSwitchRule
Checks if current ast's parent is a switch rule, e.g.:case 1 -> monthString = "January";
- Parameters:
ast
- the ast to check.- Returns:
- true if current ast belongs to a switch rule.
-
isSingleLineSwitchMember
Checks if switch member (case or default statement) in a switch rule or case group is on a single-line.- Parameters:
statement
-case statement
ordefault statement
.- Returns:
- true if current switch member is single-line statement.
-
isSingleLineCaseGroup
Checks if switch member in case group (case or default statement) is single-line statement, e.g.:case 1: System.out.println("case one"); break; case 2: System.out.println("case two"); break; case 3: ; default: System.out.println("default"); break;
- Parameters:
ast
-case statement
ordefault statement
.- Returns:
- true if current switch member is single-line statement.
-
isSingleLineSwitchRule
Checks if switch member in switch rule (case or default statement) is single-line statement, e.g.:case 1 -> System.out.println("case one"); case 2 -> System.out.println("case two"); default -> System.out.println("default");
- Parameters:
ast
-case statement
ordefault statement
.- Returns:
- true if current switch label is single-line statement.
-
isSingleLineElse
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.
-