Class AvoidModifiersForTypesCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class AvoidModifiersForTypesCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    Disallow some set of modifiers for Java types specified by regexp.

    Field modifiers types according to Java Spec: (https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1)

    • Annotation: using the 'forbiddenClassesRegexpAnnotation' option.
    • final: using the 'forbiddenClassesRegexpFinal' option.
    • static: using the 'forbiddenClassesRegexpStatic'option.
    • transient: using the 'forbiddenClassesRegexpTransient' option.
    • volatile: using the 'forbiddenClassesRegexpVolatile' option.
    • private: using the 'forbiddenClassesRegexpPrivate' option.
    • package-private: using the 'forbiddenClassesRegexpPackagePrivate' option.
    • protected: using the 'forbiddenClassesRegexpProtected' option.
    • public: using the 'forbiddenClassesRegexpPublic' option.

    Example 1: Forbid use of 'static' modifiers for 'ULCComponents' (http://ulc.canoo.com/ulccommunity/Contributions/Extensions/GoodPractices.html)

    Never keep instances of ULC classes in static variables (ULCIcons neither!). They cannot be shared between different sessions.

    So we can disallow "static" modifier for all ULC* components by setting up an "forbiddenClassesRegexpStatic" option to "ULC.+" regexp String.

    Configuration:

     <module name="TreeWalker">
          <module name="AvoidModifiersForTypesCheck">
              <property name="forbiddenClassesRegexpStatic" value="ULC.+"/>
          </module>
     </module>
     

    Example 2: Forbid using annotation for fields: (e.g. @Autowired ). This can be done by setting up the "forbiddenClassesRegexpAnnotation" option to "Person" regexp String.

    Configuration:

     <module name="TreeWalker">
          <module name="AvoidModifiersForTypesCheck">
              <property name="forbiddenClassesRegexpAnnotation" value="Person"/>
          </module>
     </module>
     
     public class Customer {
    
         @Autowired
         private Person person; // Violation
    
         private int type; // OK
    
         private String action; // OK
    
     }
     

    Example 3: Forbid returning Logger out of the class, since it is a very bad practice as it produce logs that are hard to investigate as logging class does not contains that code and search should be done in other classes or in hierarchy (if filed is public or accessible by other protected or package).

    This check can be activated by setting up the "forbiddenClassesRegexpPublic", "forbiddenClassesRegexpPackagePrivate" and "forbiddenClassesRegexpProtected" options to "Logger" regexp String.

    Configuration:

     <module name="TreeWalker">
          <module name="AvoidModifiersForTypesCheck">
              <property name="forbiddenClassesRegexpProtected" value="Logger"/>
              <property name="forbiddenClassesRegexpPublic" value="Logger"/>
              <property name="forbiddenClassesRegexpPackagePrivate" value="Logger"/>
          <module>
     </module>
     
     public class Check {
    
         private Logger log1 = Logger.getLogger(getClass().getName()); // OK
    
         protected Logger log2 = Logger.getLogger(getClass().getName()); // Violation
    
         public Logger log3 = Logger.getLogger(getClass().getName()); // Violation
    
         Logger log4 = Logger.getLogger(getClass().getName()); // Violation
    
     }
     
    Since:
    1.8.0
    Author:
    Daniil Yaroslavtsev, Yasser Aziza
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      The key is pointing to the message text String in "messages.properties file".
    • Field Detail

      • MSG_KEY

        public static final String MSG_KEY
        The key is pointing to the message text String in "messages.properties file".
        See Also:
        Constant Field Values
    • Constructor Detail

      • AvoidModifiersForTypesCheck

        public AvoidModifiersForTypesCheck()
    • Method Detail

      • setForbiddenClassesRegexpAnnotation

        public void setForbiddenClassesRegexpAnnotation​(String forbiddenClassesRegexpAnnotation)
        Sets the regexp for the names of classes, that could not have 'annotation' modifier.
        Parameters:
        forbiddenClassesRegexpAnnotation - String contains the regex to set for the names of classes, that could not have 'annotation' modifier.
      • setForbiddenClassesRegexpFinal

        public void setForbiddenClassesRegexpFinal​(String forbiddenClassesRegexpFinal)
        Sets the regexp for the names of classes, that could not have 'final' modifier.
        Parameters:
        forbiddenClassesRegexpFinal - String contains the regex to set for the names of classes, that could not have 'final' modifier.
      • setForbiddenClassesRegexpStatic

        public void setForbiddenClassesRegexpStatic​(String forbiddenClassesRegexpStatic)
        Sets the regexp for the names of classes, that could not have 'static' modifier.
        Parameters:
        forbiddenClassesRegexpStatic - String contains the regex to set for the names of classes, that could not have 'static' modifier.
      • setForbiddenClassesRegexpTransient

        public void setForbiddenClassesRegexpTransient​(String forbiddenClassesRegexpTransient)
        Sets the regexp for the names of classes, that could not have 'transient' modifier.
        Parameters:
        forbiddenClassesRegexpTransient - String contains the regex to set for the names of classes, that could not have 'transient' modifier.
      • setForbiddenClassesRegexpVolatile

        public void setForbiddenClassesRegexpVolatile​(String forbiddenClassesRegexpVolatile)
        Sets the regexp for the names of classes, that could not have 'volatile' modifier.
        Parameters:
        forbiddenClassesRegexpVolatile - String contains the regex to set for the names of classes, that could not have 'volatile' modifier.
      • setForbiddenClassesRegexpPrivate

        public void setForbiddenClassesRegexpPrivate​(String forbiddenClassesRegexpPrivate)
        Sets the regexp for the names of classes, that could not have 'private' modifier.
        Parameters:
        forbiddenClassesRegexpPrivate - String contains the regex to set for the names of classes, that could not have 'private' modifier.
      • setForbiddenClassesRegexpPackagePrivate

        public void setForbiddenClassesRegexpPackagePrivate​(String forbiddenClassesRegexpPackagePrivate)
        Sets the regexp for the names of classes, that could not have no modifier ('package-private').
        Parameters:
        forbiddenClassesRegexpPackagePrivate - String contains the regex to set for the names of classes, that could not have no modifier ('package-private').
      • setForbiddenClassesRegexpProtected

        public void setForbiddenClassesRegexpProtected​(String forbiddenClassesRegexpProtected)
        Sets the regexp for the names of classes, that could not have 'protected' modifier.
        Parameters:
        forbiddenClassesRegexpProtected - String contains the regex to set for the names of classes, that could not have 'protected' modifier.
      • setForbiddenClassesRegexpPublic

        public void setForbiddenClassesRegexpPublic​(String forbiddenClassesRegexpPublic)
        Sets the regexp for the names of classes, that could not have 'public' modifier.
        Parameters:
        forbiddenClassesRegexpPublic - String contains the regex to set for the names of classes, that could not have 'public' modifier.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck