AvoidStarImport

Description

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.

Properties

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

Examples

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:

<module name="AvoidStarImport">
   <property name="excludes" value="java.io,java.net,java.lang.Math"/>
   <property name="allowClassImports" value="false"/>
   <property name="allowStaticMemberImports" value="false"/>
</module>
        

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker

AvoidStaticImport

Description

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).

Properties

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

Examples

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:

         <module name="AvoidStaticImport">
           <property name="excludes" value="java.lang.System.out,java.lang.Math.*"/>
         </module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker

CustomImportOrder

Description

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

Rule Description

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:

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; //#8
        

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.

Notes

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:

  1. STATIC has top priority
  2. SAME_PACKAGE has second priority
  3. STANDARD_JAVA_PACKAGE and SPECIAL_IMPORTS will compete using "best match" rule: longer matching substring wins; in case of the same length, lower position of matching substring wins; if position is the same, order of rules in configuration solves the puzzle.
  4. THIRD_PARTY has the least priority

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 then "Check" position.

Properties

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

Examples

To configure the check so that it matches default Eclipse formatter configuration (tested on Kepler and Luna releases):

  • group of static imports is on the top
  • groups of non-static imports: "java" and "javax" packages first, then "org" and then all other imports
  • imports will be sorted in the groups
  • groups are separated by, at least, one blank line

Notes:

  • "com" package is not mentioned on configuration, because it is ignored by Eclipse Kepler and Luna (looks like Eclipse defect)
  • configuration below doesn't work in all 100% cases due to inconsistent behavior prior to Mars release, but covers most scenarios
<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):

  • group of static imports is on the top
  • groups of non-static imports: "java" and "javax" packages first, then "org" and "com", then all other imports as one group
  • imports will be sorted in the groups
  • groups are separated by, at least, one blank line
<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):

  • group of static imports is on the bottom
  • groups of non-static imports: all imports except of "javax" and "java", then "javax" and "java"
  • imports will be sorted in the groups
  • groups are separated by, at least, one blank line

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):

  • groups of non-static imports are not defined, all imports will be sorted as a one group
  • static imports are not separated, they will be sorted along with other imports
<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>
       

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker

IllegalImport

Description

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.

Properties

name description type default value
illegalPkgs packages to reject String Set sun

Examples

To configure the check:

<module name="IllegalImport"/>
        

To configure the check so that it rejects packages java.io.* and java.sql.*:

<module name="IllegalImport">
    <property name="illegalPkgs" value="java.io, java.sql"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker

ImportControl

Description

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://checkstyle.sourceforge.net/dtds/import_control_1_2.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.2//EN"
    "http://checkstyle.sourceforge.net/dtds/import_control_1_2.dtd">
        

Properties

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

Examples

To configure the check using a import control file called "config/import-control.xml", then have the following:

<module name="ImportControl">
    <property name="file" value="config/import-control.xml"/>
</module>
        

In the example below access to package com.puppycrawl.tools.checkstyle.checks and its subpackages is allowed from anywhere in com.puppycrawl.tools.checkstyle except from the filters subpackage where access to all check's subpackages is disallowed. Two java.lang.ref classes are allowed by virtue of one regular expression instead of listing them in two separate allow rules (as it is done with the Files and ClassPath classes).

<import-control pkg="com.puppycrawl.tools.checkstyle">
    <allow pkg="com.puppycrawl.tools.checkstyle.api"/>
    <allow pkg="com.puppycrawl.tools.checkstyle.checks"/>
    <allow class="com.google.common.io.Files"/>
    <allow class="com.google.common.reflect.ClassPath"/>
    <subpackage name="filters">
        <allow class="java\.lang\.ref\.(Weak|Soft)Reference"
            regex="true"/>
        <disallow pkg="com\.puppycrawl\.tools\.checkstyle\.checks\.[^.]+"
            regex="true"/>
        <disallow pkg="com.puppycrawl.tools.checkstyle.ant"/>
        <disallow pkg="com.puppycrawl.tools.checkstyle.doclets"/>
        <disallow pkg="com.puppycrawl.tools.checkstyle.gui"/>
    </subpackage>
</import-control>
        

In the next example regular expressions are used to enforce a layering rule: In all dao packages it is not allowed to access UI layer code (ui, awt, and swing). On the other hand it is not allowed to directly access dao and service layer from ui packages. The root package is also a regular expression that is used to handle old and new domain name with the same rules.

