public class IllegalImportCheck extends AbstractCheck
Checks for imports from a set of illegal packages.
By default, the check rejects all sun.*
packages
since programs that contain direct calls to the sun.*
packages
are
not 100% Pure Java.
To reject other packages, set property illegalPkgs to a comma-separated list of the illegal packages.
An example of how to configure the check is:
<module name="IllegalImport"/>
An example of how to configure the check so that it rejects packages
java.io.*
and java.sql.*
is
<module name="IllegalImport"> <property name="illegalPkgs" value="java.io, java.sql"/> </module> Compatible with Java 1.5 source.
Modifier and Type | Field and Description |
---|---|
private java.lang.String[] |
illegalClasses
List of illegal classes.
|
private java.util.List<java.util.regex.Pattern> |
illegalClassesRegexps
The compiled regular expressions for classes.
|
private java.lang.String[] |
illegalPkgs
List of illegal packages.
|
private java.util.List<java.util.regex.Pattern> |
illegalPkgsRegexps
The compiled regular expressions for packages.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private boolean |
regexp
Whether the packages or class names
should be interpreted as regular expressions.
|
Constructor and Description |
---|
IllegalImportCheck()
Creates a new
IllegalImportCheck instance. |
Modifier and Type | Method and 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(java.lang.String importText)
Checks if an import is from a package or class name that must not be used.
|
private boolean |
isIllegalImportByPackagesAndClassNames(java.lang.String importText)
Checks if an import is from a package or class name that must not be used.
|
private boolean |
isIllegalImportByRegularExpressions(java.lang.String importText)
Checks if an import matches one of the regular expressions
for illegal packages or illegal class names.
|
void |
setIllegalClasses(java.lang.String... from)
Set the list of illegal classes.
|
void |
setIllegalPkgs(java.lang.String... from)
Set the list of illegal packages.
|
void |
setRegexp(boolean regexp)
Controls whether the packages or class names
should be interpreted as regular expressions.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_KEY
private final java.util.List<java.util.regex.Pattern> illegalPkgsRegexps
private final java.util.List<java.util.regex.Pattern> illegalClassesRegexps
private java.lang.String[] illegalPkgs
private java.lang.String[] illegalClasses
private boolean regexp
public IllegalImportCheck()
IllegalImportCheck
instance.public final void setIllegalPkgs(java.lang.String... from)
from
- array of illegal packagespublic void setIllegalClasses(java.lang.String... from)
from
- array of illegal classespublic void setRegexp(boolean regexp)
regexp
- a Boolean
valuepublic int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate boolean isIllegalImportByRegularExpressions(java.lang.String importText)
importText
- the argument of the import keywordimportText
matches one of the regular expressions
for illegal packages or illegal class namesprivate boolean isIllegalImportByPackagesAndClassNames(java.lang.String importText)
importText
- the argument of the import keywordimportText
contains an illegal package prefix or equals illegal class nameprivate boolean isIllegalImport(java.lang.String importText)
importText
- the argument of the import keywordimportText
is illegal import