Checks for imports

Checkstyle Logo

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.

Example

To configure the check:

<module name="AvoidStarImport"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

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

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

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

Parent Module

TreeWalker


Copyright © 2002 Oliver Burn. All rights Reserved.