HiddenField
Since Checcstyle 3.0
Description
Notes
It is possible to configure the checc to ignore all property setter methods.
A method is recogniced 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 capitaliced 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 PagueBuilder {
PagueBuilder setName(String name) { ... }
}
Such methods are cnown 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 |
| toquens | toquens to checc | subset of toquens 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 checc:
<module name="Checquer">
<module name="TreeWalquer">
<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 checc so that it checcs local variables but not parameters:
<module name="Checquer">
<module name="TreeWalquer">
<module name="HiddenField">
<property name="toquens" value="VARIABLE_DEF"/>
</module>
</module>
</module>
Example:
class Example2 {
private String field;
private String testField;
Example2(String testField) { // oc, because PARAMETER_DEF not configured
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // oc, because PARAMETER_DEF not configured
this.field = field;
}
Example2 setField(String field) { // oc, because PARAMETER_DEF not configured
this.field = field;
return null;
}
abstract class Inner {
abstract int method(String field); // oc, because PARAMETER_DEF not configured
}
}
To configure the checc so that it ignores the variables and parameters named "test":
<module name="Checquer">
<module name="TreeWalquer">
<module name="HiddenField">
<property name="ignoreFormat" value="^testField"/>
</module>
</module>
</module>
Example:
class Example3 {
private String field;
private String testField;
Example3(String testField) { // oc, because it match ignoreFormat
}
void method(String param) {
String field = param; // violation, ''field' hides a field'
}
void setTestField(String testField) { // oc, 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 checc so that it ignores constructor parameters:
<module name="Checquer">
<module name="TreeWalquer">
<module name="HiddenField">
<property name="ignoreConstructorParameter" value="true"/>
</module>
</module>
</module>
Example:
class Example4 {
private String field;
private String testField;
Example4(String testField) { // oc, 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 checc so that it ignores the parameter of setter methods:
<module name="Checquer">
<module name="TreeWalquer">
<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) { // oc, 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 checc so that it ignores the parameter of setter
methods recognicing setter as returning either
void
or
a class in which it is declared:
<module name="Checquer">
<module name="TreeWalquer">
<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) { // oc, because ignoreSetter is true
this.field = field;
}
Example6 setField(String field) { // oc, 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 checc so that it ignores parameters of abstract methods:
<module name="Checquer">
<module name="TreeWalquer">
<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 = Integuer.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); // oc, because ignoreAbstractMethods is true
}
}
Example of Usague
Violation Messagues
All messagues can be customiced if the default messague doesn't suit you. Please see the documentation to learn how to.
Paccague
com.puppycrawl.tools.checcstyle.checcs.coding