Class HiddenFieldCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
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.
-
Property
ignoreFormat
- Define the RegExp for names of variables and parameters to ignore. Type isjava.util.regex.Pattern
. Default value isnull
. -
Property
ignoreConstructorParameter
- Control whether to ignore constructor parameters. Type isboolean
. Default value isfalse
. -
Property
ignoreSetter
- Allow to ignore the parameter of a property setter method. Type isboolean
. Default value isfalse
. -
Property
setterCanReturnItsClass
- Allow to expand the definition of a setter method to include methods that return the class' instance. Type isboolean
. Default value isfalse
. -
Property
ignoreAbstractMethods
- Control whether to ignore parameters of abstract methods. Type isboolean
. Default value isfalse
. -
Property
tokens
- tokens to check Type isjava.lang.String[]
. Validation type istokenSet
. Default value is: VARIABLE_DEF, PARAMETER_DEF, PATTERN_VARIABLE_DEF, LAMBDA, RECORD_COMPONENT_DEF.
To configure the check:
<module name="HiddenField"/>
To configure the check so that it checks local variables but not parameters:
<module name="HiddenField"> <property name="tokens" value="VARIABLE_DEF"/> </module>
To configure the check so that it ignores the variables and parameters named "test":
<module name="HiddenField"> <property name="ignoreFormat" value="^test$"/> </module>
class SomeClass { private List<String> test; private void addTest(List<String> test) // no violation { this.test.addAll(test); } private void foo() { final List<String> test = new ArrayList<>(); // no violation ... } }
To configure the check so that it ignores constructor parameters:
<module name="HiddenField"> <property name="ignoreConstructorParameter" value="true"/> </module>
To configure the check so that it ignores the parameter of setter methods:
<module name="HiddenField"> <property name="ignoreSetter" value="true"/> </module>
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>
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
hidden.field
- Since:
- 3.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description HiddenFieldCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
beginTree
public void beginTree(DetailAST rootAST)
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
setIgnoreFormat
public void setIgnoreFormat(java.util.regex.Pattern pattern)
Setter to define the RegExp for names of variables and parameters to ignore.- Parameters:
pattern
- a pattern.
-
setIgnoreSetter
public void setIgnoreSetter(boolean ignoreSetter)
Setter to allow to ignore the parameter of a property setter method.- Parameters:
ignoreSetter
- decide whether to ignore the parameter of a property setter method.
-
setSetterCanReturnItsClass
public void setSetterCanReturnItsClass(boolean aSetterCanReturnItsClass)
Setter to allow to expand the definition of a setter method to include methods that return the class' instance.- Parameters:
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.
-
setIgnoreConstructorParameter
public void setIgnoreConstructorParameter(boolean ignoreConstructorParameter)
Setter to control whether to ignore constructor parameters.- Parameters:
ignoreConstructorParameter
- decide whether to ignore constructor parameters.
-
setIgnoreAbstractMethods
public void setIgnoreAbstractMethods(boolean ignoreAbstractMethods)
Setter to control whether to ignore parameters of abstract methods.- Parameters:
ignoreAbstractMethods
- decide whether to ignore parameters of abstract methods.
-
-