Class ModifierOrderCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class ModifierOrderCheck
    extends AbstractCheck

    Checks that the order of modifiers conforms to the suggestions in the Java Language specification, § 8.1.1, 8.3.1, 8.4.3 and 9.4. The correct order is:

    1. public
    2. protected
    3. private
    4. abstract
    5. default
    6. static
    7. sealed
    8. non-sealed
    9. final
    10. transient
    11. volatile
    12. synchronized
    13. native
    14. strictfp

    In additional, modifiers are checked to ensure all annotations are declared before all other modifiers.

    Rationale: Code is easier to read if everybody follows a standard.

    ATTENTION: We skip type annotations from validation.

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • annotation.order
    • mod.order
    Since:
    3.0
    • 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
      • checkOrderSuggestedByJls

        private static DetailAST checkOrderSuggestedByJls​(List<DetailAST> modifiers)
        Checks if the modifiers were added in the order suggested in the Java language specification.
        Parameters:
        modifiers - list of modifier AST tokens
        Returns:
        null if the order is correct, otherwise returns the offending modifier AST.
      • skipAnnotations

        private static DetailAST skipAnnotations​(Iterator<DetailAST> modifierIterator)
        Skip all annotations in modifier block.
        Parameters:
        modifierIterator - iterator for collection of modifiers
        Returns:
        modifier next to last annotation
      • isAnnotationOnType

        private static boolean isAnnotationOnType​(DetailAST modifier)
        Checks whether annotation on type takes place.
        Parameters:
        modifier - modifier token.
        Returns:
        true if annotation on type takes place.