Class IllegalImportCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks for imports from a set of illegal packages.
Notes:
Note: By default, the check rejects all sun.* packages since programs
that contain direct calls to the sun.* packages are
"not guaranteed to work on all Java-compatible platforms". To reject other
packages, set property illegalPkgs to a list of the illegal packages.
- Since:
- 3.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String[]Specify class names to reject, if regexp property is not set, checks if import equals class name.The compiled regular expressions for classes.private String[]Specify packages to reject, if regexp property is not set, checks if import is the part of package.The compiled regular expressions for packages.static final StringA key is pointing to the warning message text in "messages.properties" file.private booleanControl whether theillegalPkgsandillegalClassesshould be interpreted as regular expressions. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private booleanisIllegalImport(String importText) Checks if an import is from a package or class name that must not be used.private booleanisIllegalImportByPackagesAndClassNames(String importText) Checks if an import is from a package or class name that must not be used.private booleanisIllegalImportByRegularExpressions(String importText) Checks if an import matches one of the regular expressions for illegal packages or illegal class names.voidsetIllegalClasses(String... from) Setter to specify class names to reject, if regexp property is not set, checks if import equals class name.final voidsetIllegalPkgs(String... from) Setter to specify packages to reject, if regexp property is not set, checks if import is the part of package.voidsetRegexp(boolean regexp) Setter to control whether theillegalPkgsandillegalClassesshould be interpreted as regular expressions.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
illegalPkgsRegexps
The compiled regular expressions for packages. -
illegalClassesRegexps
The compiled regular expressions for classes. -
illegalPkgs
Specify packages to reject, if regexp property is not set, checks if import is the part of package. If regexp property is set, then list of packages will be interpreted as regular expressions. Note, all properties for match will be used. -
illegalClasses
Specify class names to reject, if regexp property is not set, checks if import equals class name. If regexp property is set, then list of class names will be interpreted as regular expressions. Note, all properties for match will be used. -
regexp
Control whether theillegalPkgsandillegalClassesshould be interpreted as regular expressions.
-
-
Constructor Details
-
IllegalImportCheck
public IllegalImportCheck()Creates a newIllegalImportCheckinstance.
-
-
Method Details
-
setIllegalPkgs
Setter to specify packages to reject, if regexp property is not set, checks if import is the part of package. If regexp property is set, then list of packages will be interpreted as regular expressions. Note, all properties for match will be used.- Parameters:
from- illegal packages- Since:
- 3.0
-
setIllegalClasses
Setter to specify class names to reject, if regexp property is not set, checks if import equals class name. If regexp property is set, then list of class names will be interpreted as regular expressions. Note, all properties for match will be used.- Parameters:
from- illegal classes- Since:
- 7.8
-
setRegexp
Setter to control whether theillegalPkgsandillegalClassesshould be interpreted as regular expressions.- Parameters:
regexp- aBooleanvalue- Since:
- 7.8
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
isIllegalImportByRegularExpressions
Checks if an import matches one of the regular expressions for illegal packages or illegal class names.- Parameters:
importText- the argument of the import keyword- Returns:
- if
importTextmatches one of the regular expressions for illegal packages or illegal class names
-
isIllegalImportByPackagesAndClassNames
Checks if an import is from a package or class name that must not be used.- Parameters:
importText- the argument of the import keyword- Returns:
- if
importTextcontains an illegal package prefix or equals illegal class name
-
isIllegalImport
Checks if an import is from a package or class name that must not be used.- Parameters:
importText- the argument of the import keyword- Returns:
- if
importTextis illegal import
-