Class ModuleImportOrderCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.imports.ModuleImportOrderCheck
- All Implemented Interfaces:
Configurable,Contextualizable
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.
- Since:
- 14.1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordContains import attributes as import full path, module flag and import AST.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<ModuleImportOrderCheck.ImportEntry> Imports of the current file in order of appearance.static final StringA key pointing to the warning message text in "messages.properties" file.static final StringA key pointing to the warning message text in "messages.properties" file.static final StringA key pointing to the warning message text in "messages.properties" file.private ModuleImportOrderOptionSpecify policy on the position of module imports relative to type and static imports.private booleanControl whether the module import block should be separated from type and static imports by, at least, one blank line or comment. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.private voidChecks that module imports are sorted lexicographically.private booleanChecks that module imports are placed above or below all type and static imports, according to the configured option.private voidChecks that the module import block is separated from the adjacent type and static import block by, at least, one blank line or comment.voidfinishTree(DetailAST rootAST) Called after finished processing a tree.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.voidSetter to specify policy on the position of module imports relative to type and static imports.voidsetSeparated(boolean separated) Setter to control whether the module import block should be separated from type and static imports by, at least, one blank line or comment.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_POSITION
A key pointing to the warning message text in "messages.properties" file. Emitted when a module import is not placed above or below all type and static imports, as required by the configured option.- See Also:
-
MSG_SEPARATION
A key pointing to the warning message text in "messages.properties" file. Emitted when the module import block is not separated from type and static imports by a blank line.- See Also:
-
MSG_ORDERING_LEX
A key pointing to the warning message text in "messages.properties" file. Emitted when module imports are not sorted lexicographically by qualified module name.- See Also:
-
imports
Imports of the current file in order of appearance. -
option
Specify policy on the position of module imports relative to type and static imports. -
separated
Control whether the module import block should be separated from type and static imports by, at least, one blank line or comment.
-
-
Constructor Details
-
ModuleImportOrderCheck
public ModuleImportOrderCheck()Creates a newModuleImportOrderCheckinstance.
-
-
Method Details
-
setOption
Setter to specify policy on the position of module imports relative to type and static imports.- Parameters:
optionStr- string to decode option from- Throws:
IllegalArgumentException- if unable to decode- Since:
- 14.1.0
-
setSeparated
Setter to control whether the module import block should be separated from type and static imports by, at least, one blank line or comment.- Parameters:
separated- whether the module import block should be separated.- Since:
- 14.1.0
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
finishTree
Description copied from class:AbstractCheckCalled after finished processing a tree. Ideal place to report on information collected whilst processing a tree.- Overrides:
finishTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
checkLexicographicalOrder
Checks that module imports are sorted lexicographically. Each module import is compared with the previous module import, regardless of any type or static imports between them. -
checkPosition
Checks that module imports are placed above or below all type and static imports, according to the configured option.- Returns:
- true if any position violation was logged.
-
checkSeparation
Checks that the module import block is separated from the adjacent type and static import block by, at least, one blank line or comment. This method is only invoked when module imports are correctly positioned, so all module imports form a single block above or below all other imports.
-