Class RedundantThisCheck

All Implemented Interfaces:
Configurable, Contextualizable

public class RedundantThisCheck extends AbstractCheck
Checks that references to instance variables and methods of the current object avoid unnecessary use of this, unless it is required to resolve ambiguity with a shadowed field.

By default, a redundant this is tolerated when the same statement, or the statement immediately before or after it, contains a this that is required to resolve a shadowed field. This keeps groups of assignments consistent, for example constructor bodies where only some parameters shadow fields. Set allowAdjacentToRequiredThis to false to report every redundant this.

Since:
14.4.0
  • Field Details

  • Constructor Details

    • RedundantThisCheck

      Creates a new RedundantThisCheck instance.
  • Method Details

    • setCheckMethods

      public void setCheckMethods(boolean checkMethods)
      Setter to check whether to check redundant "this" with method call.
      Parameters:
      checkMethods - should we check method call
      Since:
      14.4.0
    • setAllowAdjacentToRequiredThis

      public void setAllowAdjacentToRequiredThis(boolean allowAdjacentToRequiredThis)
      Setter to allow redundant "this" when the same statement, or the statement immediately before or after it, contains a required "this".
      Parameters:
      allowAdjacentToRequiredThis - whether to allow such usages
      Since:
      14.4.0
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • 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 class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • 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:
    • 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
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      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 class AbstractCheck
      Parameters:
      ast - the token leaving
    • finishTree

      public void finishTree(DetailAST rootAST)
      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:
      rootAST - the root of the tree
    • checkUnnecessaryThis

      private void checkUnnecessaryThis(DetailAST literalThis)
      Classifies the use of "this" as required (shadowed) or redundant. Required usages are recorded by their enclosing statement; redundant usages are deferred so that finishTree can decide whether to report them.
      Parameters:
      literalThis - the LITERAL_THIS token to check
    • findEnclosingStatement

      Finds the statement (a direct child of an SLIST) enclosing the given node.
      Parameters:
      node - the node
      Returns:
      the statement, or null if the node is not inside a block (for example, a field initializer)
    • isNearRequiredThis

      private boolean isNearRequiredThis(DetailAST statement)
      Checks whether the given statement, or the statement immediately before or after it, contains a required this.
      Parameters:
      statement - the statement to check, may be null
      Returns:
      true if a required this is in the same or an adjacent statement
    • previousStatement

      private static DetailAST previousStatement(DetailAST statement)
      Gets the statement before the given one, stepping over the SEMI that terminates the previous statement.
      Parameters:
      statement - the statement
      Returns:
      the previous statement, or null if there is none
    • nextStatement

      private static DetailAST nextStatement(DetailAST statement)
      Gets the statement after the given one, stepping over the SEMI that terminates the given statement.
      Parameters:
      statement - the statement
      Returns:
      the next statement, or null if there is none
    • isShadowedByLocalName

      private boolean isShadowedByLocalName(String name)
      Checks whether the given variable name is shadowed by any parameter or local variable currently in scope, meaning that this.name is necessary to distinguish the field from the shadowing name.
      Parameters:
      name - the variable name following "this"
      Returns:
      true if a shadowing name exists in any enclosing scope
    • addParametersToScope

      private void addParametersToScope(DetailAST parametersNode)
      Adds all parameters found within parametersNode to the current (top-of-stack) scope. parametersNode may be either a PARAMETERS node (for methods and constructors) or a LITERAL_CATCH node (for catch clauses); both carry their parameter definitions as direct PARAMETER_DEF children.
      Parameters:
      parametersNode - the node whose PARAMETER_DEF children should be added to the current scope