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

    Violation Message Keys:

    • annotation.order
    • mod.order
    • 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
        the token set this check is designed for.
        See Also:
      • checkOrderSuggestedByJls

        private static DetailAST checkOrderSuggestedByJls​(List<DetailAST> modifiers)
        Checks if the modifiers were added in the order suggested in the Java language specification.
        modifiers - list of modifier AST tokens
        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.
        modifierIterator - iterator for collection of modifiers
        modifier next to last annotation
      • isAnnotationOnType

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