Class RedundantImportCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class RedundantImportCheck
    extends AbstractCheck

    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. importing java.lang.String.
    • The class non-statically imported is from the same package as the current package.

    To configure the check:

     <module name="RedundantImport"/>
     

    Example:

     package Test;
     import static Test.MyClass.*; // OK, static import
     import static java.lang.Integer.MAX_VALUE; // OK, static import
    
     import Test.MyClass; // violation, imported from the same package as the current package
     import java.lang.String; // violation, the class imported is from the 'java.lang' package
     import java.util.Scanner; // OK
     import java.util.Scanner; // violation, it is a duplicate of another import
     public class MyClass{ };
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • import.duplicate
    • import.lang
    • import.same
    Since:
    3.0