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 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.
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 is java.lang.String[].
Default value is sun.
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 is java.lang.String[].
Default value is "".
regexp - Control whether the illegalPkgs and
illegalClasses should be interpreted as regular expressions.
Type is boolean.
Default value is false.
To configure the check:
<module name="IllegalImport"/>
To configure the check so that it rejects packages java.io.* and java.sql.*:
<module name="IllegalImport"> <property name="illegalPkgs" value="java.io, java.sql"/> </module>
The following example shows class with no illegal imports
import java.lang.ArithmeticException;
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import sun.applet.*;
public class InputIllegalImport { }
The following example shows class with two illegal imports
import java.io.*; // violation
import java.lang.ArithmeticException;
import java.sql.Connection; // violation
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import sun.applet.*;
public class InputIllegalImport { }
To configure the check so that it rejects classes java.util.Date and
java.sql.Connection:
<module name="IllegalImport">
<property name="illegalClasses"
value="java.util.Date, java.sql.Connection"/>
</module>
The following example shows class with no illegal imports
import java.io.*;
import java.lang.ArithmeticException;
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import sun.applet.*;
public class InputIllegalImport { }
The following example shows class with two illegal imports
import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection; // violation
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import java.util.Date; // violation
import sun.applet.*;
public class InputIllegalImport { }
To configure the check so that it rejects packages not satisfying to regular
expression java\.util:
<module name="IllegalImport"> <property name="regexp" value="true"/> <property name="illegalPkgs" value="java\.util"/> </module>
The following example shows class with no illegal imports
import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection;
import sun.applet.*;
public class InputIllegalImport { }
The following example shows class with four illegal imports
All four imports match "java\.util" regular expression
import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection;
import java.util.List; // violation
import java.util.Enumeration; // violation
import java.util.Arrays; // violation
import java.util.Date; // violation
import sun.applet.*;
public class InputIllegalImport { }
To configure the check so that it rejects class names not satisfying to regular
expression ^java\.util\.(List|Arrays) and ^java\.sql\.Connection:
<module name="IllegalImport">
<property name="regexp" value="true"/>
<property name="illegalClasses"
value="^java\.util\.(List|Arrays), ^java\.sql\.Connection"/>
</module>
The following example shows class with no illegal imports
import java.io.*;
import java.lang.ArithmeticException;
import java.util.Enumeration;
import java.util.Date;
import sun.applet.*;
public class InputIllegalImport { }
The following example shows class with three illegal imports
import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection; // violation
import java.util.List; // violation
import java.util.Enumeration;
import java.util.Arrays; // violation
import java.util.Date;
import sun.applet.*;
public class InputIllegalImport { }
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
import.illegal
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private java.lang.String[] |
illegalClasses
Specify class names to reject, if regexp property is not set,
checks if import equals class name.
|
private java.util.List<java.util.regex.Pattern> |
illegalClassesRegexps
The compiled regular expressions for classes.
|
private java.lang.String[] |
illegalPkgs
Specify packages to reject, if regexp property is not set, checks
if import is the part of package.
|
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
Control whether the
illegalPkgs and illegalClasses
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)
Setter to specify class names to reject, if regexp property is not
set, checks if import equals class name.
|
void |
setIllegalPkgs(java.lang.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 the
illegalPkgs and illegalClasses
should be interpreted as regular expressions. |
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic 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
illegalPkgs and illegalClasses
should be interpreted as regular expressions.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)
illegalPkgs and illegalClasses
should be interpreted as regular expressions.regexp - a Boolean valuepublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - 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 importCopyright © 2001-2022. All Rights Reserved.