<import-control pkg="(de.olddomain|de.newdomain)\..*" regex="true">
    <subpackage pkg="[^.]+\.dao" regex="true">
        <disallow pkg=".*\.ui" regex="true"/>
        <disallow pkg=".*\.(awt|swing).\.*" regex="true"/>
    </subpackage>
    <subpackage pkg="[^.]+\.ui" regex="true">
        <disallow pkg=".*\.(dao|service)" regex="true"/>
    </subpackage>
</import-control>
        

For a real-life import control file look at the file called import-control.xml which is part of the Checkstyle distribution.

Notes on regular expressions

Regular expressions in import rules have to match either Java packages or classes. The language rules for packages and class names can be described by the following complicated regular expression that takes into account that Java names may contain any unicode letter, numbers, underscores, and dollar signs (see section 3.8 in the Java specs):

  • [\p{Letter}_$][\p{Letter}\p{Number}_$]* or short [\p{L}_$][\p{L}\p{N}_$]* for a class name or package component.
  • ([\p{L}_$][\p{L}\p{N}_$]*\.)*[\p{L}_$][\p{L}\p{N}_$]* for a fully qualified name.

But it is not necessary to use these complicated expressions since no validation is required. Differentiating between package separator '.' and others is sufficient. Unfortunately '.' has a special meaning in regular expressions so one has to write \. to match an actual dot.

  • Use [^.]+ (one or more "not a dot" characters) for a class name or package component.
  • Use com\.google\.common\.[^.]+ to match any subpackage of com.google.common.
  • When matching concrete packages like com.google.common omitting the backslash before the dots may improve readability and may be just exact enough: com.google.common\.[^.]+ matches not only subpackages of com.google.common but e.g. also of com.googleecommon but you may not care for that.
  • Do not use .* unless you really do not care for what is matched. Often you want to match only a certain package level instead.

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker

ImportOrder

Description

Checks the ordering/grouping of imports. Features are:

  • groups imports: ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes second, then everything else)
  • adds a separation between groups : ensures that a blank line sit between each group
  • sorts imports inside each group: ensures that imports within each group are in lexicographic order
  • sorts according to case: ensures that the comparison between imports is case sensitive, in ASCII sort order
  • groups static imports: ensures the relative order between regular imports and static imports (see import orders)

Examples section contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans

Properties

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
useContainerOrderingForStatic whether to use container ordering (Eclipse IDE term) for static imports or not Boolean false
tokens tokens to check subset of tokens STATIC_IMPORT. STATIC_IMPORT.

Examples

To configure the check so that it matches default Eclipse formatter configuration (tested on Kepler and Luna releases):

  • group of static imports is on the top
  • groups of non-static imports: "java" and "javax" packages first, then "org" and then all other imports
  • imports will be sorted in the groups
  • groups are separated by, at least, one blank line

Notes:

  • "com" package is not mentioned on configuration, because it is ignored by Eclipse Kepler and Luna (looks like Eclipse defect)
  • configuration below doesn't work in all 100% cases due to inconsistent behavior prior to Mars release, but covers most scenarios
<module name="ImportOrder">
    <property name="groups" value="/^javax?\./,org"/>
    <property name="ordered" value="true"/>
    <property name="separated" value="true"/>
    <property name="option" value="above"/>
    <property name="sortStaticImportsAlphabetically" value="true"/>
</module>
        

To configure the check so that it matches default Eclipse formatter configuration (tested on Mars release):

  • group of static imports is on the top
  • groups of non-static imports: "java" and "javax" packages first, then "org" and "com", then all other imports as one group
  • imports will be sorted in the groups
  • groups are separated by, at least, one blank line
<module name="ImportOrder">
    <property name="groups" value="/^javax?\./,org,com"/>
    <property name="ordered" value="true"/>
    <property name="separated" value="true"/>
    <property name="option" value="above"/>
    <property name="sortStaticImportsAlphabetically" value="true"/>
</module>
        

