public class EqualsAvoidNullCheck extends AbstractCheck
Checks that any combination of String literals
is on the left side of an equals() comparison.
Also checks for String literals assigned to some field
(such as 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 checks right before equals comparisons
but following this rule such checks are not required.
ignoreEqualsIgnoreCase - Control whether to ignore
String.equalsIgnoreCase(String) invocations.
Type is boolean.
Default value is false.
To configure the check:
<module name="EqualsAvoidNull"/>
Example:
String nullString = null;
nullString.equals("My_Sweet_String"); // violation
"My_Sweet_String".equals(nullString); // OK
nullString.equalsIgnoreCase("My_Sweet_String"); // violation
"My_Sweet_String".equalsIgnoreCase(nullString); // OK
To configure the check to allow ignoreEqualsIgnoreCase:
<module name="EqualsAvoidNull"> <property name="ignoreEqualsIgnoreCase" value="true"/> </module>
Example:
String nullString = null;
nullString.equals("My_Sweet_String"); // violation
"My_Sweet_String".equals(nullString); // OK
nullString.equalsIgnoreCase("My_Sweet_String"); // OK
"My_Sweet_String".equalsIgnoreCase(nullString); // OK
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
equals.avoid.null
equalsIgnoreCase.avoid.null
| Modifier and Type | Class and Description |
|---|---|
private static class |
EqualsAvoidNullCheck.FieldFrame
Holds the names of fields of a type.
|
AutomaticBean.OutputStreamOptions| 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
Control whether to ignore
String.equalsIgnoreCase(String) invocations. |
private static java.lang.String |
LEFT_CURLY
Curly for comparison.
|
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 |
|---|---|
private static boolean |
astTypeIsClassOrEnumOrRecordDef(int tokenType)
Verify that a token is either CLASS_DEF, RECORD_DEF, or ENUM_DEF.
|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
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 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 void |
leaveLiteralNew(DetailAST ast)
Determine whether LITERAL_NEW is an anonymous class definition and leave
the frame it is in.
|
private void |
leaveSlist(DetailAST ast)
Determine whether SLIST begins a block, determined by braces.
|
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 a block, determined by braces, and add it as
a frame in this case.
|
void |
setIgnoreEqualsIgnoreCase(boolean newValue)
Setter to control whether to ignore
String.equalsIgnoreCase(String) invocations. |
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.
|
clearViolations, destroy, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic 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 static final java.lang.String LEFT_CURLY
private boolean ignoreEqualsIgnoreCase
String.equalsIgnoreCase(String) invocations.private EqualsAvoidNullCheck.FieldFrame currentFrame
public EqualsAvoidNullCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void setIgnoreEqualsIgnoreCase(boolean newValue)
String.equalsIgnoreCase(String) invocations.newValue - whether to ignore checking
String.equalsIgnoreCase(String).public void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingpublic void finishTree(DetailAST ast)
AbstractCheckfinishTree in class AbstractCheckast - 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 leaveLiteralNew(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 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 java.lang.String getFieldType(DetailAST field)
field - to get the type from.private static boolean astTypeIsClassOrEnumOrRecordDef(int tokenType)
tokenType - the type of tokenCopyright © 2001-2022. All Rights Reserved.