Checks that there are no import statements that use the * notation.
Rationale: Importing all classes from a package or static members from a class leads to tight coupling between packages or classes and might lead to problems when a new version of a library introduces name clashes.
name | description | type | default value |
---|---|---|---|
excludes | packages where star imports are allowed. Note that this property is not recursive, subpackages of excluded packages are not automatically excluded. | String Set | empty list |
allowClassImports |
whether to allow starred class imports like
import java.util.*; .
|
Boolean | false |
allowStaticMemberImports |
whether to allow starred static member imports like
import static org.junit.Assert.*;
|
Boolean | false |
An example how to configure the check so that star imports from packages java.io and java.net as well as static members from class from java.lang.Math are allowed:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports
Checks that there are no static import statements.
Rationale: Importing static members can lead to naming conflicts between class' members. It may lead to poor code readability since it may no longer be clear what class a member resides in (without looking at the import statement).
name | description | type | default value |
---|---|---|---|
excludes |
Allows for certain classes via a star notation to be
excluded such as java.lang.Math.* or specific static
members to be excluded like java.lang.System.out for a variable or
java.lang.Math.random for a
method.
If you exclude a starred import on a class this automatically excludes each member individually. For example: Excluding java.lang.Math.* . will allow the
import of each static member in the Math class
individually like java.lang.Math.PI .
|
String Set | empty list |
An example of how to configure the check so that the java.lang.System.out
member and all
members from java.lang.Math
are
allowed:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports
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.
Examples section contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans
The rule consists of:
1) STATIC group. This group sets the ordering of static imports.
2) SAME_PACKAGE(n) group. This group sets the ordering of the same package imports. Imports are considered on SAME_PACKAGE group if n first domains in package name and import name are identical. For example:
If 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.
3) 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.
4) STANDARD_JAVA_PACKAGE group. This group sets ordering of standard java/javax imports.
5) SPECIAL_IMPORTS group. This group may contains some imports that have particular meaning for the user.
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:
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:
Result: import will be assigned to SPECIAL_IMPORTS. Matching substring length is 5 for both patterns. However, "Avoid" position is lower then "Check" position.
name | description | type | default value |
---|---|---|---|
customImportOrderRules | List of order declaration customizing by user. | string | null |
standardPackageRegExp | RegExp for STANDARD_JAVA_PACKAGE group imports. | regular expression | ^(java|javax)\. |
thirdPartyPackageRegExp | RegExp for THIRDPARTY_PACKAGE group imports. | regular expression | .* |
specialImportsRegExp | RegExp for SPECIAL_IMPORTS group imports. | regular expression | ^$ |
separateLineBetweenGroups | Force empty line separator between import groups. | Boolean | true |
sortImportsInGroupAlphabetically | Force grouping alphabetically, in ASCII sort order. | Boolean | false |
To configure the check so that it matches default Eclipse formatter configuration (tested on Kepler and Luna releases):
Notes:
To configure the check so that it matches default Eclipse formatter configuration (tested on Mars release):
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"
To configure the check so that it matches default NetBeans formatter configuration (tested on v8):
To set RegExps for THIRD_PARTY_PACKAGE and STANDARD_JAVA_PACKAGE groups use thirdPartyPackageRegExp and standardPackageRegExp options.
Also, this check can be configured to force empty line separator between import groups. For example.
It is possible to enforce ASCII sort order of imports in groups using the following configuration:
Example of ASCII order:
To force checking imports sequence such as:
configure as follows:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports
Checks for imports from a set of illegal packages. By default, the
check rejects all sun.*
packages since
programs that contain direct calls to the sun.*
packages are "not guaranteed
to work on all Java-compatible platforms". To reject other packages, set property illegalPkgs
to a list of the illegal packages.
name | description | type | default value |
---|---|---|---|
illegalPkgs | packages to reject | String Set | sun |
To configure the check:
To configure the check so that it rejects packages java.io.*
and java.sql.*
:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports
Controls what can be imported in each package. Useful for ensuring that application layering rules are not violated, especially on large projects.
The DTD for a import control XML document is at http://www.puppycrawl.com/dtds/import_control_1_1.dtd. It contains documentation on each of the elements and attributes.
The check validates a XML document when it loads the document. To validate against the above DTD, include the following document type declaration in your XML document:
<!DOCTYPE import-control PUBLIC "-//Puppy Crawl//DTD Import Control 1.1//EN" "http://www.puppycrawl.com/dtds/import_control_1_1.dtd">
name | description | type | default value |
---|---|---|---|
file | name of the file containing the import control configuration. | string | null |
url | URL of the file containing the import control configuration. | string | null |
To configure the check using a import control file called "config/import-control.xml", then have the following:
In the example below, all classes beginning with an I in the package java.awt are allowed. In the package java.io only the classes File and InputStream are allowed.
For an example import control file, look at the file called import-control.xml which is part of the Checkstyle distribution.
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports
Checks the ordering/grouping of imports. Features are:
Examples section contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans
name | description | type | default value |
---|---|---|---|
option | policy on the relative order between regular imports and static imports | import order | under |
groups |
list of imports groups (every group identified either by a
common prefix string, or by a regular expression enclosed
in forward slashes (e.g. /regexp/ )
|
String Set | empty list |
ordered | whether imports within group should be sorted | Boolean | true |
separated | whether imports groups should be separated by, at least, one blank line | Boolean | false |
caseSensitive | whether string comparison should be case sensitive or not. Case sensitive sorting is in ASCII sort order | Boolean | true |
sortStaticImportsAlphabetically | whether static imports grouped by top or bottom option are sorted alphabetically or not | Boolean | false |
tokens | tokens to check | subset of tokens STATIC_IMPORT. | STATIC_IMPORT. |
To configure the check so that it matches default Eclipse formatter configuration (tested on Kepler and Luna releases):
Notes:
To configure the check so that it matches default Eclipse formatter configuration (tested on Mars release):
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"
To configure the check so that it matches default NetBeans formatter configuration (tested on v8):
To configure the Check allows static imports grouped to the top being sorted alphabetically:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports
Checks for redundant import statements. An import statement is considered redundant if:
java.lang
package, e.g. importing java.lang.String
.
To configure the check:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports
Checks for unused import statements. Checkstyle uses a simple but very reliable algorithm to report on unused import statements. An import statement is considered unused if:
import
java.io.*;
. Most IDE's provide very sophisticated checks
for imports that handle wild-card imports.
java.lang
package. For example importing java.lang.String
.
java.util.Date
would be
considered referenced with the Javadoc comment
{@link Date}
. The alternative to avoid introducing a
compile time dependency would be to write the Javadoc comment as
{@link java.util.Date}
.
The main limitation of this check is handling the case where an imported type has the same name as a declaration, such as a member variable.
For example, in the following case the import java.awt.Component
will not be flagged as
unused:
name | description | type | default value |
---|---|---|---|
processJavadoc | whether to process Javadoc | Boolean | true |
To configure the check:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports