RedundantImport
Since Checkstyle 3.0
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. importingjava.lang.String
. - The class non-statically imported is from the same package as the current package.
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="RedundantImport"/>
</module>
</module>
Example:
import static com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.Example1.*; // OK, static import
import static java.lang.Integer.MAX_VALUE; // OK, static import
// violation below, 'Redundant import from the same package'
import com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.Example1;
import java.lang.String; // violation, "Redundant import from the java.lang package"
import java.util.Scanner;
import java.util.Scanner; // violation 'Duplicate import to line 18 - java.util.Scanner'
public class Example1{ }
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