Class ModuleDirectiveOrderCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks the ordering, grouping and separation of directives in a module declaration. Directives of each kind must form a single block, the blocks must appear in a configurable order, and each block must be separated from the previous one by exactly one blank line.

The default configuration enforces Google Java Style Guide, Section 3.5.1: all requires directives first, then exports, opens, uses and provides, each kind in a single block, with a single blank line between blocks. Blank lines are what delimit blocks, so blank lines between directives of the same kind are also violations.

All forms of requires (plain, transitive, static) belong to a single block, and the order of directives inside a block is not validated.

Directive kinds that are not listed in the order property are not validated.

Since:
14.1.0
  • Field Details

    • MSG_ORDER

      public static final String MSG_ORDER
      A key pointing to the warning message text in "messages.properties" file. Emitted when a directive block appears after a block that it should precede.
      See Also:
    • MSG_GROUPING

      public static final String MSG_GROUPING
      A key pointing to the warning message text in "messages.properties" file. Emitted when directives of one kind are interleaved with directives of another kind.
      See Also:
    • MSG_SEPARATED_INTERNALLY

      public static final String MSG_SEPARATED_INTERNALLY
      A key pointing to the warning message text in "messages.properties" file. Emitted when directives of the same kind are separated by blank lines.
      See Also:
    • MSG_SEPARATION

      public static final String MSG_SEPARATION
      A key pointing to the warning message text in "messages.properties" file. Emitted when a directive block is not separated from the previous block by exactly one blank line.
      See Also:
    • DEFAULT_ORDER

      private static final List<String> DEFAULT_ORDER
      Default order of directive kinds.
    • VALID_KINDS

      private static final Set<String> VALID_KINDS
      Valid values for entries of the order property.
    • order

      Specify directive kinds in the order their blocks must appear inside the module declaration.
    • validateBlockSeparation

      private boolean validateBlockSeparation
      Control whether blank line separation is validated: exactly one blank line between directive blocks and no blank lines inside a block.
  • Constructor Details

  • Method Details

    • setOrder

      public void setOrder(String... order)
      Setter to specify directive kinds in the order their blocks must appear inside the module declaration.
      Parameters:
      order - the order of directive kinds.
      Throws:
      IllegalArgumentException - when an element of order is not a directive kind.
      Since:
      14.1.0
    • setValidateBlockSeparation

      public void setValidateBlockSeparation(boolean validateBlockSeparation)
      Setter to control whether blank line separation is validated: exactly one blank line between directive blocks and no blank lines inside a block.
      Parameters:
      validateBlockSeparation - the value to set.
      Since:
      14.1.0
    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • 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:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • validateDirectivePlacement

      private void validateDirectivePlacement(DetailAST directive, DetailAST previous, Set<String> seenKinds)
      Validates the placement of a directive relative to the previous directive of the module.

      A directive of the same kind as the previous one continues the current block and must not be separated from it by blank lines. Otherwise the directive starts a new block, which must not repeat an earlier kind, must not belong before the previous block, and must be separated from it by exactly one blank line. Blank line requirements are validated only when validateBlockSeparation is enabled.

      Parameters:
      directive - the directive to validate.
      previous - the directive before the given one.
      seenKinds - kinds of all directives seen before the given one.
    • precedesInOrder

      private boolean precedesInOrder(String kind, String previousKind)
      Checks whether the given kind precedes the other kind in the order property.
      Parameters:
      kind - the kind of the directive being validated.
      previousKind - the kind of the previous directive.
      Returns:
      true if kind precedes previousKind in the configured order.
    • countBlankLinesBetweenDirectives

      private int countBlankLinesBetweenDirectives(DetailAST previous, DetailAST directive)
      Counts the blank lines between the end of the previous directive and the start of the given directive.
      Parameters:
      previous - the directive before the given one.
      directive - the directive to count blank lines before.
      Returns:
      the number of blank lines between the two directives.