Class DeclarationOrderCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class DeclarationOrderCheck
    extends AbstractCheck

    Checks that the parts of a class, record, or interface declaration appear in the order suggested by the Code Conventions for the Java Programming Language.

    According to Code Conventions for the Java Programming Language, the parts of a class or interface declaration should appear in the following order:

    1. Class (static) variables. First the public class variables, then protected, then package level (no access modifier), and then private.
    2. Instance variables. First the public class variables, then protected, then package level (no access modifier), and then private.
    3. Constructors
    4. Methods

    Purpose of ignore* option is to ignore related violations, however it still impacts on other class members.

    ATTENTION: the check skips class fields which have forward references from validation due to the fact that we have Checkstyle's limitations to clearly detect user intention of fields location and grouping. For example:

     public class A {
       private double x = 1.0;
       private double y = 2.0;
       public double slope = x / y; // will be skipped from validation due to forward reference
     }
     
    • Property ignoreConstructors - control whether to ignore constructors. Type is boolean. Default value is false.
    • Property ignoreModifiers - control whether to ignore modifiers (fields, ...). Type is boolean. Default value is false.

    To configure the check:

     <module name="DeclarationOrder"/>
     

    Example:

     public class Test {
    
       public int a;
       protected int b;
       public int c;            // violation, variable access definition in wrong order
    
       Test() {
         this.a = 0;
       }
    
       public void foo() {
         // This method does nothing
       }
    
       Test(int a) {            // violation, constructor definition in wrong order
         this.a = a;
       }
    
       private String name;     // violation, instance variable declaration in wrong order
     }
     

    To configure the check to ignore validation of constructors:

     <module name="DeclarationOrder">
       <property name="ignoreConstructors" value="true"/>
     </module>
     

    Example:

     public class Test {
    
       public int a;
       protected int b;
       public int c;            // violation, variable access definition in wrong order
    
       Test() {
         this.a = 0;
       }
    
       public void foo() {
         // This method does nothing
       }
    
       Test(int a) {            // OK, validation of constructors ignored
         this.a = a;
       }
    
       private String name;     // violation, instance variable declaration in wrong order
     }
     

    To configure the check to ignore modifiers:

     <module name="DeclarationOrder">
       <property name="ignoreModifiers" value="true"/>
     </module>
     

    Example:

     public class Test {
    
       public int a;
       protected int b;
       public int c;            // OK, access modifiers not considered while validating
    
       Test() {
         this.a = 0;
       }
    
       public void foo() {
         // This method does nothing
       }
    
       Test(int a) {            // violation, constructor definition in wrong order
         this.a = a;
       }
    
       private String name;     // violation, instance variable declaration in wrong order
     }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • declaration.order.access
    • declaration.order.constructor
    • declaration.order.instance
    • declaration.order.static
    Since:
    3.2
    • 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
      • processModifiers

        private void processModifiers​(DetailAST ast)
        Processes modifiers.
        Parameters:
        ast - ast of Modifiers.
      • processModifiersState

        private boolean processModifiersState​(DetailAST modifierAst,
                                              DeclarationOrderCheck.ScopeState state)
        Process if given modifiers are appropriate in given state (STATE_STATIC_VARIABLE_DEF, STATE_INSTANCE_VARIABLE_DEF, (STATE_CTOR_DEF, STATE_METHOD_DEF), if it is it updates states where appropriate or logs violation.
        Parameters:
        modifierAst - modifiers to process
        state - current state
        Returns:
        true if modifierAst is valid in given state, false otherwise
      • processModifiersSubState

        private void processModifiersSubState​(DetailAST modifiersAst,
                                              DeclarationOrderCheck.ScopeState state,
                                              boolean isStateValid)
        Checks if given modifiers are valid in substate of given state(Scope), if it is it updates substate or else it logs violation.
        Parameters:
        modifiersAst - modifiers to process
        state - current state
        isStateValid - is main state for given modifiers is valid
      • isForwardReference

        private boolean isForwardReference​(DetailAST fieldDef)
        Checks whether an identifier references a field which has been already defined in class.
        Parameters:
        fieldDef - a field definition.
        Returns:
        true if an identifier references a field which has been already defined in class.
      • getAllTokensOfType

        private static Set<DetailASTgetAllTokensOfType​(DetailAST ast,
                                                         int tokenType)
        Collects all tokens of specific type starting with the current ast node.
        Parameters:
        ast - ast node.
        tokenType - token type.
        Returns:
        a set of all tokens of specific type starting with the current ast node.
      • setIgnoreConstructors

        public void setIgnoreConstructors​(boolean ignoreConstructors)
        Setter to control whether to ignore constructors.
        Parameters:
        ignoreConstructors - whether to ignore constructors.
      • setIgnoreModifiers

        public void setIgnoreModifiers​(boolean ignoreModifiers)
        Setter to control whether to ignore modifiers (fields, ...).
        Parameters:
        ignoreModifiers - whether to ignore modifiers.