Class AvoidStarImportCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class AvoidStarImportCheck
    extends AbstractCheck
    Checks that there are no import statements that use the * notation.

    Rationale: Importing all classes from a package or static members from a class leads to tight coupling between packages or classes and might lead to problems when a new version of a library introduces name clashes.

    Note that property excludes is not recursive, subpackages of excluded packages are not automatically excluded.

    • Property allowClassImports - Control whether to allow starred class imports like import java.util.*;. Type is boolean. Default value is false.
    • Property allowStaticMemberImports - Control whether to allow starred static member imports like import static org.junit.Assert.*;. Type is boolean. Default value is false.
    • Property excludes - Specify packages where starred class imports are allowed and classes where starred static member imports are allowed. Type is java.lang.String[]. Default value is "".

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • import.avoidStar
    Since:
    3.0
    • Field Detail

      • excludes

        private final Set<String> excludes
        Specify packages where starred class imports are allowed and classes where starred static member imports are allowed.
      • allowClassImports

        private boolean allowClassImports
        Control whether to allow starred class imports like import java.util.*;.
      • allowStaticMemberImports

        private boolean allowStaticMemberImports
        Control whether to allow starred static member imports like import static org.junit.Assert.*;.
    • Method Detail

      • 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
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • setExcludes

        public void setExcludes​(String... excludesParam)
        Setter to specify packages where starred class imports are allowed and classes where starred static member imports are allowed.
        Parameters:
        excludesParam - package names/fully-qualifies class names where star imports are ok
        Since:
        3.2
      • setAllowClassImports

        public void setAllowClassImports​(boolean allow)
        Setter to control whether to allow starred class imports like import java.util.*;.
        Parameters:
        allow - true to allow false to disallow
        Since:
        5.3
      • setAllowStaticMemberImports

        public void setAllowStaticMemberImports​(boolean allow)
        Setter to control whether to allow starred static member imports like import static org.junit.Assert.*;.
        Parameters:
        allow - true to allow false to disallow
        Since:
        5.3
      • logsStarredImportViolation

        private void logsStarredImportViolation​(DetailAST startingDot)
        Gets the full import identifier. If the import is a starred import and it's not excluded then a violation is logged.
        Parameters:
        startingDot - the starting dot for the import statement