public class HiddenFieldCheck extends AbstractCheck
An example of how to configure the check is:
<module name="HiddenField"/>
An example of how to configure the check so that it checks variables but not parameters is:
<module name="HiddenField"> <property name="tokens" value="VARIABLE_DEF"/> </module>
An example of how to configure the check so that it ignores the parameter of a setter method is:
<module name="HiddenField"> <property name="ignoreSetter" value="true"/> </module>
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.
An example of how to configure the check so that it ignores the parameter of either a setter that returns void or a chain-setter.
<module name="HiddenField"> <property name="ignoreSetter" value="true"/> <property name="setterCanReturnItsClass" value="true"/> </module>
An example of how to configure the check so that it ignores constructor parameters is:
<module name="HiddenField"> <property name="ignoreConstructorParameter" value="true"/> </module>
An example of how to configure the check so that it ignores 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
...
}
}
Modifier and Type | Class and Description |
---|---|
private static class |
HiddenFieldCheck.FieldFrame
Holds the names of static and instance fields of a type.
|
Modifier and Type | Field and Description |
---|---|
private HiddenFieldCheck.FieldFrame |
frame
Stack of sets of field names,
one for each class of a set of nested classes.
|
private boolean |
ignoreAbstractMethods
Controls whether to check the parameter of abstract methods.
|
private boolean |
ignoreConstructorParameter
Controls whether to check the parameter of a constructor.
|
private java.util.regex.Pattern |
ignoreFormat
Pattern for names of variables and parameters to ignore.
|
private boolean |
ignoreSetter
Controls whether to check the parameter of a property setter method.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private boolean |
setterCanReturnItsClass
If ignoreSetter is set to true then this variable controls what
the setter method can return By default setter must return void.
|
Constructor and Description |
---|
HiddenFieldCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
private static java.lang.String |
capitalize(java.lang.String name)
Capitalizes a given property name the way we expect to see it in
a setter name.
|
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.
|
private boolean |
isIgnoredConstructorParam(DetailAST ast)
Decides whether to ignore an AST node that is the parameter of a
constructor.
|
private boolean |
isIgnoredParam(DetailAST ast,
java.lang.String name)
Checks whether method or constructor parameter is ignored.
|
private boolean |
isIgnoredParamOfAbstractMethod(DetailAST ast)
Decides whether to ignore an AST node that is the parameter of an
abstract method.
|
private boolean |
isIgnoredSetterParam(DetailAST ast,
java.lang.String name)
Decides whether to ignore an AST node that is the parameter of a
setter method, where the property setter method for field 'xyz' has
name 'setXyz', one parameter named 'xyz', and return type void
(default behavior) or return type is name of the class in which
such method is declared (allowed only if
setSetterCanReturnItsClass(boolean) is called with
value true). |
private static boolean |
isInStatic(DetailAST ast)
Determines whether an AST node is in a static method or static
initializer.
|
private boolean |
isMatchingRegexp(java.lang.String name)
Check name by regExp.
|
private boolean |
isSetterMethod(DetailAST aMethodAST,
java.lang.String aName)
Determine if a specific method identified by methodAST and a single
variable name aName is a setter.
|
private boolean |
isStaticFieldHiddenFromAnonymousClass(DetailAST nameAST,
java.lang.String name)
Checks whether a static field is hidden from closure.
|
private boolean |
isStaticOrInstanceField(DetailAST ast,
java.lang.String name)
Check for static or instance field.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
private void |
processLambda(DetailAST ast)
Process a lambda token.
|
private void |
processVariable(DetailAST ast)
Process a variable token.
|
void |
setIgnoreAbstractMethods(boolean ignoreAbstractMethods)
Set whether to ignore parameters of abstract methods.
|
void |
setIgnoreConstructorParameter(boolean ignoreConstructorParameter)
Set whether to ignore constructor parameters.
|
void |
setIgnoreFormat(java.util.regex.Pattern pattern)
Set the ignore format for the specified regular expression.
|
void |
setIgnoreSetter(boolean ignoreSetter)
Set whether to ignore the parameter of a property setter method.
|
void |
setSetterCanReturnItsClass(boolean aSetterCanReturnItsClass)
Controls if setter can return only void (default behavior) or it
can also return class in which it is declared.
|
private void |
visitOtherTokens(DetailAST ast,
int type)
Called to process tokens other than
TokenTypes.VARIABLE_DEF
and TokenTypes.PARAMETER_DEF . |
void |
visitToken(DetailAST ast)
Called to process a token.
|
destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_KEY
private HiddenFieldCheck.FieldFrame frame
private java.util.regex.Pattern ignoreFormat
private boolean ignoreSetter
private boolean setterCanReturnItsClass
private boolean ignoreConstructorParameter
private boolean ignoreAbstractMethods
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treepublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate void processLambda(DetailAST ast)
ast
- the lambda token.private void visitOtherTokens(DetailAST ast, int type)
TokenTypes.VARIABLE_DEF
and TokenTypes.PARAMETER_DEF
.ast
- token to processtype
- type of the tokenpublic void leaveToken(DetailAST ast)
AbstractCheck
leaveToken
in class AbstractCheck
ast
- the token leavingprivate void processVariable(DetailAST ast)
ast
- the variable token.private boolean isStaticFieldHiddenFromAnonymousClass(DetailAST nameAST, java.lang.String name)
nameAST
- local variable or parameter.name
- field name.private boolean isIgnoredParam(DetailAST ast, java.lang.String name)
ast
- the parameter token.name
- the parameter name.private boolean isStaticOrInstanceField(DetailAST ast, java.lang.String name)
ast
- tokenname
- identifier of tokenprivate boolean isMatchingRegexp(java.lang.String name)
name
- string value to checkprivate static boolean isInStatic(DetailAST ast)
ast
- the node to check.private boolean isIgnoredSetterParam(DetailAST ast, java.lang.String name)
setSetterCanReturnItsClass(boolean)
is called with
value true).ast
- the AST to check.name
- the name of ast.private boolean isSetterMethod(DetailAST aMethodAST, java.lang.String aName)
aMethodAST
- AST corresponding to a method callaName
- name of single parameter of this method.private static java.lang.String capitalize(java.lang.String name)
name
- a property nameprivate boolean isIgnoredConstructorParam(DetailAST ast)
ast
- the AST to check.private boolean isIgnoredParamOfAbstractMethod(DetailAST ast)
ast
- the AST to check.public 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.