Class 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 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 ignore String.equalsIgnoreCase(String) invocations. Type is boolean. Default value is false.

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • equals.avoid.null
    • equalsIgnoreCase.avoid.null
    Since:
    5.0
    • Method Detail

      • 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 class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • setIgnoreEqualsIgnoreCase

        public void setIgnoreEqualsIgnoreCase​(boolean newValue)
        Setter to control whether to ignore String.equalsIgnoreCase(String) invocations.
        Parameters:
        newValue - whether to ignore checking String.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 class AbstractCheck
        Parameters:
        rootAST - the root of the tree
      • 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 class AbstractCheck
        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.
      • 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.
      • 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.