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), STANDARD_JAVA_PACKAGE and SPECIAL_IMPORTS.
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>
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>
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[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
void |
setCustomImportOrderRules(String inputCustoimportOrder)
Sets a custom import order from the rules in the string format specified
by user.
|
void |
setSeparateLineBetweenGroups(boolean value)
Sets separateLineBetweenGroups specified by user.
|
void |
setSortImportsInGroupAlphabetically(boolean value)
Sets sortImportsInGroupAlphabetically specified by user.
|
void |
setSpecialImportsRegExp(String regexp)
Sets specialImportsRegExp specified by user.
|
void |
setStandardPackageRegExp(String regexp)
Sets standardRegExp specified by user.
|
void |
setThirdPartyPackageRegExp(String regexp)
Sets thirdPartyRegExp specified by user.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
destroy, getAcceptableTokens, getClassLoader, getFileContents, getLine, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public CustomImportOrderCheck()
public final void setStandardPackageRegExp(String regexp)
regexp
- user value.public final void setThirdPartyPackageRegExp(String regexp)
regexp
- user value.public final void setSpecialImportsRegExp(String 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 inputCustoimportOrder)
inputCustoimportOrder
- user value.public int[] getDefaultTokens()
Check
getDefaultTokens
in class Check
TokenTypes
public void beginTree(DetailAST rootAST)
Check
public void visitToken(DetailAST ast)
Check
visitToken
in class Check
ast
- the token to processpublic void finishTree(DetailAST rootAST)
Check
finishTree
in class Check
rootAST
- the root of the treeCopyright © 2001–2015. All rights reserved.