Since Checkstyle 3.0
Checks that a local variable or a parameter does not shadow a field that is defined in the same class.
It is possible to configure the check to ignore all property setter methods.
A method is recognized as a setter if it is in the following form
${returnType} set${Name}(${anyType} ${name}) { ... }
where ${anyType} is any primitive type, class or interface name; ${name} is name of the variable that is being set and ${Name} its capitalized form that appears in the method name. By default, it is expected that setter returns void, i.e. ${returnType} is 'void'. For example
void setTime(long time) { ... }
Any other return types will not let method match a setter pattern. However, by setting setterCanReturnItsClass property to true definition of a setter is expanded, so that setter return type can also be a class in which setter is declared. For example
class PageBuilder { PageBuilder setName(String name) { ... } }
Such methods are known as chain-setters and a common when Builder-pattern is used. Property setterCanReturnItsClass has effect only if ignoreSetter is set to true.
name | description | type | default value | since |
---|---|---|---|---|
ignoreAbstractMethods | Control whether to ignore parameters of abstract methods. | boolean | false |
4.0 |
ignoreConstructorParameter | Control whether to ignore constructor parameters. | boolean | false |
3.2 |
ignoreFormat | Define the RegExp for names of variables and parameters to ignore. | Pattern | null |
3.2 |
ignoreSetter | Allow to ignore the parameter of a property setter method. | boolean | false |
3.2 |
setterCanReturnItsClass | Allow to expand the definition of a setter method to include methods that return the class' instance. | boolean | false |
6.3 |
tokens | tokens to check | subset of tokens VARIABLE_DEF , PARAMETER_DEF , PATTERN_VARIABLE_DEF , LAMBDA , RECORD_COMPONENT_DEF . | VARIABLE_DEF , PARAMETER_DEF , PATTERN_VARIABLE_DEF , LAMBDA , RECORD_COMPONENT_DEF . | 3.0 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="HiddenField"/> </module> </module>
Example:
public class SomeClass { private String field; private String testField; public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field } public void method(String param) { // OK String field = param; // violation, 'field' variable hides 'field' field } public void setTestField(String testField) { // violation, 'testField' param // hides 'testField' field this.field = field; } public SomeClass setField(String field) { // violation, 'field' param hides 'field' field this.field = field; } }
To configure the check so that it checks local variables but not parameters:
<module name="Checker"> <module name="TreeWalker"> <module name="HiddenField"> <property name="tokens" value="VARIABLE_DEF"/> </module> </module> </module>
Example:
public class SomeClass { private String field; private String testField; public SomeClass(String testField) { // OK, 'testField' param doesn't hide any field } public void method(String param) { // OK String field = param; // violation, 'field' variable hides 'field' field } public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field this.field = field; } public SomeClass setField(String field) { // OK, 'field' param doesn't hide any field this.field = field; } }
To configure the check so that it ignores the variables and parameters named "test":
<module name="Checker"> <module name="TreeWalker"> <module name="HiddenField"> <property name="ignoreFormat" value="^testField"/> </module> </module> </module>
Example:
public class SomeClass { private String field; private String testField; public SomeClass(String testField) { // OK, because it match ignoreFormat } public void method(String param) { // OK String field = param; // violation, 'field' variable hides 'field' field } public void setTestField(String testField) { // OK, because it match ignoreFormat this.field = field; } public SomeClass setField(String field) { // violation, 'field' param hides 'field' field this.field = field; } }
To configure the check so that it ignores constructor parameters:
<module name="Checker"> <module name="TreeWalker"> <module name="HiddenField"> <property name="ignoreConstructorParameter" value="true"/> </module> </module> </module>
Example:
public class SomeClass { private String field; private String testField; public SomeClass(String testField) { // OK, 'testField' param doesn't hide any field } public void method(String param) { // OK String field = param; // violation, 'field' variable hides 'field' field } public void setTestField(String testField) { // violation, 'testField' variable // hides 'testField' field this.field = field; } public SomeClass setField(String field) { // violation, 'field' param hides 'field' field this.field = field; } }
To configure the check so that it ignores the parameter of setter methods:
<module name="Checker"> <module name="TreeWalker"> <module name="HiddenField"> <property name="ignoreSetter" value="true"/> </module> </module> </module>
Example:
public class SomeClass { private String field; private String testField; public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field } public void method(String param) { // OK String field = param; // violation, 'field' variable hides 'field' field } public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field this.field = field; } public SomeClass setField(String field) { // violation, 'field' param hides 'field' field this.field = field; } }
To configure the check so that it ignores the parameter of setter
methods recognizing setter as returning either void
or
a class in which it is declared:
<module name="Checker"> <module name="TreeWalker"> <module name="HiddenField"> <property name="ignoreSetter" value="true"/> <property name="setterCanReturnItsClass" value="true"/> </module> </module> </module>
Example:
public class SomeClass { private String field; private String testField; public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field } public void method(String param) { // OK String field = param; // violation, 'field' variable hides 'field' field } public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field this.field = field; } public SomeClass setField(String field) { // OK, 'field' param doesn't hide any field this.field = field; } }
To configure the check so that it ignores parameters of abstract methods:
<module name="Checker"> <module name="TreeWalker"> <module name="HiddenField"> <property name="ignoreAbstractMethods" value="true"/> </module> </module> </module>
Example:
abstract class SomeClass { private String field; public SomeClass(int field) { // violation, 'field' param hides a 'field' field float field; // violation, 'field' variable hides a 'field' field } public abstract int method(String field); // OK } public class Demo extends SomeClass { public int method(String param){ return param; } }
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