Since Checkstyle 3.4
Warning: the Check is very controversial if 'validateOnlyOverlapping' option is set to 'false' and not that actual nowadays.
Rationale:
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.
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 |
To configure the default check:
<module name="Checker"> <module name="TreeWalker"> <module name="RequireThis"/> </module> </module>
Example:
public class Test { private int a; private int b; private int c; public Test(int a) { // overlapping by constructor argument this.a = a; // OK, this keyword used b = 0; // OK, no overlap foo(5); // OK } public void foo(int c) { // overlapping by method argument c = c; // violation, reference to instance variable "c" requires "this" } }
To configure the check for fields only:
<module name="Checker"> <module name="TreeWalker"> <module name="RequireThis"> <property name="checkMethods" value="false"/> </module> </module> </module>
Example:
public class Test { private int a; private int b; private int c; public Test(int a) { // overlapping by constructor argument this.a = a; // OK, this keyword used b = 0; // OK, no overlap foo(5); // OK, no validation for methods } public void foo(int c) { // overlapping by method argument c = c; // violation, reference to instance variable "c" requires "this" } }
To configure the check for methods only:
<module name="Checker"> <module name="TreeWalker"> <module name="RequireThis"> <property name="checkFields" value="false"/> </module> </module> </module>
Example:
public class Test { private int a; private int b; private int c; public Test(int a) { // overlapping by constructor argument this.a = a; // OK, no validation for fields b = 0; // OK, no validation for fields foo(5); // OK, no overlap } public void foo(int c) { // overlapping by method argument c = c; // OK, no validation for fields } }
Note that method call foo(5) does not raise a violation because methods cannot be overlapped in java.
To configure the check to validate for non-overlapping fields and methods:
<module name="Checker"> <module name="TreeWalker"> <module name="RequireThis"> <property name="validateOnlyOverlapping" value="false"/> </module> </module> </module>
Example:
public class Test { private int a; private int b; private int c; public Test(int a) { // overlapping by constructor argument this.a = a; // OK, no validation for fields b = 0; // violation, reference to instance variable "b" requires "this" foo(5); // violation, method call "foo(5)" requires "this" } public void foo(int c) { // overlapping by method argument c = c; // violation, reference to instance variable "c" requires "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, because method parameter is returned return prefix; } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding