Class ModuleImportOrderCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks the ordering and placement of module import declarations. Features are:
  • position of module imports: ensures that module imports are placed above or below all type and static imports (see ModuleImportOrderOption)
  • sorts module imports: ensures that module imports are sorted lexicographically by qualified module name, in ASCII sort order
  • adds a separation between module imports and other imports: ensures that the module import block is separated from type and static imports by, at least, one blank line or comment

This check only validates module imports. It observes type and static imports to locate the boundary of the module import block, but does not validate their order. Use ImportOrder alongside this check for those.

Since:
14.1.0
  • Field Details

    • MSG_POSITION

      public static final String MSG_POSITION
      A key pointing to the warning message text in "messages.properties" file. Emitted when a module import is not placed above or below all type and static imports, as required by the configured option.
      See Also:
    • MSG_SEPARATION

      public static final String MSG_SEPARATION
      A key pointing to the warning message text in "messages.properties" file. Emitted when the module import block is not separated from type and static imports by a blank line.
      See Also:
    • MSG_ORDERING_LEX

      public static final String MSG_ORDERING_LEX
      A key pointing to the warning message text in "messages.properties" file. Emitted when module imports are not sorted lexicographically by qualified module name.
      See Also:
    • imports

      Imports of the current file in order of appearance.
    • option

      Specify policy on the position of module imports relative to type and static imports.
    • separated

      private boolean separated
      Control whether the module import block should be separated from type and static imports by, at least, one blank line or comment.
  • Constructor Details

  • Method Details

    • setOption

      public void setOption(String optionStr)
      Setter to specify policy on the position of module imports relative to type and static imports.
      Parameters:
      optionStr - string to decode option from
      Throws:
      IllegalArgumentException - if unable to decode
      Since:
      14.1.0
    • setSeparated

      public void setSeparated(boolean separated)
      Setter to control whether the module import block should be separated from type and static imports by, at least, one blank line or comment.
      Parameters:
      separated - whether the module import block should be separated.
      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:
    • 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
    • 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
    • finishTree

      public void finishTree(DetailAST rootAST)
      Description copied from class: AbstractCheck
      Called after finished processing a tree. Ideal place to report on information collected whilst processing a tree.
      Overrides:
      finishTree in class AbstractCheck
      Parameters:
      rootAST - the root of the tree
    • checkLexicographicalOrder

      private void checkLexicographicalOrder()
      Checks that module imports are sorted lexicographically. Each module import is compared with the previous module import, regardless of any type or static imports between them.
    • checkPosition

      private boolean checkPosition()
      Checks that module imports are placed above or below all type and static imports, according to the configured option.
      Returns:
      true if any position violation was logged.
    • checkSeparation

      private void checkSeparation()
      Checks that the module import block is separated from the adjacent type and static import block by, at least, one blank line or comment. This method is only invoked when module imports are correctly positioned, so all module imports form a single block above or below all other imports.