HiddenField
Since Checkstyle 3.0
Description
Notes
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.
Properties
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 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="HiddenField"/>
</module>
</module>
Example:
class Example1 {
private String field;
private String testField;
Example1(String testField) { // violation, 'field' hides a field
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // violation, ''testField' hides a field'
this.field = field;
}
Example1 setField(String field) { // violation, ''field' hides a field'
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // violation, ''field' hides a 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:
class Example2 {
private String field;
private String testField;
Example2(String testField) { // OK, because PARAMETER_DEF not configured
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // OK, because PARAMETER_DEF not configured
this.field = field;
}
Example2 setField(String field) { // OK, because PARAMETER_DEF not configured
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // OK, because PARAMETER_DEF not configured
}
}
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:
class Example3 {
private String field;
private String testField;
Example3(String testField) { // OK, because it match ignoreFormat
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // OK, because it match ignoreFormat
this.field = field;
}
Example3 setField(String field) { // violation, ''field' hides a field'
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // violation, ''field' hides a 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:
class Example4 {
private String field;
private String testField;
Example4(String testField) { // OK, because ignoreConstructorParameter is true
}
void method(String param) {
String field = param; // violation, ''field' hides a field' field
}
void setTestField(String testField) { // violation, ''testField' hides a field'
this.field = field;
}
Example4 setField(String field) { // violation, ''field' hides a field'
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // violation, ''field' hides a 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:
class Example5 {
private String field;
private String testField;
Example5(String testField) { // violation, ''testField' hides a field'
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // OK, because ignoreSetter is true
this.field = field;
}
Example5 setField(String field) { // violation, ''field' hides a field'
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // violation, ''field' hides a 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:
class Example6 {
private String field;
private String testField;
Example6(String testField) { // violation, ''testField' hides a field'
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // OK, because ignoreSetter is true
this.field = field;
}
Example6 setField(String field) { // OK, because setterCanReturnItsClass is true
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // violation, ''field' hides a 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:
class Example7 {
private String field;
private String testField;
Example7(int field) { // violation, ''field' hides a field'
this.field = Integer.toString(field);
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // violation, 'testField' hides a field'
this.field = field;
}
Example7 setField(String field) { // violation, ''field' hides a field'
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // OK, because ignoreAbstractMethods is true
}
}
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