Class UnusedImportsCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class UnusedImportsCheck
    extends AbstractCheck

    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*;. 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.List would 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.)
    • Property processJavadoc - Control whether to process Javadoc comments. Type is boolean. Default value is true.

    Parent is

    Violation Message Keys:

    • import.unused
    • Method Detail

      • setProcessJavadoc

        public void setProcessJavadoc​(boolean value)
        Setter to control whether to process Javadoc comments.
        value - Flag for processing Javadoc comments.
      • 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.
        beginTree in class AbstractCheck
        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.
        finishTree in class AbstractCheck
        rootAST - the root of the tree
      • 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.
        Specified by:
        getAcceptableTokens in class AbstractCheck
        the token set this check is designed for.
        See Also:
      • isUnusedImport

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

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

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

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

        private void processStaticImport​(DetailAST ast)
        Collects the details of static imports.
        ast - node containing the static import details
      • collectReferencesFromJavadoc

        private void collectReferencesFromJavadoc​(DetailAST ast)
        Collects references made in Javadoc comments.
        ast - node to inspect for Javadoc
      • collectReferencesFromJavadoc

        private static Set<StringcollectReferencesFromJavadoc​(TextBlock textBlock)
        Process a javadoc TextBlock and return the set of classes referenced within.
        textBlock - The javadoc block to parse
        a set of classes referenced in the javadoc block
      • processJavadocTag

        private static Set<StringprocessJavadocTag​(JavadocTag tag)
        Returns a list of references that found in a javadoc JavadocTag.
        tag - The javadoc tag to parse
        A list of references that found in this tag
      • matchPattern

        private static Set<StringmatchPattern​(String identifier,
                                                Pattern pattern)
        Extracts a set of texts matching a Pattern from a String.
        identifier - The String to match the pattern against
        pattern - The Pattern used to extract the texts
        A set of texts which matched the pattern
      • 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.
        type - A possibly qualified type name
        The simple name of the top level type