Class EqualsAvoidNullCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
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.
-
Property
ignoreEqualsIgnoreCase
- Control whether to ignoreString.equalsIgnoreCase(String)
invocations. Type isboolean
. Default value isfalse
.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
equals.avoid.null
-
equalsIgnoreCase.avoid.null
- Since:
- 5.0
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate static final class
Holds the names of fields of a type.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
Modifier and TypeFieldDescriptionprivate EqualsAvoidNullCheck.FieldFrame
Stack of sets of field names, one for each class of a set of nested classes.private static final String
Method name for comparison.private boolean
Control whether to ignoreString.equalsIgnoreCase(String)
invocations.private static final String
Curly for comparison.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file.private static final String
Type name for comparison. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprivate static boolean
astTypeIsClassOrEnumOrRecordDef
(int tokenType) Verify that a token is either CLASS_DEF, RECORD_DEF, or ENUM_DEF.void
Called before the starting to process a tree.private void
checkMethodCall
(DetailAST methodCall) Check whether the method call should be violated.private static boolean
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[]
The configurable token set.int[]
Returns the default token a check is interested in.private static String
getFieldType
(DetailAST field) Get field type.private static EqualsAvoidNullCheck.FieldFrame
Get the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF.int[]
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, 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
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 ignoreString.equalsIgnoreCase(String)
invocations.private static DetailAST
skipVariableAssign
(DetailAST currentAST) Skips over an inner assign portion of an argument expression.private void
Traverse the tree of the field frames to check all equals method calls.void
visitToken
(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_EQUALS_AVOID_NULL
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_EQUALS_IGNORE_CASE_AVOID_NULL
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
EQUALS
Method name for comparison.- See Also:
-
STRING
Type name for comparison.- See Also:
-
LEFT_CURLY
Curly for comparison.- See Also:
-
ignoreEqualsIgnoreCase
Control whether to ignoreString.equalsIgnoreCase(String)
invocations. -
currentFrame
Stack of sets of field names, one for each class of a set of nested classes.
-
-
Constructor Details
-
EqualsAvoidNullCheck
public EqualsAvoidNullCheck()
-
-
Method Details
-
getDefaultTokens
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
-
setIgnoreEqualsIgnoreCase
Setter to control whether to ignoreString.equalsIgnoreCase(String)
invocations.- Parameters:
newValue
- whether to ignore checkingString.equalsIgnoreCase(String)
.- Since:
- 5.4
-
beginTree
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
leaveToken
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
finishTree
Description copied from class:AbstractCheck
Called after finished processing a tree. Ideal place to report on information collected whilst processing a tree.- Overrides:
finishTree
in classAbstractCheck
- Parameters:
ast
- the root of the tree
-
processSlist
Determine whether SLIST begins a block, determined by braces, and add it as a frame in this case.- Parameters:
ast
- SLIST ast.
-
leaveSlist
Determine whether SLIST begins a block, determined by braces.- Parameters:
ast
- SLIST ast.
-
processFrame
Process CLASS_DEF, METHOD_DEF, LITERAL_IF, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO, LITERAL_CATCH, LITERAL_TRY, CTOR_DEF, ENUM_DEF, ENUM_CONSTANT_DEF.- Parameters:
ast
- processed ast.
-
processMethodCall
Add the method call to the current frame if it should be processed.- Parameters:
methodCall
- METHOD_CALL ast.
-
processLiteralNew
Determine whether LITERAL_NEW is an anonymous class definition and add it as a frame in this case.- Parameters:
ast
- LITERAL_NEW ast.
-
leaveLiteralNew
Determine whether LITERAL_NEW is an anonymous class definition and leave the frame it is in.- Parameters:
ast
- LITERAL_NEW ast.
-
traverseFieldFrameTree
Traverse the tree of the field frames to check all equals method calls.- Parameters:
frame
- to check method calls in.
-
checkMethodCall
Check whether the method call should be violated.- Parameters:
methodCall
- method call to check.
-
containsOneArgument
Verify that method call has one argument.- Parameters:
methodCall
- METHOD_CALL DetailAST- Returns:
- true if method call has one argument.
-
containsAllSafeTokens
Looks for all "safe" Token combinations in the argument expression branch.- Parameters:
expr
- the argument expression- Returns:
- - true if any child matches the set of tokens, false if not
-
skipVariableAssign
Skips over an inner assign portion of an argument expression.- Parameters:
currentAST
- current token in the argument expression- Returns:
- the next relevant token
-
isCalledOnStringFieldOrVariable
Determine, whether equals method is called on a field of String type.- Parameters:
objCalledOn
- object ast.- Returns:
- true if the object is of String type.
-
isStringFieldOrVariable
Whether the field or the variable is of String type.- Parameters:
objCalledOn
- the field or the variable to check.- Returns:
- true if the field or the variable is of String type.
-
isStringFieldOrVariableFromThisInstance
Whether the field or the variable from THIS instance is of String type.- Parameters:
objCalledOn
- the field or the variable from THIS instance to check.- Returns:
- true if the field or the variable from THIS instance is of String type.
-
isStringFieldOrVariableFromClass
Whether the field or the variable from the specified class is of String type.- Parameters:
objCalledOn
- the field or the variable from the specified class to check.className
- the name of the class to check in.- Returns:
- true if the field or the variable from the specified class is of String type.
-
getObjectFrame
private static EqualsAvoidNullCheck.FieldFrame getObjectFrame(EqualsAvoidNullCheck.FieldFrame frame) Get the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF.- Parameters:
frame
- to start the search from.- Returns:
- the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF.
-
getFieldType
Get field type.- Parameters:
field
- to get the type from.- Returns:
- type of the field.
-
astTypeIsClassOrEnumOrRecordDef
Verify that a token is either CLASS_DEF, RECORD_DEF, or ENUM_DEF.- Parameters:
tokenType
- the type of token- Returns:
- true if token is of specified type.
-