UnusedImports
Since Checkstyle 3.0
Description
          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.langpackage. For example importingjava.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.Listwould be considered referenced with the Javadoc comment{@link List}. The alternative to avoid introducing a compile-time dependency would be to write the Javadoc comment as{@link java.util.List}. 
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.)
 
Properties
| name | description | type | default value | since | 
|---|---|---|---|---|
| processJavadoc | Control whether to process Javadoc comments. | boolean | true | 
              
5.4 | 
Examples
To configure the check:
<module name="Checker">
  <module name="TreeWalker">
    <module name="UnusedImports"/>
  </module>
</module>
Example:
// limitation as it match field name in code
import java.awt.Component;
// no ability to recognize what import is not used
import static java.util.Map.copyOf;
import static java.util.Arrays.copyOf;
import java.lang.String; // violation 'Unused import - java.lang.String.'
import java.util.Stack;
import java.util.Map;   // violation 'Unused import - java.util.Map.'
import java.util.List;
import java.util.function.Function;
import static java.lang.Integer.parseInt; // violation 'Unused import - java.lang.Integer.parseInt.'
/**
* {@link List}
*/
class Example1{
  Stack stack = new Stack();
  private Object Component;
  int[] arr = {0,0};
  int[] array = copyOf(arr , 1);
  Function <String, Integer> stringToInteger = Integer::parseInt;
}
To configure the check so that it ignores the imports referenced in Javadoc comments:
<module name="Checker">
  <module name="TreeWalker">
    <module name="UnusedImports">
      <property name="processJavadoc" value="false"/>
    </module>
  </module>
</module>
Example:
// limitation as it match field name in code
import java.awt.Component;
// no ability to recognize what import is not used
import static java.util.Map.copyOf;
import static java.util.Arrays.copyOf;
import java.lang.String; // violation 'Unused import - java.lang.String.'
import java.util.Stack;
import java.util.Map;   // violation 'Unused import - java.util.Map.'
import java.util.List; // violation 'Unused import - java.util.List.'
import java.util.function.Function;
import static java.lang.Integer.parseInt; // violation 'Unused import - java.lang.Integer.parseInt.'
/**
* {@link List}
*/
class Example2{
  Stack stack = new Stack();
  private Object Component;
  int[] arr = {0,0};
  int[] array = copyOf(arr , 1);
  Function <String, Integer> stringToInteger = Integer::parseInt;
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.imports






