public class CustomImportOrderCheck extends Check
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:STATIC group. This group sets the ordering of static imports.
SAME_PACKAGE(n) group. This group sets the ordering of the same package imports. 'n' - a number of the first package domains. For example:
package java.util.concurrent; import java.util.regex.Pattern; import java.util.List; import java.util.StringTokenizer; import java.util.regex.Pattern; import java.util.*; import java.util.concurrent.AbstractExecutorService; import java.util.concurrent.*; And we have such configuration: SAME_PACKAGE (3). Same package imports are java.util.*, java.util.concurrent.*, java.util.concurrent.AbstractExecutorService, java.util.List and java.util.StringTokenizer
THIRD_PARTY_PACKAGE group. This group sets ordering of third party imports. Third party imports are all imports except STATIC, SAME_PACKAGE(n) and STANDARD_JAVA_PACKAGE.
STANDARD_JAVA_PACKAGE group. This group sets ordering of standard java (java|javax) imports.
SPECIAL_IMPORTS group. This group may contains some imports that have particular meaning for the user.
NOTICE!
Use the separator '###' between rules.
To set RegExps for THIRD_PARTY_PACKAGE and STANDARD_JAVA_PACKAGE groups use thirdPartyPackageRegExp and standardPackageRegExp options.
For example:
<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>
By the option it is possible to force alphabetically sorting.
<module name="CustomImportOrder"> <property name="sortImportsInGroupAlphabetically" value="true"/> </module>
Constructor and Description |
---|
CustomImportOrderCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST aRootAST)
Called before the starting to process a tree.
|
void |
finishTree(DetailAST aRootAST)
Called after finished processing a tree.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
void |
setCustomImportOrderRules(String aInputCustomImportOrder)
Sets a custom import order from the rules in the string format specified
by user.
|
void |
setSeparateLineBetweenGroups(boolean aValue)
Sets mSeparateLineBetweenGroups specified by user.
|
void |
setSortImportsInGroupAlphabetically(boolean aValue)
Sets mSortImportsInGroupAlphabetically specified by user.
|
void |
setSpecialImportsRegExp(String aRegexp)
Sets mSpecialImportsRegExp specified by user.
|
void |
setStandardPackageRegExp(String aRegexp)
Sets mStandardRegExp specified by user.
|
void |
setThirdPartyPackageRegExp(String aRegexp)
Sets mThirdPartyRegExp specified by user.
|
void |
visitToken(DetailAST aAST)
Called to process a token.
|
destroy, getAcceptableTokens, getClassLoader, getFileContents, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public final void setStandardPackageRegExp(String aRegexp)
aRegexp
- user value.public final void setThirdPartyPackageRegExp(String aRegexp)
aRegexp
- user value.public final void setSpecialImportsRegExp(String aRegexp)
aRegexp
- user value.public final void setSeparateLineBetweenGroups(boolean aValue)
aValue
- user value.public final void setSortImportsInGroupAlphabetically(boolean aValue)
aValue
- user value.public final void setCustomImportOrderRules(String aInputCustomImportOrder)
aInputCustomImportOrder
- user value.public int[] getDefaultTokens()
Check
getDefaultTokens
in class Check
TokenTypes
public void beginTree(DetailAST aRootAST)
Check
public void visitToken(DetailAST aAST)
Check
visitToken
in class Check
aAST
- the token to processpublic void finishTree(DetailAST aRootAST)
Check
finishTree
in class Check
aRootAST
- the root of the treeCopyright © 2001–2015. All rights reserved.