Class NeedBracesCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Attention: The break in case blocks is not counted to allow compact view.
- Since:
- 3.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanAllow loops with empty bodies.private booleanAllow single-line statements without braces.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]The configurable token set.int[]Returns the default token a check is interested in.private static DetailASTgetLastLambdaToken(DetailAST lambda) Looks for the last token in lambda.int[]The tokens that this check must be registered for.private static booleanChecks if switch member (case, default statements) has statements without curly braces.private booleanisBracesNeeded(DetailAST ast) Checks if token needs braces.private booleanChecks if current loop has empty body and can be skipped by this check.private static booleanisInSwitchRule(DetailAST ast) Checks if current ast's parent is a switch rule, e.g.:private static booleanChecks if switch member in case group (case or default statement) is single-line statement, e.g.:private static booleanisSingleLineDoWhile(DetailAST literalDo) Checks if current do-while statement is single-line statement, e.g.:private static booleanisSingleLineElse(DetailAST literalElse) Checks if current else statement is single-line statement, e.g.:private static booleanisSingleLineFor(DetailAST literalFor) Checks if current for statement is single-line statement, e.g.:private static booleanisSingleLineIf(DetailAST literalIf) Checks if current if statement is single-line statement, e.g.:private static booleanisSingleLineLambda(DetailAST lambda) Checks if current lambda statement is single-line statement, e.g.:private static booleanisSingleLineStatement(DetailAST statement) Checks if current statement is single-line statement, e.g.:private static booleanisSingleLineSwitchMember(DetailAST statement) Checks if switch member (case or default statement) in a switch rule or case group is on a single-line.private static booleanChecks if switch member in switch rule (case or default statement) is single-line statement, e.g.:private static booleanisSingleLineWhile(DetailAST literalWhile) Checks if current while statement is single-line statement, e.g.:private booleanisSkipStatement(DetailAST statement) Checks if current statement can be skipped by "need braces" warning.voidsetAllowEmptyLoopBody(boolean allowEmptyLoopBody) Setter to allow loops with empty bodies.voidsetAllowSingleLineStatement(boolean allowSingleLineStatement) Setter to allow single-line statements without braces.voidvisitToken(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, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods 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:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
isBracesNeeded
Checks if token needs braces. Some tokens have additional conditions:TokenTypes.LITERAL_FORTokenTypes.LITERAL_WHILETokenTypes.LITERAL_CASETokenTypes.LITERAL_DEFAULTTokenTypes.LITERAL_ELSETokenTypes.LAMBDA
trueis returned.- Parameters:
ast- token to check- Returns:
- result of additional checks for specific token types,
trueif 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 statementordefault 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 statementordefault 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 statementordefault 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.
-