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
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 assomeString.equals(anotherString = "text")
).Rationale: Calling the
equals()
method on String literals will avoid a potentialNullPointerException
. 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
Nested Classes Modifier and Type Class Description private static class
EqualsAvoidNullCheck.FieldFrame
Holds the names of fields of a type.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private EqualsAvoidNullCheck.FieldFrame
currentFrame
Stack of sets of field names, one for each class of a set of nested classes.private static String
EQUALS
Method name for comparison.private boolean
ignoreEqualsIgnoreCase
Control whether to ignoreString.equalsIgnoreCase(String)
invocations.private static String
LEFT_CURLY
Curly for comparison.static String
MSG_EQUALS_AVOID_NULL
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_EQUALS_IGNORE_CASE_AVOID_NULL
A key is pointing to the warning message text in "messages.properties" file.private static String
STRING
Type name for comparison.
-
Constructor Summary
Constructors Constructor Description EqualsAvoidNullCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 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, 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 ignoreString.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.-
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 Detail
-
MSG_EQUALS_AVOID_NULL
public static final String MSG_EQUALS_AVOID_NULL
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_EQUALS_IGNORE_CASE_AVOID_NULL
public static final String MSG_EQUALS_IGNORE_CASE_AVOID_NULL
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
EQUALS
private static final String EQUALS
Method name for comparison.- See Also:
- Constant Field Values
-
STRING
private static final String STRING
Type name for comparison.- See Also:
- Constant Field Values
-
LEFT_CURLY
private static final String LEFT_CURLY
Curly for comparison.- See Also:
- Constant Field Values
-
ignoreEqualsIgnoreCase
private boolean ignoreEqualsIgnoreCase
Control whether to ignoreString.equalsIgnoreCase(String)
invocations.
-
currentFrame
private EqualsAvoidNullCheck.FieldFrame currentFrame
Stack of sets of field names, one for each class of a set of nested classes.
-
-
Constructor Detail
-
EqualsAvoidNullCheck
public EqualsAvoidNullCheck()
-
-
Method Detail
-
getDefaultTokens
public int[] 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:
TokenTypes
-
getAcceptableTokens
public int[] 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:
TokenTypes
-
getRequiredTokens
public int[] 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:
TokenTypes
-
setIgnoreEqualsIgnoreCase
public void setIgnoreEqualsIgnoreCase(boolean newValue)
Setter to control whether to ignoreString.equalsIgnoreCase(String)
invocations.- Parameters:
newValue
- whether to ignore checkingString.equalsIgnoreCase(String)
.- Since:
- 5.4
-
beginTree
public void beginTree(DetailAST rootAST)
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
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
finishTree
public void finishTree(DetailAST ast)
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
private void processSlist(DetailAST ast)
Determine whether SLIST begins a block, determined by braces, and add it as a frame in this case.- Parameters:
ast
- SLIST ast.
-
leaveSlist
private void leaveSlist(DetailAST ast)
Determine whether SLIST begins a block, determined by braces.- Parameters:
ast
- SLIST ast.
-
processFrame
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.- Parameters:
ast
- processed ast.
-
processMethodCall
private void processMethodCall(DetailAST methodCall)
Add the method call to the current frame if it should be processed.- Parameters:
methodCall
- METHOD_CALL ast.
-
processLiteralNew
private void processLiteralNew(DetailAST ast)
Determine whether LITERAL_NEW is an anonymous class definition and add it as a frame in this case.- Parameters:
ast
- LITERAL_NEW ast.
-
leaveLiteralNew
private void leaveLiteralNew(DetailAST ast)
Determine whether LITERAL_NEW is an anonymous class definition and leave the frame it is in.- Parameters:
ast
- LITERAL_NEW ast.
-
traverseFieldFrameTree
private void traverseFieldFrameTree(EqualsAvoidNullCheck.FieldFrame frame)
Traverse the tree of the field frames to check all equals method calls.- Parameters:
frame
- to check method calls in.
-
checkMethodCall
private void checkMethodCall(DetailAST methodCall)
Check whether the method call should be violated.- Parameters:
methodCall
- method call to check.
-
containsOneArgument
private static boolean containsOneArgument(DetailAST methodCall)
Verify that method call has one argument.- Parameters:
methodCall
- METHOD_CALL DetailAST- Returns:
- true if method call has one argument.
-
containsAllSafeTokens
private static boolean containsAllSafeTokens(DetailAST expr)
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
private static DetailAST skipVariableAssign(DetailAST currentAST)
Skips over an inner assign portion of an argument expression.- Parameters:
currentAST
- current token in the argument expression- Returns:
- the next relevant token
-
isCalledOnStringFieldOrVariable
private boolean isCalledOnStringFieldOrVariable(DetailAST objCalledOn)
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
private boolean isStringFieldOrVariable(DetailAST objCalledOn)
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
private boolean isStringFieldOrVariableFromThisInstance(DetailAST objCalledOn)
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
private boolean isStringFieldOrVariableFromClass(DetailAST objCalledOn, String className)
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
private static String getFieldType(DetailAST field)
Get field type.- Parameters:
field
- to get the type from.- Returns:
- type of the field.
-
astTypeIsClassOrEnumOrRecordDef
private static boolean astTypeIsClassOrEnumOrRecordDef(int tokenType)
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.
-
-