public class HiddenFieldCheck extends AbstractCheck
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.
ignoreFormat - Define the RegExp for names of variables
and parameters to ignore.
Type is java.util.regex.Pattern.
Default value is null.
ignoreConstructorParameter - Control whether to ignore constructor parameters.
Type is boolean.
Default value is false.
ignoreSetter - Allow to ignore the parameter of a property setter method.
Type is boolean.
Default value is false.
setterCanReturnItsClass - Allow to expand the definition of a setter method
to include methods that return the class' instance.
Type is boolean.
Default value is false.
ignoreAbstractMethods - Control whether to ignore parameters
of abstract methods.
Type is boolean.
Default value is false.
tokens - tokens to check
Type is java.lang.String[].
Validation type is tokenSet.
Default value is:
VARIABLE_DEF,
PARAMETER_DEF,
PATTERN_VARIABLE_DEF,
LAMBDA,
RECORD_COMPONENT_DEF.
To configure the check:
<module name="HiddenField"/>
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="HiddenField"> <property name="tokens" value="VARIABLE_DEF"/> </module>
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="HiddenField"> <property name="ignoreFormat" value="^testField"/> </module>
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="HiddenField"> <property name="ignoreConstructorParameter" value="true"/> </module>
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="HiddenField"> <property name="ignoreSetter" value="true"/> </module>
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="HiddenField"> <property name="ignoreSetter" value="true"/> <property name="setterCanReturnItsClass" value="true"/> </module>
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="HiddenField"> <property name="ignoreAbstractMethods" value="true"/> </module>
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;
}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
hidden.field
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
HiddenFieldCheck() |
| 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 |
setIgnoreAbstractMethods(boolean ignoreAbstractMethods)
Setter to control whether to ignore parameters of abstract methods.
|
void |
setIgnoreConstructorParameter(boolean ignoreConstructorParameter)
Setter to control whether to ignore constructor parameters.
|
void |
setIgnoreFormat(java.util.regex.Pattern pattern)
Setter to define the RegExp for names of variables and parameters to ignore.
|
void |
setIgnoreSetter(boolean ignoreSetter)
Setter to allow to ignore the parameter of a property setter method.
|
void |
setSetterCanReturnItsClass(boolean aSetterCanReturnItsClass)
Setter to allow to expand the definition of a setter method to include methods
that return the class' instance.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearViolations, destroy, finishTree, getFileContents, getLine, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
public HiddenFieldCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingpublic void setIgnoreFormat(java.util.regex.Pattern pattern)
pattern - a pattern.public void setIgnoreSetter(boolean ignoreSetter)
ignoreSetter - decide whether to ignore the parameter of
a property setter method.public void setSetterCanReturnItsClass(boolean aSetterCanReturnItsClass)
aSetterCanReturnItsClass - if true then setter can return
either void or class in which it is declared. If false then
in order to be recognized as setter method (otherwise
already recognized as a setter) must return void. Later is
the default behavior.public void setIgnoreConstructorParameter(boolean ignoreConstructorParameter)
ignoreConstructorParameter - decide whether to ignore
constructor parameters.public void setIgnoreAbstractMethods(boolean ignoreAbstractMethods)
ignoreAbstractMethods - decide whether to ignore
parameters of abstract methods.Copyright © 2001-2021. All Rights Reserved.