RequireThis

Since Checkstyle 3.4

Description

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).

Notes

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.

Properties

name description type default value since
checkFields Control whether to check references to fields. boolean true 3.4
checkMethods Control whether to check references to methods. boolean true 3.4
validateOnlyOverlapping Control whether to check only overlapping by variables or arguments. boolean true 6.17

Examples

To configure the default check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="RequireThis"/>
  </module>
</module>
        

Example:

class Example1 {
  int field1,field2,field3;

  Example1(int field1) {
    this.field1 = field1;
    field2 = 0;
    foo(5); // OK, methods cannot be overlapped in java.
  }

  void method2(int i) {
    foo(i); // OK, methods cannot be overlapped in java.
  }

  void foo(int field3) {
    // violation below, reference to instance variable "field3" requires "this"
    field3 = field3;
  }
}
        

Note that method call foo(5) does not raise a violation because methods cannot be overlapped in java.

To configure the check demand methods and fields to have 'this.'

<module name="Checker">
  <module name="TreeWalker">
    <module name="RequireThis">
      <property name="validateOnlyOverlapping" value="false"/>
    </module>
  </module>
</module>
        

Example:

class Example4 {
  int field1,field2,field3;

  Example4(int field1) {
    this.field1 = field1;
    field2 = 0; // violation, reference to instance variable "field2" requires "this"
    foo(5); // violation, method call "foo(5)" requires "this"
  }

  void method2(int i) {
    foo(i); // violation, 'Method call to 'foo' needs "this.".'
  }

  void foo(int field3) {
    // violation below, reference to instance variable "field3" requires "this"
    field3 = field3;
  }
}
        

To configure the check demand fields only to have 'this.'

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

Example:

class Example2 {
  int field1,field2,field3;

  Example2(int field1) {
    this.field1 = field1;
    field2 = 0; // violation, reference to instance variable "field2" requires "this"
    foo(5); // OK, checkMethods is false
  }

  void method2(int i) {
    foo(i); // OK, checkMethods is false
  }

  void foo(int field3) {
    // violation below, reference to instance variable "field3" requires "this"
    field3 = field3;
  }
}
        

To configure the check demand methods only to have 'this.'

<module name="Checker">
  <module name="TreeWalker">
    <module name="RequireThis">
      <property name="validateOnlyOverlapping" value="false"/>
      <property name="checkFields" value="false"/>
    </module>
  </module>
</module>
        

Example:

class Example3 {
  int field1,field2,field3;

  Example3(int field1) {
    this.field1 = field1;
    field2 = 0;
    foo(5); // violation, method call "foo(5)" requires "this"
  }

  void method2(int i) {
    foo(i); // violation, 'Method call to 'foo' needs "this.".'
  }

  void foo(int field3) {

    field3 = field3;
  }
}
        

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:

class Example5 {
  int field1,field2;

  public void foo(int field1) {
    field1 = this.field1;

    if (field1 > 0) {
      field1 = -field1;
    }
    // violation below, reference to instance variable "field2" requires "this"
    field2 *= field1;
  }
}
        

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

class Example6 {
  String prefix;

  String modifyPrefix(String prefix) {
    prefix = "^" + prefix + "$";  //OK, because method parameter is returned
    return prefix;
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker