Class ForbidCertainImportsCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.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.AbstractCheckForbids certain imports usage in certain packages.
You can configure this check using the following parameters:- Package qualified name regexp;
- Forbidden imports regexp;
- 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
-
-
Constructor Summary
Constructors Constructor Description ForbidCertainImportsCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)int[]getAcceptableTokens()int[]getDefaultTokens()StringgetForbiddenImportRegexp()Gets the regexp is used for matching forbidden imports.int[]getRequiredTokens()voidsetForbiddenImportsExcludesRegexp(String forbiddenImportsExcludesRegexp)Sets the regexp for excluding imports from checking.voidsetForbiddenImportsRegexp(String forbiddenImportsRegexp)Sets the regexp for matching forbidden imports.voidsetPackageNameRegexp(String packageNameRegexp)Sets the regexp for matching package fully qualified name.voidvisitToken(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
-
-
-
-
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
-
-
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:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
beginTree
public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)
- Overrides:
beginTreein classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-