Class CustomDeclarationOrderCheck

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

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

    Checks that the parts of a class(main, nested, member inner) declaration appear in the rules order set by user using regular expressions.

    The check forms line which consists of class member annotations, modifiers, type and name from your code and compares it with your RegExp.

    The rule consists of:
     ClassMember(RegExp)
     
    To set class order use the following notation of the class members (case insensitive):
    1. "Field" to denote the Fields
    2. "DeclareAnonClassField" to denote the fields keeping objects of anonymous classes
    3. "Ctor" to denote the Constructors
    4. "Method" to denote the Methods
    5. "GetterSetter" to denote the group of getter and setter methods
    6. "MainMethod" to denote the main method
    7. "InnerClass" to denote the Inner Classes
    8. "InnerInterface" to denote the Inner Interfaces
    9. "InnerEnum" to denote the Inner Enums
    RegExp can include:
    1. Annotations
    2. Modifiers(public, protected, private, abstract, static, final)
    3. Type
    4. Name
    ATTENTION!

    Use separator ' ', '.', '\s' between declaration in the RegExp. Whitespace should be added after each modifier.

     Example:
          Field(public .*final .*)
          Field(public final .*)
          Field(public\s*final .*)
     
    NOTICE!

    It is important to write exact order of modifiers in rules. So rule Field(public final) does not match to final public value;. ModifierOrderCheck is recommended to use.

    If you set empty RegExp e.g. Field(), it means that class member doesn't have modifiers(default modifier) and checking the type and name of member doesn't occur.

    Between the declaration of a array and generic can't be whitespaces. E.g.: ArrayList<String[]> someName

    Use the separator '###' between the class declarations.

    For Example:

    Field(private static final long serialVersionUID) ### Field(public static final .*) ### Field(.*private .*) ### Ctor(.*) ### GetterSetter(.*) ### Method(.*public .*final .*|@Ignore.*public .*) ### Method(public static .*(final|(new|edit|create).*).*) ### InnerClass(public abstract .*) ### InnerInterface(.*) ### InnerEnum(.*)

    What is group of getters and setters(GetterSetter)?

    It is ordered sequence of getters and setters like:

     public int getValue() {
         log.info("Getting value");
         return value;
     }
    
     public void setValue(int newValue) {
         value = newValue;
     }
    
     public Object getObj() {
        return obj;
     }
    
     public void setObj(Object obj) {
        if (obj != null) {
          this.obj = obj;
        } else {
          throw new IllegalArgumentException("Null value");
        }
     }
    
     ...
     

    Getter is public method that returns class field. Name of getter should be 'getFieldName' in camel case.

    Setter is public method with one parameter that assigns this parameter to class field. Name of setter should be 'setFieldName' in camel case.

    Setter of field X should be right after getter of field X.

    Since:
    1.8.0
    Author:
    Danil Lopatin, Baratali Izmailov
    • 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_CLASS
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_CONSTRUCTOR
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_ENUM
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_FIELD
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_INTERFACE
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_INVALID_SETTER
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_METHOD
      A key is pointing to the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void leaveToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      void setCaseSensitive​(boolean caseSensitive)
      Set whether or not the match is case sensitive.
      void setCustomDeclarationOrder​(String inputOrderDeclaration)
      Set custom order declaration from string with user rules.
      void setFieldPrefix​(String fieldPrefix)
      Set prefix of class fields.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, 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_FIELD

        public static final String MSG_KEY_FIELD
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_METHOD

        public static final String MSG_KEY_METHOD
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_CONSTRUCTOR

        public static final String MSG_KEY_CONSTRUCTOR
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_CLASS

        public static final String MSG_KEY_CLASS
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_INTERFACE

        public static final String MSG_KEY_INTERFACE
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_ENUM

        public static final String MSG_KEY_ENUM
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_INVALID_SETTER

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

      • CustomDeclarationOrderCheck

        public CustomDeclarationOrderCheck()
        Constructor to set default format.
    • Method Detail

      • setCustomDeclarationOrder

        public final void setCustomDeclarationOrder​(String inputOrderDeclaration)
        Set custom order declaration from string with user rules.
        Parameters:
        inputOrderDeclaration - The string line with the user custom declaration.
        Throws:
        IllegalArgumentException - if the input rule could not be parsed
      • setFieldPrefix

        public void setFieldPrefix​(String fieldPrefix)
        Set prefix of class fields.
        Parameters:
        fieldPrefix - string
      • setCaseSensitive

        public void setCaseSensitive​(boolean caseSensitive)
        Set whether or not the match is case sensitive.
        Parameters:
        caseSensitive - true if the match is case sensitive.
      • 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
      • leaveToken

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