Class RequireThisCheck

All Implemented Interfaces:
Configurable, Contextualizable

public class RequireThisCheck extends AbstractCheck

Checks that references to instance variables and methods of the present object are explicitly of the form "this.varName" or "this.methodName(args)" and that those references don't rely on the default behavior when "this." is absent.

Warning: the Check is very controversial if 'validateOnlyOverlapping' option is set to 'false' and not that actual nowadays.

Rationale:

  1. The same notation/habit for C++ and Java (C++ have global methods, so having "this." do make sense in it to distinguish call of method of class instead of global).
  2. Non-IDE development (ease of refactoring, some clearness to distinguish static and non-static methods).

Limitations: Nothing is currently done about static variables or catch-blocks. Static methods invoked on a class name seem to be OK; both the class name and the method name have a DOT parent. Non-static methods invoked on either this or a variable name seem to be OK, likewise.

  • Property checkFields - Control whether to check references to fields. Type is boolean. Default value is true.
  • Property checkMethods - Control whether to check references to methods. Type is boolean. Default value is true.
  • Property validateOnlyOverlapping - Control whether to check only overlapping by variables or arguments. Type is boolean. Default value is true.

To configure the default check:

 <module name="RequireThis"/>
 

To configure to check the this qualifier for fields only:

 <module name="RequireThis">
   <property name="checkMethods" value="false"/>
 </module>
 

Examples of how the check works if validateOnlyOverlapping option is set to true:

 public static class A {
   private int field1;
   private int field2;

   public A(int field1) {
     // Overlapping by constructor argument.
     field1 = field1; // violation: Reference to instance variable "field1" needs "this".
     field2 = 0;
   }

   void foo3() {
     String field1 = "values";
     // Overlapping by local variable.
     field1 = field1; // violation:  Reference to instance variable "field1" needs "this".
   }
 }

 public static class B {
   private int field;

   public A(int f) {
     field = f;
   }

   String addSuffixToField(String field) {
     // Overlapping by method argument. Equal to "return field = field + "suffix";"
     return field += "suffix"; // violation: Reference to instance variable "field" needs "this".
   }
 }

 public static record C(int x) {
     void getTwoX(int x) {
         // Overlapping by method argument.
         return x += x; // violation: Reference to instance variable "x" needs "this".
     }
 }
 

Please, be aware of the following logic, which is implemented in the check:

1) If you arrange 'this' in your code on your own, the check will not raise violation for variables which use 'this' to reference a class field, for example:

 public class C {
   private int scale;
   private int x;
   public void foo(int scale) {
     scale = this.scale; // no violation
     if (scale > 0) {
       scale = -scale; // no violation
     }
     x *= scale;
   }
 }
 

2) If method parameter is returned from the method, the check will not raise violation for returned variable/parameter, for example:

 public class D {
   private String prefix;
   public String modifyPrefix(String prefix) {
     prefix = "^" + prefix + "$" // no violation (modification of parameter)
     return prefix; // modified method parameter is returned from the method
   }
 }
 

Examples of how the check works if validateOnlyOverlapping option is set to false:

 public static class A {
   private int field1;
   private int field2;

   public A(int field1) {
     field1 = field1; // violation: Reference to instance variable "field1" needs "this".
     field2 = 0; // violation: Reference to instance variable "field2" needs "this".
     String field2;
     field2 = "0"; // No violation. Local var allowed
   }

   void foo3() {
     String field1 = "values";
     field1 = field1; // violation:  Reference to instance variable "field1" needs "this".
   }
 }

 public static class B {
   private int field;

   public A(int f) {
     field = f; // violation:  Reference to instance variable "field" needs "this".
   }

   String addSuffixToField(String field) {
     return field += "suffix"; // violation: Reference to instance variable "field" needs "this".
   }
 }

 // If the variable is locally defined, there won't be a violation provided the variable
 // doesn't overlap.
 class C {
   private String s1 = "foo1";
   String s2 = "foo2";

   C() {
     s1 = "bar1"; // Violation. Reference to instance variable 's1' needs "this.".
     String s2;
     s2 = "bar2"; // No violation. Local var allowed.
     s2 += s2; // Violation. Overlapping. Reference to instance variable 's2' needs "this.".
   }
 }

 public static record D(int x) {
     public D {
         x = x; // violation: Reference to instance variable "x" needs "this".
     }
 }
 

Parent is com.puppycrawl.tools.checkstyle.TreeWalker

Violation Message Keys:

  • require.this.method
  • require.this.variable
Since:
3.4
  • Field Details

    • MSG_METHOD

      public static final String MSG_METHOD
      A key is pointing to the warning message text in "messages.properties" file.
      See Also:
    • MSG_VARIABLE

      public static final String MSG_VARIABLE
      A key is pointing to the warning message text in "messages.properties" file.
      See Also:
  • Constructor Details

    • RequireThisCheck

      public RequireThisCheck()
  • Method Details

    • setCheckFields

      public void setCheckFields(boolean checkFields)
      Setter to control whether to check references to fields.
      Parameters:
      checkFields - should we check fields usage or not.
    • setCheckMethods

      public void setCheckMethods(boolean checkMethods)
      Setter to control whether to check references to methods.
      Parameters:
      checkMethods - should we check methods usage or not.
    • setValidateOnlyOverlapping

      public void setValidateOnlyOverlapping(boolean validateOnlyOverlapping)
      Setter to control whether to check only overlapping by variables or arguments.
      Parameters:
      validateOnlyOverlapping - should we check only overlapping by variables or arguments.
    • 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:
    • 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:
    • 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