Class 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 is java.util.regex.Pattern. Default value is null.
    • Property ignoreConstructorParameter - Control whether to ignore constructor parameters. Type is boolean. Default value is false.
    • Property ignoreSetter - Allow to ignore the parameter of a property setter method. Type is boolean. Default value is false.
    • Property 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.
    • Property ignoreAbstractMethods - Control whether to ignore parameters of abstract methods. Type is boolean. Default value is false.
    • Property 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
    Since:
    3.0
    • Field Detail

      • ignoreFormat

        private Pattern ignoreFormat
        Define the RegExp for names of variables and parameters to ignore.
      • ignoreSetter

        private boolean ignoreSetter
        Allow to ignore the parameter of a property setter method.
      • setterCanReturnItsClass

        private boolean setterCanReturnItsClass
        Allow to expand the definition of a setter method to include methods that return the class' instance.
      • ignoreAbstractMethods

        private boolean ignoreAbstractMethods
        Control whether to ignore parameters of abstract methods.
    • Method Detail

      • 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 class AbstractCheck
        Returns:
        the token set this check is designed 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 class AbstractCheck
        Parameters:
        rootAST - the root of the tree
      • processLambda

        private void processLambda​(DetailAST ast)
        Process a lambda token. Checks whether a lambda parameter shadows a field. Note, that when parameter of lambda expression is untyped, ANTLR parses the parameter as an identifier.
        Parameters:
        ast - the lambda token.
      • processVariable

        private void processVariable​(DetailAST ast)
        Process a variable token. Check whether a local variable or parameter shadows a field. Store a field for later comparison with local variables and parameters.
        Parameters:
        ast - the variable token.
      • isIgnoredParam

        private boolean isIgnoredParam​(DetailAST ast,
                                       String name)
        Checks whether method or constructor parameter is ignored.
        Parameters:
        ast - the parameter token.
        name - the parameter name.
        Returns:
        true if parameter is ignored.
      • isInstanceField

        private boolean isInstanceField​(DetailAST ast,
                                        String name)
        Check for instance field.
        Parameters:
        ast - token
        name - identifier of token
        Returns:
        true if instance field
      • isMatchingRegexp

        private boolean isMatchingRegexp​(String name)
        Check name by regExp.
        Parameters:
        name - string value to check
        Returns:
        true is regexp is matching
      • isInStatic

        private static boolean isInStatic​(DetailAST ast)
        Determines whether an AST node is in a static method or static initializer.
        Parameters:
        ast - the node to check.
        Returns:
        true if ast is in a static method or a static block;
      • isIgnoredSetterParam

        private boolean isIgnoredSetterParam​(DetailAST ast,
                                             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).
        Parameters:
        ast - the AST to check.
        name - the name of ast.
        Returns:
        true if ast should be ignored because check property ignoreSetter is true and ast is the parameter of a setter method.
      • isSetterMethod

        private boolean isSetterMethod​(DetailAST aMethodAST,
                                       String aName)
        Determine if a specific method identified by methodAST and a single variable name aName is a setter. This recognition partially depends on mSetterCanReturnItsClass property.
        Parameters:
        aMethodAST - AST corresponding to a method call
        aName - name of single parameter of this method.
        Returns:
        true of false indicating of method is a setter or not.
      • capitalize

        private static String capitalize​(String name)
        Capitalizes a given property name the way we expect to see it in a setter name.
        Parameters:
        name - a property name
        Returns:
        capitalized property name
      • isIgnoredConstructorParam

        private boolean isIgnoredConstructorParam​(DetailAST ast)
        Decides whether to ignore an AST node that is the parameter of a constructor.
        Parameters:
        ast - the AST to check.
        Returns:
        true if ast should be ignored because check property ignoreConstructorParameter is true and ast is a constructor parameter.
      • isIgnoredParamOfAbstractMethod

        private boolean isIgnoredParamOfAbstractMethod​(DetailAST ast)
        Decides whether to ignore an AST node that is the parameter of an abstract method.
        Parameters:
        ast - the AST to check.
        Returns:
        true if ast should be ignored because check property ignoreAbstractMethods is true and ast is a parameter of abstract methods.
      • setIgnoreFormat

        public void setIgnoreFormat​(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.