public class CustomImportOrderCheck extends AbstractCheck
Checks that the groups of import declarations appear in the order specified by the user. If there is an import but its group is not specified in the configuration such an import should be placed at the end of the import list.
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; //#8If we have SAME_PACKAGE(3) on configuration file, imports #4-6 will be considered as a SAME_PACKAGE group (java.util.concurrent.*, java.util.concurrent.AbstractExecutorService, java.util.concurrent.locks.LockSupport). SAME_PACKAGE(2) will include #1-8. SAME_PACKAGE(4) will include only #6. SAME_PACKAGE(5) will result in no imports assigned to SAME_PACKAGE group because actual package java.util.concurrent.locks has only 4 domains.
Use the separator '###' between rules.
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.
customImportOrderRules
- Specify format of order declaration
customizing by user.
Type is java.lang.String
.
Default value is ""
.
standardPackageRegExp
- Specify RegExp for STANDARD_JAVA_PACKAGE group imports.
Type is java.util.regex.Pattern
.
Default value is "^(java|javax)\."
.
thirdPartyPackageRegExp
- Specify RegExp for THIRD_PARTY_PACKAGE group imports.
Type is java.util.regex.Pattern
.
Default value is ".*"
.
specialImportsRegExp
- Specify RegExp for SPECIAL_IMPORTS group imports.
Type is java.util.regex.Pattern
.
Default value is "^$" (empty)
.
separateLineBetweenGroups
- Force empty line separator between
import groups.
Type is boolean
.
Default value is true
.
sortImportsInGroupAlphabetically
- Force grouping alphabetically,
in ASCII sort order.
Type is boolean
.
Default value is false
.
To configure the check so that it matches default Eclipse formatter configuration (tested on Kepler and Luna releases):
Notes:
<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>
To configure the check so that it matches default Eclipse formatter configuration (tested on Mars release):
<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>
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="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>
To configure the check so that it matches default NetBeans formatter configuration (tested on v8):
<module name="CustomImportOrder"/>
To set RegExps for THIRD_PARTY_PACKAGE and STANDARD_JAVA_PACKAGE groups use thirdPartyPackageRegExp and standardPackageRegExp options.
<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>
Also, this check can be configured to force empty line separator between import groups. For example.
<module name="CustomImportOrder"> <property name="separateLineBetweenGroups" value="true"/> </module>
It is possible to enforce ASCII sort order of imports in groups using the following configuration:
<module name="CustomImportOrder"> <property name="sortImportsInGroupAlphabetically" value="true"/> </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="CustomImportOrder"> <property name="customImportOrderRules" value="SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STATIC###SPECIAL_IMPORTS"/> <property name="specialImportsRegExp" value="^android\."/> </module>
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
custom.import.order
custom.import.order.lex
custom.import.order.line.separator
custom.import.order.nonGroup.expected
custom.import.order.nonGroup.import
custom.import.order.separated.internally
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_LEX
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_LINE_SEPARATOR
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_NONGROUP_EXPECTED
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_NONGROUP_IMPORT
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_ORDER
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_SEPARATED_IN_GROUP
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
SAME_PACKAGE_RULE_GROUP
SAME_PACKAGE group name.
|
static String |
SPECIAL_IMPORTS_RULE_GROUP
SPECIAL_IMPORTS group name.
|
static String |
STANDARD_JAVA_PACKAGE_RULE_GROUP
STANDARD_JAVA_PACKAGE group name.
|
static String |
STATIC_RULE_GROUP
STATIC group name.
|
static String |
THIRD_PARTY_PACKAGE_RULE_GROUP
THIRD_PARTY_PACKAGE group name.
|
Constructor and Description |
---|
CustomImportOrderCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
void |
finishTree(DetailAST rootAST)
Called after finished processing a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setCustomImportOrderRules(String inputCustomImportOrder)
Setter to specify format of order declaration customizing by user.
|
void |
setSeparateLineBetweenGroups(boolean value)
Setter to force empty line separator between import groups.
|
void |
setSortImportsInGroupAlphabetically(boolean value)
Setter to force grouping alphabetically, in
ASCII sort order.
|
void |
setSpecialImportsRegExp(Pattern regexp)
Setter to specify RegExp for SPECIAL_IMPORTS group imports.
|
void |
setStandardPackageRegExp(Pattern regexp)
Setter to specify RegExp for STANDARD_JAVA_PACKAGE group imports.
|
void |
setThirdPartyPackageRegExp(Pattern regexp)
Setter to specify RegExp for THIRD_PARTY_PACKAGE group imports.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_LINE_SEPARATOR
public static final String MSG_SEPARATED_IN_GROUP
public static final String MSG_LEX
public static final String MSG_NONGROUP_IMPORT
public static final String MSG_NONGROUP_EXPECTED
public static final String MSG_ORDER
public static final String STATIC_RULE_GROUP
public static final String SAME_PACKAGE_RULE_GROUP
public static final String THIRD_PARTY_PACKAGE_RULE_GROUP
public static final String STANDARD_JAVA_PACKAGE_RULE_GROUP
public static final String SPECIAL_IMPORTS_RULE_GROUP
public CustomImportOrderCheck()
public final void setStandardPackageRegExp(Pattern regexp)
regexp
- user value.public final void setThirdPartyPackageRegExp(Pattern regexp)
regexp
- user value.public final void setSpecialImportsRegExp(Pattern regexp)
regexp
- user value.public final void setSeparateLineBetweenGroups(boolean value)
value
- user value.public final void setSortImportsInGroupAlphabetically(boolean value)
value
- user value.public final void setCustomImportOrderRules(String inputCustomImportOrder)
inputCustomImportOrder
- user value.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treepublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processpublic void finishTree(DetailAST rootAST)
AbstractCheck
finishTree
in class AbstractCheck
rootAST
- the root of the treeCopyright © 2001–2020. All rights reserved.