Since Checkstyle 5.8
The rule consists of:
package java.util.concurrent.locks; import java.io.File; import java.util.*; //#1 import java.util.List; //#2 import java.util.StringTokenizer; //#3 import java.util.concurrent.*; //#4 import java.util.concurrent.AbstractExecutorService; //#5 import java.util.concurrent.locks.LockSupport; //#6 import java.util.regex.Pattern; //#7 import java.util.regex.Matcher; //#8
Rules are configured as a comma-separated ordered list.
Note: '###' group separator is deprecated (in favor of a comma-separated list), but is currently supported for backward compatibility.
To set RegExps for THIRD_PARTY_PACKAGE and STANDARD_JAVA_PACKAGE groups use thirdPartyPackageRegExp and standardPackageRegExp options.
Pretty often one import can match more than one group. For example, static import from standard package or regular expressions are configured to allow one import match multiple groups. In this case, group will be assigned according to priorities:
Few examples to illustrate "best match":
1. patterns STANDARD_JAVA_PACKAGE = "Check", SPECIAL_IMPORTS="ImportOrderCheck" and input file:
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck;
Result: imports will be assigned to SPECIAL_IMPORTS, because matching substring length is 16. Matching substring for STANDARD_JAVA_PACKAGE is 5.
2. patterns STANDARD_JAVA_PACKAGE = "Check", SPECIAL_IMPORTS="Avoid" and file:
import com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck;
Result: import will be assigned to SPECIAL_IMPORTS. Matching substring length is 5 for both patterns. However, "Avoid" position is lower than "Check" position.
name | description | type | default value | since |
---|---|---|---|---|
customImportOrderRules | Specify ordered list of import groups. | String[] | {} |
5.8 |
separateLineBetweenGroups | Force empty line separator between import groups. | boolean | true |
5.8 |
sortImportsInGroupAlphabetically | Force grouping alphabetically, in ASCII sort order. | boolean | false |
5.8 |
specialImportsRegExp | Specify RegExp for SPECIAL_IMPORTS group imports. | Pattern | "^$" |
5.8 |
standardPackageRegExp | Specify RegExp for STANDARD_JAVA_PACKAGE group imports. | Pattern | "^(java|javax)\." |
5.8 |
thirdPartyPackageRegExp | Specify RegExp for THIRD_PARTY_PACKAGE group imports. | Pattern | ".*" |
5.8 |
To configure the check :
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"/> </module> </module>
Example:
package com.company; import org.apache.commons.io.FileUtils; // OK import static java.util.*; // OK import java.time.*; // OK import static java.io.*; // OK import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
To configure the check so that it checks in the order (static imports,standard java packages,third party package):
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, STANDARD_JAVA_PACKAGE, THIRD_PARTY_PACKAGE"/> </module> </module> </module>
Example:
package com.company; import static java.util.*; // OK import java.time.*; // OK import javax.net.*; // OK import static java.io.*; // violation as static imports should be in top import org.apache.commons.io.FileUtils; // OK import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
To configure the check such that only java packages are included in standard java packages
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, STANDARD_JAVA_PACKAGE, THIRD_PARTY_PACKAGE"/> <property name="standardPackageRegExp" value="^java\."/> </module> </module> </module>
Example:
package com.company; import static java.util.*; // OK import static java.io.*; // OK import java.time.*; // OK import javax.net.*; // violation as it is not included in standard java package group. import org.apache.commons.io.FileUtils; // violation import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
To configure the check to include only "com" packages as third party group imports:
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, STANDARD_JAVA_PACKAGE, SPECIAL_IMPORTS, THIRD_PARTY_PACKAGE"/> <property name="thirdPartyPackageRegExp" value="^com\."/> </module> </module> </module>
Example:
package com.company; import static java.util.*; // OK import static java.io.*; // OK import java.time.*; // OK import javax.net.*; // OK import org.apache.commons.io.FileUtils; // violation(should be in end) import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // violation import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
To configure the check to force some packages in special import group:
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, SPECIAL_IMPORTS, STANDARD_JAVA_PACKAGE"/> <property name="specialImportsRegExp" value="^org\."/> </module> </module> </module>
Example:
package com.company; import static java.util.*; // OK import static java.io.*; // OK import org.json.JSONObject; // OK import java.time.*; // OK import javax.net.*; // OK import org.apache.commons.io.FileUtils; // violation
To configure the check such that empty line separator between two groups is enabled:
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, STANDARD_JAVA_PACKAGE, SPECIAL_IMPORTS, THIRD_PARTY_PACKAGE"/> <property name="specialImportsRegExp" value="^org\."/> <property name="thirdPartyPackageRegExp" value="^com\."/> <property name="separateLineBetweenGroups" value="true"/> </module> </module> </module>
Example:
package com.company; import static java.util.*; // OK import static java.io.*; // OK import java.time.*; // OK import javax.net.*; // OK import org.apache.commons.io.FileUtils; // violation import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // violation import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
To configure the check such that import groups are forced to be sorted alphabetically:
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, STANDARD_JAVA_PACKAGE, SPECIAL_IMPORTS, THIRD_PARTY_PACKAGE"/> <property name="specialImportsRegExp" value="^org\."/> <property name="thirdPartyPackageRegExp" value="^com\."/> <property name="separateLineBetweenGroups" value="false"/> <property name="sortImportsInGroupAlphabetically" value="true"/> </module> </module> </module>
Example:
package com.company; import static java.util.*; // OK import static java.io.*; // Violation since it should come before"java.util" import java.time.*; // OK import javax.net.*; // OK import org.apache.commons.io.FileUtils; // OK import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
To configure the check so that it matches default Eclipse formatter configuration (tested on Kepler and Luna releases):
Notes:
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, STANDARD_JAVA_PACKAGE, SPECIAL_IMPORTS"/> <property name="specialImportsRegExp" value="^org\."/> <property name="sortImportsInGroupAlphabetically" value="true"/> <property name="separateLineBetweenGroups" value="true"/> </module> </module> </module>
Example:
package com.company; import static java.util.*; // OK import static java.io.*; // Violation since it should come before"java.util" import java.time.*; // OK import javax.net.*; // OK import org.apache.commons.io.FileUtils; // Violation should be separated by space import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
To configure the check so that it matches default Eclipse formatter configuration (tested on Mars release):
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, STANDARD_JAVA_PACKAGE, SPECIAL_IMPORTS, THIRD_PARTY_PACKAGE"/> <property name="specialImportsRegExp" value="^org\."/> <property name="thirdPartyPackageRegExp" value="^com\."/> <property name="sortImportsInGroupAlphabetically" value="true"/> <property name="separateLineBetweenGroups" value="true"/> </module> </module> </module>
Example:
package com.company; import static java.io.*; // OK import static java.util.*; // OK import java.time.*; // OK import javax.net.*; // OK import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // Violation import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // Violation import org.apache.commons.io.FileUtils;
To configure the check so that it matches default IntelliJ IDEA formatter configuration (tested on v14):
Note: "separated" option is disabled because IDEA default has blank line between "java" and static imports, and no blank line between "javax" and "java"
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE, SPECIAL_IMPORTS, STANDARD_JAVA_PACKAGE, STATIC"/> <property name="specialImportsRegExp" value="^javax\."/> <property name="standardPackageRegExp" value="^java\."/> <property name="sortImportsInGroupAlphabetically" value="true"/> <property name="separateLineBetweenGroups" value="false"/> </module> </module> </module>
Example:
package com.company; import static java.io.*; // OK import static java.util.*; // OK import java.time.*; // violation should be in standard package group // below special import import javax.net.*; // Violation should be in special import group import org.apache.commons.io.FileUtils; // Violation should be in // THIRD PARTY PACKAGE GROUP import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // Violation import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // Violation
To configure the check so that it matches default NetBeans formatter configuration (tested on v8):
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"/> </module> </module>
Example:
package com.company; import static java.io.*; // OK import static java.util.*; // OK import java.time.*; // OK import javax.net.*; // OK import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK import org.apache.commons.io.FileUtils; // should not be separated by line
To set RegExps for THIRD_PARTY_PACKAGE and STANDARD_JAVA_PACKAGE groups use thirdPartyPackageRegExp and standardPackageRegExp options.
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="STATIC, SAME_PACKAGE(3), THIRD_PARTY_PACKAGE, STANDARD_JAVA_PACKAGE"/> <property name="thirdPartyPackageRegExp" value="^(com|org)\."/> <property name="standardPackageRegExp" value="^(java|javax)\."/> </module> </module> </module>
Example:
package com.company; import static java.io.*; // OK import static java.util.*; // OK import java.time.*; // violation import javax.net.*; // violation import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK import org.apache.commons.io.FileUtils; // OK
Also, this check can be configured to force empty line separator between import groups. For example.
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="separateLineBetweenGroups" value="true"/> </module> </module> </module>
Example:
package com.company; import static java.io.*; // OK import static java.util.*; // OK import java.time.*; // OK import javax.net.*; // OK import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK import org.apache.commons.io.FileUtils; // OK
It is possible to enforce ASCII sort order of imports in groups using the following configuration:
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="sortImportsInGroupAlphabetically" value="true"/> </module> </module> </module>
Example of ASCII order:
import java.awt.Dialog; import java.awt.Window; import java.awt.color.ColorSpace; import java.awt.Frame; // violation here - in ASCII order 'F' should go before 'c', // as all uppercase come before lowercase letters
To force checking imports sequence such as:
package com.puppycrawl.tools.checkstyle.imports; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.Beta; import com.google.common.annotations.VisibleForTesting; import org.abego.treelayout.Configuration; import static sun.tools.util.ModifierFilter.ALL_ACCESS; import com.google.common.annotations.GwtCompatible; // violation here - should be in the // THIRD_PARTY_PACKAGE group import android.*;
configure as follows:
<module name="Checker"> <module name="TreeWalker"> <module name="CustomImportOrder"> <property name="customImportOrderRules" value="SAME_PACKAGE(3), THIRD_PARTY_PACKAGE, STATIC, SPECIAL_IMPORTS"/> <property name="specialImportsRegExp" value="^android\."/> </module> </module> </module>
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports