public class SimplifyBooleanExpressionCheck extends AbstractCheck
Checks for over-complicated boolean expressions. Currently finds code like
if (b == true), b || true, !false,
etc.
Rationale: Complex boolean logic makes code hard to understand and maintain.
To configure the check:
<module name="SimplifyBooleanExpression"/>
Example:
public class Test {
public void bar() {
boolean a, b;
Foo c, d, e;
if (!false) {}; // violation, can be simplified to true
if (a == true) {}; // violation, can be simplified to a
if (a == b) {}; // OK
if (a == false) {}; // violation, can be simplified to !a
if (!(a != true)) {}; // violation, can be simplified to a
e = (a || b) ? c : d; // OK
e = (a || false) ? c : d; // violation, can be simplified to a
e = (a && b) ? c : d; // OK
}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
simplify.expression
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
SimplifyBooleanExpressionCheck() |
| Modifier and Type | Method and 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.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final String MSG_KEY
public SimplifyBooleanExpressionCheck()
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 processCopyright © 2001–2020. All rights reserved.