To configure the check so that it matches default IntelliJ IDEA formatter configuration (tested on v14):

  • group of static imports is on the bottom
  • groups of non-static imports: all imports except of "javax" and "java", then "javax" and "java"
  • imports will be sorted in the groups
  • groups are separated by, at least, one blank line

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="ImportOrder">
    <property name="groups" value="*,javax,java"/>
    <property name="ordered" value="true"/>
    <property name="separated" value="false"/>
    <property name="option" value="bottom"/>
    <property name="sortStaticImportsAlphabetically" value="true"/>
</module>
        

To configure the check so that it matches default NetBeans formatter configuration (tested on v8):

  • groups of non-static imports are not defined, all imports will be sorted as a one group
  • static imports are not separated, they will be sorted along with other imports
<module name="ImportOrder">
    <property name="option" value="inflow"/>
</module>
        

To configure the Check allows static imports grouped to the top being sorted alphabetically:

<module name="ImportOrder">
    <property name="sortStaticImportsAlphabetically" value="true"/>
    <property name="option" value="top"/>
</module>
        
import static java.lang.Math.PI;
import static java.lang.Math.abs; // OK, alphabetical case sensitive ASCII order, 'P' < 'a'
import static org.abego.treelayout.Configuration.AlignmentInLevel; // OK, alphabetical order

import org.abego.*;

import java.util.Set; //  Wrong order for 'java.util.Set' import.

public class SomeClass { ... }
        

The following example shows the idea of 'useContainerOrderingForStatic' option that is useful for Eclipse IDE users to match ordering validation. This is how the import comparison works for static imports: we first compare the container of the static import, container is the type enclosing the static element being imported. When the result of the comparison is 0 (containers are equal), we compare the fully qualified import names. For e.g. this is what is considered to be container names for the given example: import static HttpConstants.COLON => HttpConstants import static HttpHeaders.addHeader => HttpHeaders import static HttpHeaders.setHeader => HttpHeaders import static HttpHeaders.Names.DATE => HttpHeaders.Names According to this logic, HttpHeaders.Names should come after HttpHeaders.

<module name="ImportOrder">
    <property name="useContainerOrderingForStatic" value="true"/>
    <property name="ordered" value="true"/>
    <property name="option" value="top"/>
    <property name="caseSensitive" value="false"/>
    <property name="sortStaticImportsAlphabetically" value="true"/>
</module>
        
import static io.netty.handler.codec.http.HttpConstants.COLON;
import static io.netty.handler.codec.http.HttpHeaders.addHeader;
import static io.netty.handler.codec.http.HttpHeaders.setHeader;
import static io.netty.handler.codec.http.HttpHeaders.Names.DATE;

public class InputEclipseStaticImportsOrder { }
        
<module name="ImportOrder">
    <property name="useContainerOrderingForStatic" value="false"/>
    <property name="ordered" value="true"/>
    <property name="option" value="top"/>
    <property name="caseSensitive" value="false"/>
    <property name="sortStaticImportsAlphabetically" value="true"/>
</module>
        
import static io.netty.handler.codec.http.HttpConstants.COLON;
import static io.netty.handler.codec.http.HttpHeaders.addHeader;
import static io.netty.handler.codec.http.HttpHeaders.setHeader;
import static io.netty.handler.codec.http.HttpHeaders.Names.DATE; // violation

public class InputEclipseStaticImportsOrder { }
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker

RedundantImport

Description

Checks for redundant import statements. An import statement is considered redundant if:

  • It is a duplicate of another import. This is, when a class is imported more than once.
  • The class non-statically imported is from the java.lang package, e.g. importing java.lang.String.
  • The class non-statically imported is from the same package as the current package.

Examples

To configure the check:

<module name="RedundantImport"/>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker

UnusedImports

Description

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:

  • It is not referenced in the file. The algorithm does not support wild-card imports like import java.io.*;. Most IDE's provide very sophisticated checks for imports that handle wild-card imports.
  • It is a duplicate of another import. This is when a class is imported more than once.
  • The class imported is from the java.lang package. For example importing java.lang.String.
  • The class imported is from the same package.
  • Optionally: it is referenced in Javadoc comments. This check is on by default, but it is considered bad practice to introduce a compile time dependency for documentation purposes only. As an example, the import 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:

import java.awt.Component;
class FooBar {
    private Object Component; // a bad practice in my opinion
    ...
}
        

Properties

name description type default value
processJavadoc whether to process Javadoc Boolean true

Examples

To configure the check:

<module name="UnusedImports"/>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker