Class UnusedImportsCheck

All Implemented Interfaces:
Configurable, Contextualizable

public class UnusedImportsCheck extends AbstractJavadocCheck
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
  • Field Details

    • MSG_KEY

      public static final String MSG_KEY
      A key is pointing to the warning message text in "messages.properties" file.
      See Also:
    • JAVA_LANG_PACKAGE_PATTERN

      private static final Pattern JAVA_LANG_PACKAGE_PATTERN
      Regexp pattern to match java.lang package.
    • STAR_IMPORT_SUFFIX

      private static final String STAR_IMPORT_SUFFIX
      Suffix for the star import.
      See Also:
    • imports

      private final Set<FullIdent> imports
      Set of the imports.
    • processJavadoc

      private boolean processJavadoc
      Control whether to process Javadoc comments.
    • currentFrame

      private UnusedImportsCheck.Frame currentFrame
      The scope is being processed. Types declared in a scope can shadow imported types.
  • Constructor Details

    • UnusedImportsCheck

      public UnusedImportsCheck()
  • Method Details

    • setProcessJavadoc

      public void setProcessJavadoc(boolean value)
      Setter to control whether to process Javadoc comments.
      Parameters:
      value - Flag for processing Javadoc comments.
      Since:
      5.4
    • setViolateExecutionOnNonTightHtml

      public void setViolateExecutionOnNonTightHtml(boolean shouldReportViolation)
      Setter to control when to print violations if the Javadoc being examined by this check violates the tight html rules defined at Tight-HTML Rules.
      Overrides:
      setViolateExecutionOnNonTightHtml in class AbstractJavadocCheck
      Parameters:
      shouldReportViolation - value to which the field shall be set to
      Since:
      8.3
    • beginTree

      public void beginTree(DetailAST rootAST)
      Description copied from class: AbstractCheck
      Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.
      Overrides:
      beginTree in class AbstractJavadocCheck
      Parameters:
      rootAST - the root of the tree
    • finishTree

      public void finishTree(DetailAST rootAST)
      Description copied from class: AbstractCheck
      Called after finished processing a tree. Ideal place to report on information collected whilst processing a tree.
      Overrides:
      finishTree in class AbstractCheck
      Parameters:
      rootAST - the root of the tree
    • getRequiredJavadocTokens

      public int[] getRequiredJavadocTokens()
      Description copied from class: AbstractJavadocCheck
      The javadoc tokens that this check must be registered for.
      Overrides:
      getRequiredJavadocTokens in class AbstractJavadocCheck
      Returns:
      the javadoc token set this must be registered for.
      See Also:
    • getDefaultJavadocTokens

      public int[] getDefaultJavadocTokens()
      Description copied from class: AbstractJavadocCheck
      Returns the default javadoc token types a check is interested in.
      Specified by:
      getDefaultJavadocTokens in class AbstractJavadocCheck
      Returns:
      the default javadoc token types
      See Also:
    • visitJavadocToken

      public void visitJavadocToken(DetailNode ast)
      Description copied from class: AbstractJavadocCheck
      Called to process a Javadoc token.
      Specified by:
      visitJavadocToken in class AbstractJavadocCheck
      Parameters:
      ast - the token to process
    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Overrides:
      getDefaultTokens in class AbstractJavadocCheck
      Returns:
      the default tokens
      See Also:
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Description copied from class: AbstractCheck
      The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
      Overrides:
      getAcceptableTokens in class AbstractJavadocCheck
      Returns:
      the token set this check is designed for.
      See Also:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Overrides:
      getRequiredTokens in class AbstractJavadocCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractJavadocCheck
      Parameters:
      ast - the token to process
    • leaveToken

      public void leaveToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called after all the child nodes have been process.
      Overrides:
      leaveToken in class AbstractCheck
      Parameters:
      ast - the token leaving
    • isUnusedImport

      private boolean isUnusedImport(String imprt)
      Checks whether an import is unused.
      Parameters:
      imprt - an import.
      Returns:
      true if an import is unused.
    • processIdent

      private void processIdent(DetailAST ast)
      Collects references made by IDENT.
      Parameters:
      ast - the IDENT node to process
    • isQualifiedIdentifier

      private static boolean isQualifiedIdentifier(DetailAST ast)
      Checks whether ast is a fully qualified identifier.
      Parameters:
      ast - to check
      Returns:
      true if given ast is a fully qualified identifier
    • processImport

      private void processImport(DetailAST ast)
      Collects the details of imports.
      Parameters:
      ast - node containing the import details
    • processStaticImport

      private void processStaticImport(DetailAST ast)
      Collects the details of static imports.
      Parameters:
      ast - node containing the static import details
    • processReference

      private void processReference(DetailNode ast)
      Processes a Javadoc reference to record referenced types.
      Parameters:
      ast - the Javadoc reference node
    • processParameterType

      private void processParameterType(DetailNode ast)
      Processes a Javadoc parameter type tag to record referenced type.
      Parameters:
      ast - the Javadoc parameter type node
    • processException

      private void processException(DetailNode ast)
      Processes a Javadoc throws or exception tag to record referenced type.
      Parameters:
      ast - the Javadoc throws or exception node
    • topLevelType

      private static String topLevelType(String type)
      If the given type string contains "." (e.g. "Map.Entry"), returns the top level type (e.g. "Map"), as that is what must be imported for the type to resolve. Otherwise, returns the type as-is.
      Parameters:
      type - A possibly qualified type name
      Returns:
      The simple name of the top level type