# Imports

- About
- Checkstyle
- Documentation
- Release Notes
- Configuration
- Property Types
- Running
- Ant Task
- Command Line
- Available Checks
- Standard Checks
- Javadoc Comments
- Naming Conventions
- Headers
- Imports
- AvoidStarImport
- IllegalImport
- RedundantImport
- UnusedImports
- ImportOrder
- ImportControl
- Size Violations
- Whitespace
- Modifiers
- Block Checks
- Coding
- Class Design
- Duplicate Code
- Metrics
- Miscellaneous
- Optional Checks
- J2EE Checks
- Extending Checkstyle
- Writing checks
- Writing filters
- Writing listeners
- Javadoc
- SourceForge
- Mailing Lists
- Bug Database
- Feature Requests
- Developers
- Project Page
- SVN Repository
- Contributing

- Hosted by:

## AvoidStarImport

### Description

Checks that there are no import statements that use the * notation.

Rationale: Importing all classes from a package leads to tight coupling between packages 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. | list of strings | empty list

### Example

An example how to configure the check so that star imports from java.io and java.net are allowed:

    <module name="AvoidStarImport">
       <property name="excludes" value="java.io,java.net"/>
    </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 100% Pure Java. To reject other packages, set property illegalPkgs to a list of the illegal packages.

### Properties

name | description | type | default value
--- | --- | --- | ---
illegalPkgs | packages to reject | list of strings | 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>

### 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 imported is from the java.lang package, e.g. importing java.lang.String.
- The class imported is from the same package.

### Example

To configure the check:

    <module name="RedundantImport"/>

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

### Example

To configure the check:

    <module name="UnusedImports"/>

### Package

com.puppycrawl.tools.checkstyle.checks.imports

### Parent Module

TreeWalker

## ImportOrder

### Description

Checks the ordering/grouping of imports. Ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes first, then everything else) and imports within each group are in lexicographic order. Static imports must be at the end of a group and in lexicographic order amongst themselves.

### Properties

name | description | type | default value
--- | --- | --- | ---
groups | list of imports groups (every group identified by string it's started) | list of strings | 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 comparision should be case sensitive or not | Boolean | true

### Example

To configure the check so that it requires "java" packages first, than "javax" and than all other imports, imports will be sorted in the groups and groups are separated by, at least, on blank line:

    <module name="ImportOrder">
        <property name="groups" value="java,javax"/>
        <property name="ordered" value="true"/>
        <property name="separated" value="true"/>
    </module>

### 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://www.puppycrawl.com/dtds/import_control_1_0.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.0//EN"
        "http://www.puppycrawl.com/dtds/import_control_1_0.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

### Example

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

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

For an example import control file, look at the file called import-control.xml which is part of the Checkstyle distribution.

### Package

com.puppycrawl.tools.checkstyle.checks.imports

### Parent Module

TreeWalker

Copyright © 2001-2007, Oliver Burn