Class NeedBracesCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- 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.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
Fields Modifier and Type Field Description private boolean
allowEmptyLoopBody
Allow loops with empty bodies.private boolean
allowSingleLineStatement
Allow single-line statements without braces.static 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.private static DetailAST
getLastLambdaToken(DetailAST lambda)
Looks for the last token in lambda.int[]
getRequiredTokens()
The tokens that this check must be registered for.private static boolean
hasUnbracedStatements(DetailAST ast)
Checks if switch member (case, default statements) has statements without curly braces.private boolean
isBracesNeeded(DetailAST ast)
Checks if token needs braces.private boolean
isEmptyLoopBodyAllowed(DetailAST ast)
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
isSingleLineCaseGroup(DetailAST ast)
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
isSingleLineSwitchRule(DetailAST ast)
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 Detail
-
MSG_KEY_NEED_BRACES
public static final 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
Allow single-line statements without braces.
-
allowEmptyLoopBody
private boolean allowEmptyLoopBody
Allow loops with empty bodies.
-
-
Constructor Detail
-
NeedBracesCheck
public NeedBracesCheck()
-
-
Method Detail
-
setAllowSingleLineStatement
public void setAllowSingleLineStatement(boolean allowSingleLineStatement)
Setter to allow single-line statements without braces.- Parameters:
allowSingleLineStatement
- Check's option for skipping single-line statements- Since:
- 6.5
-
setAllowEmptyLoopBody
public void setAllowEmptyLoopBody(boolean allowEmptyLoopBody)
Setter to allow loops with empty bodies.- Parameters:
allowEmptyLoopBody
- Check's option for allowing loops with empty body.- Since:
- 6.12.1
-
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
-
isBracesNeeded
private boolean isBracesNeeded(DetailAST ast)
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
private boolean isEmptyLoopBodyAllowed(DetailAST ast)
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
private static boolean hasUnbracedStatements(DetailAST ast)
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
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.
-
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.
-
getLastLambdaToken
private static DetailAST getLastLambdaToken(DetailAST lambda)
Looks for the last token in lambda.- Parameters:
lambda
- token to check.- Returns:
- last token in lambda
-
isInSwitchRule
private static boolean isInSwitchRule(DetailAST ast)
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
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.- Parameters:
statement
-case statement
ordefault statement
.- Returns:
- true if current switch member is single-line statement.
-
isSingleLineCaseGroup
private static boolean isSingleLineCaseGroup(DetailAST ast)
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
private static boolean isSingleLineSwitchRule(DetailAST ast)
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
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.
-
-