ModuleImportOrder

Since Checkstyle 14.1.0

Description

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.

Properties

name description type default value since
option Specify policy on the position of module imports relative to type and static imports. ModuleImportOrderOption top 14.1.0
separated Control whether the module import block should be separated from type and static imports by, at least, one blank line or comment. boolean false 14.1.0

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ModuleImportOrder"/>
  </module>
</module>

Example:


import module java.sql;
// violation below 'Wrong lexicographical order for module import 'java.desktop''
import module java.desktop;
import java.util.List;
// violation below 'Module import 'java.logging' violates the configured'
import module java.logging;

To configure the check to require a blank line between the module import block and other imports:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ModuleImportOrder">
      <property name="separated" value="true"/>
    </module>
  </module>
</module>

Example:


import module java.desktop;
import module java.sql;
import java.util.List; // violation ''java.util.List' should be separated'

To configure the check to require module imports to be placed below all type and static imports:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ModuleImportOrder">
      <property name="option" value="bottom"/>
    </module>
  </module>
</module>

Example:


// violation below 'Module import 'java.desktop' violates the configured'
import module java.desktop;
import java.util.List;

import module java.sql;

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.imports.ModuleImportOrderCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker