public class NeedBracesCheck extends AbstractCheck
Checks for braces around code blocks.
allowSingleLineStatement - allow single-line statements without braces.
Type is boolean.
Default value is false.
allowEmptyLoopBody - allow loops with empty bodies.
Type is boolean.
Default value is false.
tokens - tokens to check
Type is java.lang.String[].
Validation type is tokenSet.
Default value is:
LITERAL_DO,
LITERAL_ELSE,
LITERAL_FOR,
LITERAL_IF,
LITERAL_WHILE.
To configure the check:
<module name="NeedBraces"/>
Example:
if (obj.isValid()) return true; // violation, single-line statements not allowed without braces
if (true) { // OK
return true;
} else // violation, single-line statements not allowed without braces
return false;
for (int i = 0; i < 5; i++) { // OK
++count;
}
do // violation, single-line statements not allowed without braces
++count;
while (false);
for (int j = 0; j < 10; j++); // violation, empty loop body not allowed
for(int i = 0; i < 10; value.incrementValue()); // violation, empty loop body not allowed
while (counter < 10) // violation, single-line statements not allowed without braces
++count;
while (value.incrementValue() < 5); // violation, empty loop body not allowed
switch (num) {
case 1: counter++; break; // OK
}
To configure the check for if and else blocks:
<module name="NeedBraces"> <property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/> </module>
Example:
if (obj.isValid()) return true; // violation, single-line statements not allowed without braces
if (true) { // OK
return true;
} else // violation, single-line statements not allowed without braces
return false;
for (int i = 0; i < 5; i++) { // OK
++count;
}
do // OK
++count;
while (false);
for (int j = 0; j < 10; j++); // OK
for(int i = 0; i < 10; value.incrementValue()); // OK
while (counter < 10) // OK
++count;
while (value.incrementValue() < 5); // OK
switch (num) {
case 1: counter++; break; // OK
}
To configure the check to allow single-line statements
(if, while, do-while, for) without braces:
<module name="NeedBraces">
<property name="allowSingleLineStatement" value="true"/>
<property name="tokens"
value="LITERAL_IF, LITERAL_WHILE, LITERAL_DO, LITERAL_FOR"/>
</module>
Example:
if (obj.isValid()) return true; // OK
if (true) { // OK
return true;
} else // OK
return false;
for (int i = 0; i < 5; i++) { // OK
++count;
}
do // OK
++count;
while (false);
for (int j = 0; j < 10; j++); // violation, empty loop body not allowed
for(int i = 0; i < 10; value.incrementValue()); // violation, empty loop body not allowed
while (counter < 10) // OK
++count;
while (value.incrementValue() < 5); // violation, empty loop body not allowed
switch (num) {
case 1: counter++; break; // OK
}
while (obj.isValid()) return true; // OK
do this.notify(); while (o != null); // OK
for (int i = 0; ; ) this.notify(); // OK
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>
Next statements won't be violated by check:
if (obj.isValid()) return true; // OK
if (true) { // OK
return true;
} else // OK
return false;
for (int i = 0; i < 5; i++) { // OK
++count;
}
do // OK
++count;
while (false);
for (int j = 0; j < 10; j++); // OK
for(int i = 0; i < 10; value.incrementValue()); // OK
while (counter < 10) // OK
++count;
while (value.incrementValue() < 5); // OK
switch (num) {
case 1: counter++; break; // OK
case 6: counter += 10; break; // OK
default: counter = 100; break; // OK
}
To configure the check to allow loops (while, for) with empty bodies:
<module name="NeedBraces"> <property name="allowEmptyLoopBody" value="true"/> <property name="tokens" value="LITERAL_WHILE, LITERAL_FOR"/> </module>
Example:
if (obj.isValid()) return true; // OK
if (true) { // OK
return true;
} else // OK
return false;
for (int i = 0; i < 5; i++) { // OK
++count;
}
do // OK
++count;
while (false);
for (int j = 0; j < 10; j++); // OK
for(int i = 0; i < 10; value.incrementValue()); // OK
while (counter < 10) // violation, single-line statements not allowed without braces
++count;
while (value.incrementValue() < 5); // OK
switch (num) {
case 1: counter++; break; // OK
}
To configure the check to lambdas:
<module name="NeedBraces"> <property name="tokens" value="LAMBDA"/> <property name="allowSingleLineStatement" value="true"/> </module>
Results in following:
allowedFuture.addCallback(result -> assertEquals("Invalid response",
EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS), result), // violation, lambda spans 2 lines
ex -> fail(ex.getMessage())); // OK
allowedFuture.addCallback(result -> {
return assertEquals("Invalid response",
EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS), result);
}, // OK
ex -> fail(ex.getMessage()));
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
needBraces
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private boolean |
allowEmptyLoopBody
Allow loops with empty bodies.
|
private boolean |
allowSingleLineStatement
Allow single-line statements without braces.
|
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.
|
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.
|
private static boolean |
isSwitchLabeledExpression(DetailAST ast)
Checks if current expression is a switch labeled expression.
|
void |
setAllowEmptyLoopBody(boolean allowEmptyLoopBody)
Setter to allow loops with empty bodies.
|
void |
setAllowSingleLineStatement(boolean allowSingleLineStatement)
Setter to allow single-line statements without braces.
|
private static boolean |
switchRuleHasSingleExpression(DetailAST switchRule)
Checks if current switch labeled expression contains only a single expression.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY_NEED_BRACES
private boolean allowSingleLineStatement
private boolean allowEmptyLoopBody
public NeedBracesCheck()
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()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate boolean isBracesNeeded(DetailAST ast)
TokenTypes.LITERAL_FORTokenTypes.LITERAL_WHILETokenTypes.LITERAL_CASETokenTypes.LITERAL_DEFAULTTokenTypes.LITERAL_ELSETokenTypes.LAMBDAtrue is returned.ast - token to checktrue if there is no additional checks for tokenprivate boolean isEmptyLoopBodyAllowed(DetailAST ast)
ast - for, while statements.private static boolean hasUnbracedStatements(DetailAST ast)
ast - case, default statements.private boolean isSkipStatement(DetailAST statement)
statement - if, for, while, do-while, lambda, else, case, default statements.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 DetailAST getLastLambdaToken(DetailAST lambda)
lambda - token to check.private static boolean isInSwitchRule(DetailAST ast)
case 1 -> monthString = "January";
ast - the ast to check.private static boolean isSwitchLabeledExpression(DetailAST ast)
case 1 -> 4;
ast - the ast to checkprivate static boolean switchRuleHasSingleExpression(DetailAST switchRule)
switchRule - TokenTypes.SWITCH_RULE.private static boolean isSingleLineSwitchMember(DetailAST statement)
statement - case statement or
default statement.private static boolean isSingleLineCaseGroup(DetailAST ast)
case 1: System.out.println("case one"); break;
case 2: System.out.println("case two"); break;
case 3: ;
default: System.out.println("default"); break;
ast - case statement or
default statement.private static boolean isSingleLineSwitchRule(DetailAST ast)
case 1 -> System.out.println("case one");
case 2 -> System.out.println("case two");
default -> System.out.println("default");
ast - case statement or
default statement.private static boolean isSingleLineElse(DetailAST literalElse)
else doSomeStuff();
literalElse - else statement.Copyright © 2001-2022. All Rights Reserved.