Class ForbidCertainImportsCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class ForbidCertainImportsCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    Forbids certain imports usage in certain packages.

    You can configure this check using the following parameters:
    1. Package qualified name regexp;
    2. Forbidden imports regexp;
    3. Forbidden imports excludes regexp.

    This check loads packages qualified names without words "package","import" and semicolons, so, please, do NOT include "package" or "import" words (or semicolons) into config regexps.

    Real-life example of usage: forbid to use all "*.ui.*" packages in "*.dao.*" packages, but ignore all Exception imports (such as org.springframework.dao.InvalidDataAccessResourceUsageException). For doing that, you should to use the following check parameters:

    • Package name regexp = ".*ui.*"
    • Forbidden imports regexp = ".*dao.*"
    • Forbidden imports excludes regexp = "^.+Exception$"

    You can cover more sophisticated rules by means of few check instances.

    Since:
    1.8.0
    Author:
    Daniil Yaroslavtsev
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      The key is pointing to the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)  
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      String getForbiddenImportRegexp()
      Gets the regexp is used for matching forbidden imports.
      int[] getRequiredTokens()  
      void setForbiddenImportsExcludesRegexp​(String forbiddenImportsExcludesRegexp)
      Sets the regexp for excluding imports from checking.
      void setForbiddenImportsRegexp​(String forbiddenImportsRegexp)
      Sets the regexp for matching forbidden imports.
      void setPackageNameRegexp​(String packageNameRegexp)
      Sets the regexp for matching package fully qualified name.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_KEY

        public static final String MSG_KEY
        The key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ForbidCertainImportsCheck

        public ForbidCertainImportsCheck()
    • Method Detail

      • setPackageNameRegexp

        public void setPackageNameRegexp​(String packageNameRegexp)
        Sets the regexp for matching package fully qualified name.
        Parameters:
        packageNameRegexp - regexp for package fully qualified name matching.
      • getForbiddenImportRegexp

        public String getForbiddenImportRegexp()
        Gets the regexp is used for matching forbidden imports.
        Returns:
        regexp for forbidden imports matching.
      • setForbiddenImportsRegexp

        public void setForbiddenImportsRegexp​(String forbiddenImportsRegexp)
        Sets the regexp for matching forbidden imports.
        Parameters:
        forbiddenImportsRegexp - regexp for matching forbidden imports.
      • setForbiddenImportsExcludesRegexp

        public void setForbiddenImportsExcludesRegexp​(String forbiddenImportsExcludesRegexp)
        Sets the regexp for excluding imports from checking.
        Parameters:
        forbiddenImportsExcludesRegexp - String contains a regexp for excluding imports from checking.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • beginTree

        public void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)
        Overrides:
        beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck