public class UnnecessaryParenthesesCheck extends AbstractCheck
Checks if unnecessary parentheses are used in a statement or expression. The check will flag the following with warnings:
return (x); // parens around identifier return (x + 1); // parens around return value int x = (y / 2 + 1); // parens around assignment rhs for (int i = (0); i < 10; i++) { // parens around literal t -= (z + 1); // parens around assignment rhs
The check is not "type aware", that is to say, it can't tell if parentheses are unnecessary based on the types in an expression. It also doesn't know about operator precedence and associativity; therefore it won't catch something like
int x = (a + b) + c;
In the above case, given that a, b, and c are
all int
variables, the parentheses around a + b
are not needed.
Modifier and Type | Field and Description |
---|---|
private int |
assignDepth
Depth of nested assignments.
|
private static int[] |
ASSIGNMENTS
Token types for assignment operations.
|
private static int[] |
LITERALS
Token types for literals.
|
private static int |
MAX_QUOTED_LENGTH
The maximum string length before we chop the string.
|
static java.lang.String |
MSG_ASSIGN
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_EXPR
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_IDENT
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_LITERAL
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_RETURN
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_STRING
A key is pointing to the warning message text in "messages.properties"
file.
|
private DetailAST |
parentToSkip
Used to test if logging a warning in a parent node may be skipped
because a warning was already logged on an immediate child node.
|
Constructor and Description |
---|
UnnecessaryParenthesesCheck() |
Modifier and Type | Method and Description |
---|---|
private static java.lang.String |
chopString(java.lang.String value)
Returns the specified string chopped to
MAX_QUOTED_LENGTH
plus an ellipsis (...) if the length of the string exceeds MAX_QUOTED_LENGTH . |
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.
|
private static boolean |
isExprSurrounded(DetailAST ast)
Tests if the given expression node is surrounded by parentheses.
|
private static boolean |
isInTokenList(int type,
int... tokens)
Check if the given token type can be found in an array of token types.
|
private static boolean |
isSurrounded(DetailAST ast)
Tests if the given
DetailAST is surrounded by parentheses. |
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_IDENT
public static final java.lang.String MSG_ASSIGN
public static final java.lang.String MSG_EXPR
public static final java.lang.String MSG_LITERAL
public static final java.lang.String MSG_STRING
public static final java.lang.String MSG_RETURN
private static final int MAX_QUOTED_LENGTH
private static final int[] LITERALS
private static final int[] ASSIGNMENTS
private DetailAST parentToSkip
private int assignDepth
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processpublic void leaveToken(DetailAST ast)
AbstractCheck
leaveToken
in class AbstractCheck
ast
- the token leavingprivate static boolean isSurrounded(DetailAST ast)
DetailAST
is surrounded by parentheses.
In short, does ast
have a previous sibling whose type is
TokenTypes.LPAREN
and a next sibling whose type is TokenTypes.RPAREN
.ast
- the DetailAST
to check if it is surrounded by
parentheses.true
if ast
is surrounded by
parentheses.private static boolean isExprSurrounded(DetailAST ast)
ast
- a DetailAST
whose type is
TokenTypes.EXPR
.true
if the expression is surrounded by
parentheses.private static boolean isInTokenList(int type, int... tokens)
type
- the token type.tokens
- an array of token types to search.true
if type
was found in tokens
.private static java.lang.String chopString(java.lang.String value)
MAX_QUOTED_LENGTH
plus an ellipsis (...) if the length of the string exceeds MAX_QUOTED_LENGTH
.value
- the string to potentially chop.string
is longer than
MAX_QUOTED_LENGTH
; otherwise string
.