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
public class IllegalImportCheck extends AbstractCheck
Checks for imports from a set of illegal packages.Note: By default, the check rejects all
sun.*
packages since programs that contain direct calls to thesun.*
packages are "not guaranteed to work on all Java-compatible platforms". To reject other packages, set propertyillegalPkgs
to a list of the illegal packages.-
Property
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. Type isjava.lang.String[]
. Default value is""
. -
Property
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. Type isjava.lang.String[]
. Default value issun
. -
Property
regexp
- Control whether theillegalPkgs
andillegalClasses
should be interpreted as regular expressions. Type isboolean
. Default value isfalse
.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
import.illegal
- Since:
- 3.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private String[]
illegalClasses
Specify class names to reject, if regexp property is not set, checks if import equals class name.private List<Pattern>
illegalClassesRegexps
The compiled regular expressions for classes.private String[]
illegalPkgs
Specify packages to reject, if regexp property is not set, checks if import is the part of package.private List<Pattern>
illegalPkgsRegexps
The compiled regular expressions for packages.static String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.private boolean
regexp
Control whether theillegalPkgs
andillegalClasses
should be interpreted as regular expressions.
-
Constructor Summary
Constructors Constructor Description IllegalImportCheck()
Creates a newIllegalImportCheck
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.private boolean
isIllegalImport(String importText)
Checks if an import is from a package or class name that must not be used.private boolean
isIllegalImportByPackagesAndClassNames(String importText)
Checks if an import is from a package or class name that must not be used.private boolean
isIllegalImportByRegularExpressions(String importText)
Checks if an import matches one of the regular expressions for illegal packages or illegal class names.void
setIllegalClasses(String... from)
Setter to specify class names to reject, if regexp property is not set, checks if import equals class name.void
setIllegalPkgs(String... from)
Setter to specify packages to reject, if regexp property is not set, checks if import is the part of package.void
setRegexp(boolean regexp)
Setter to control whether theillegalPkgs
andillegalClasses
should be interpreted as regular expressions.void
visitToken(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, 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.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
illegalPkgsRegexps
private final List<Pattern> illegalPkgsRegexps
The compiled regular expressions for packages.
-
illegalClassesRegexps
private final List<Pattern> illegalClassesRegexps
The compiled regular expressions for classes.
-
illegalPkgs
private String[] 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
private String[] 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
private boolean regexp
Control whether theillegalPkgs
andillegalClasses
should be interpreted as regular expressions.
-
-
Constructor Detail
-
IllegalImportCheck
public IllegalImportCheck()
Creates a newIllegalImportCheck
instance.
-
-
Method Detail
-
setIllegalPkgs
public final void setIllegalPkgs(String... from)
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
public void setIllegalClasses(String... from)
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
public void setRegexp(boolean regexp)
Setter to control whether theillegalPkgs
andillegalClasses
should be interpreted as regular expressions.- Parameters:
regexp
- aBoolean
value- Since:
- 7.8
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
isIllegalImportByRegularExpressions
private boolean isIllegalImportByRegularExpressions(String importText)
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
importText
matches one of the regular expressions for illegal packages or illegal class names
-
isIllegalImportByPackagesAndClassNames
private boolean isIllegalImportByPackagesAndClassNames(String importText)
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
importText
contains an illegal package prefix or equals illegal class name
-
isIllegalImport
private boolean isIllegalImport(String importText)
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
importText
is illegal import
-
-