public class EqualsAvoidNullCheck extends AbstractCheck
someString.equals(anotherString = "text")
).
Rationale: Calling the equals() method on String literals will avoid a potential NullPointerException. Also, it is pretty common to see null check right before equals comparisons which is not necessary in the below example.
For example:
String nullString = null;
nullString.equals("My_Sweet_String");
should be refactored to
String nullString = null;
"My_Sweet_String".equals(nullString);
Modifier and Type | Class and Description |
---|---|
private static class |
EqualsAvoidNullCheck.FieldFrame
Holds the names of fields of a type.
|
Modifier and Type | Field and Description |
---|---|
private EqualsAvoidNullCheck.FieldFrame |
currentFrame
Stack of sets of field names, one for each class of a set of nested classes.
|
private static java.lang.String |
EQUALS
Method name for comparison.
|
private boolean |
ignoreEqualsIgnoreCase
Whether to process equalsIgnoreCase() invocations.
|
static java.lang.String |
MSG_EQUALS_AVOID_NULL
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_EQUALS_IGNORE_CASE_AVOID_NULL
A key is pointing to the warning message text in "messages.properties"
file.
|
private static java.lang.String |
STRING
Type name for comparison.
|
Constructor and Description |
---|
EqualsAvoidNullCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
private static boolean |
checkLineNo(DetailAST field,
DetailAST objCalledOn)
Check whether the field is declared before the method call in case of
methods and initialization blocks.
|
private void |
checkMethodCall(DetailAST methodCall)
Check whether the method call should be violated.
|
private static boolean |
containsAllSafeTokens(DetailAST expr)
Looks for all "safe" Token combinations in the argument
expression branch.
|
private static boolean |
containsOneArgument(DetailAST methodCall)
Verify that method call has one argument.
|
void |
finishTree(DetailAST ast)
Called after finished processing a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static java.lang.String |
getFieldType(DetailAST field)
Get field type.
|
private static EqualsAvoidNullCheck.FieldFrame |
getObjectFrame(EqualsAvoidNullCheck.FieldFrame frame)
Get the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private boolean |
isCalledOnStringFieldOrVariable(DetailAST objCalledOn)
Determine, whether equals method is called on a field of String type.
|
private static boolean |
isObjectValid(DetailAST objCalledOn)
Check whether the object equals method is called on is not a String literal
and not too complex.
|
private boolean |
isStringFieldOrVariable(DetailAST objCalledOn)
Whether the field or the variable is of String type.
|
private boolean |
isStringFieldOrVariableFromClass(DetailAST objCalledOn,
java.lang.String className)
Whether the field or the variable from the specified class is of String type.
|
private boolean |
isStringFieldOrVariableFromThisInstance(DetailAST objCalledOn)
Whether the field or the variable from THIS instance is of String type.
|
private static boolean |
isStringLiteral(DetailAST objCalledOn)
Checks for calling equals on String literal and
anon object which cannot be null.
|
private void |
leaveSlist(DetailAST ast)
Determine whether SLIST begins static or non-static block.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
private void |
processFrame(DetailAST ast)
Process CLASS_DEF, METHOD_DEF, LITERAL_IF, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO,
LITERAL_CATCH, LITERAL_TRY, CTOR_DEF, ENUM_DEF, ENUM_CONSTANT_DEF.
|
private void |
processLiteralNew(DetailAST ast)
Determine whether LITERAL_NEW is an anonymous class definition and add it as
a frame in this case.
|
private void |
processMethodCall(DetailAST methodCall)
Add the method call to the current frame if it should be processed.
|
private void |
processSlist(DetailAST ast)
Determine whether SLIST begins static or non-static block and add it as
a frame in this case.
|
void |
setIgnoreEqualsIgnoreCase(boolean newValue)
Whether to ignore checking
String.equalsIgnoreCase(String) . |
private static DetailAST |
skipVariableAssign(DetailAST currentAST)
Skips over an inner assign portion of an argument expression.
|
private void |
traverseFieldFrameTree(EqualsAvoidNullCheck.FieldFrame frame)
Traverse the tree of the field frames to check all equals method calls.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
destroy, 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_EQUALS_AVOID_NULL
public static final java.lang.String MSG_EQUALS_IGNORE_CASE_AVOID_NULL
private static final java.lang.String EQUALS
private static final java.lang.String STRING
private boolean ignoreEqualsIgnoreCase
private EqualsAvoidNullCheck.FieldFrame currentFrame
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 setIgnoreEqualsIgnoreCase(boolean newValue)
String.equalsIgnoreCase(String)
.newValue
- whether to ignore checking
String.equalsIgnoreCase(String)
.public void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treepublic 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 leavingpublic void finishTree(DetailAST ast)
AbstractCheck
finishTree
in class AbstractCheck
ast
- the root of the treeprivate void processSlist(DetailAST ast)
ast
- SLIST ast.private void leaveSlist(DetailAST ast)
ast
- SLIST ast.private void processFrame(DetailAST ast)
ast
- processed ast.private void processMethodCall(DetailAST methodCall)
methodCall
- METHOD_CALL ast.private void processLiteralNew(DetailAST ast)
ast
- LITERAL_NEW ast.private void traverseFieldFrameTree(EqualsAvoidNullCheck.FieldFrame frame)
frame
- to check method calls in.private void checkMethodCall(DetailAST methodCall)
methodCall
- method call to check.private static boolean isObjectValid(DetailAST objCalledOn)
objCalledOn
- the object equals method is called on ast.private static boolean isStringLiteral(DetailAST objCalledOn)
objCalledOn
- object ASTprivate static boolean containsOneArgument(DetailAST methodCall)
methodCall
- METHOD_CALL DetailASTprivate static boolean containsAllSafeTokens(DetailAST expr)
expr
- the argument expressionprivate static DetailAST skipVariableAssign(DetailAST currentAST)
currentAST
- current token in the argument expressionprivate boolean isCalledOnStringFieldOrVariable(DetailAST objCalledOn)
objCalledOn
- object ast.private boolean isStringFieldOrVariable(DetailAST objCalledOn)
objCalledOn
- the field or the variable to check.private boolean isStringFieldOrVariableFromThisInstance(DetailAST objCalledOn)
objCalledOn
- the field or the variable from THIS instance to check.private boolean isStringFieldOrVariableFromClass(DetailAST objCalledOn, java.lang.String className)
objCalledOn
- the field or the variable from the specified class to check.className
- the name of the class to check in.private static EqualsAvoidNullCheck.FieldFrame getObjectFrame(EqualsAvoidNullCheck.FieldFrame frame)
frame
- to start the search from.private static boolean checkLineNo(DetailAST field, DetailAST objCalledOn)
field
- field to check.objCalledOn
- object equals method called on.private static java.lang.String getFieldType(DetailAST field)
field
- to get the type from.