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:
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.
checkFields
- Control whether to check references to fields.
Default value is true
.
checkMethods
- Control whether to check references to methods.
Default value is true
.
validateOnlyOverlapping
- Control whether to check only
overlapping by variables or arguments.
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". } }
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.". } }
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
MSG_METHOD
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_VARIABLE
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
RequireThisCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
void |
setCheckFields(boolean checkFields)
Setter to control whether to check references to fields.
|
void |
setCheckMethods(boolean checkMethods)
Setter to control whether to check references to methods.
|
void |
setValidateOnlyOverlapping(boolean validateOnlyOverlapping)
Setter to control whether to check only overlapping by variables or arguments.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final java.lang.String MSG_METHOD
public static final java.lang.String MSG_VARIABLE
public RequireThisCheck()
public void setCheckFields(boolean checkFields)
checkFields
- should we check fields usage or not.public void setCheckMethods(boolean checkMethods)
checkMethods
- should we check methods usage or not.public void setValidateOnlyOverlapping(boolean validateOnlyOverlapping)
validateOnlyOverlapping
- should we check only overlapping by variables or arguments.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treepublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processpublic void leaveToken(DetailAST ast)
AbstractCheck
leaveToken
in class AbstractCheck
ast
- the token leavingCopyright © 2001-2019. All Rights Reserved.