Class UnusedImportsCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks for 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.
  • The class imported is from the java.lang package. For example importing java.lang.String.
  • The class imported is from the same package.
  • A static method is imported when used as method reference. In that case, only the type needs to be imported and that's enough to resolve the method.
  • 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.Set would be considered referenced with the Javadoc comment {@link Set}. The alternative to avoid introducing a compile-time dependency would be to write the Javadoc comment as {@link Set}.

The main limitation of this check is handling the cases where:

  • An imported type has the same name as a declaration, such as a member variable.
  • There are two or more static imports with the same method name (javac can distinguish imports with same name but different parameters, but checkstyle can not due to limitation.)
  • Module import declarations are used. Checkstyle does not resolve modules and therefore cannot determine which packages or types are brought into scope by an import module declaration. See limitations.
Since:
3.0