Class IllegalImportCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- 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 propertyillegalPkgsto a list of the illegal packages.-
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
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
regexp- Control whether theillegalPkgsandillegalClassesshould be interpreted as regular expressions. Type isboolean. Default value isfalse.
To configure the check:
<module name="IllegalImport"/>
To configure the check so that it rejects packages
java.io.*andjava.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
- java.io.*, illegalPkgs property contains this package
- java.sql.Connection is inside java.sql package
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.Dateandjava.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
- java.sql.Connection, illegalClasses property contains this class
- java.util.Date, illegalClasses property contains this class
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
- java.util.List
- java.util.Enumeration
- java.util.Arrays
- java.util.Date
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
- java.sql.Connection matches "^java\.sql\.Connection" regular expression
- java.util.List matches "^java\.util\.(List|Arrays)" regular expression
- java.util.Arrays matches "^java\.util\.(List|Arrays)" regular expression
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.TreeWalkerViolation Message Keys:
-
import.illegal
- Since:
- 3.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringMSG_KEYA key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description IllegalImportCheck()Creates a newIllegalImportCheckinstance.
-
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.voidsetIllegalClasses(java.lang.String... from)Setter to specify class names to reject, if regexp property is not set, checks if import equals class name.voidsetIllegalPkgs(java.lang.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, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, 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.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
setIllegalPkgs
public final 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. 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- array of illegal packages
-
setIllegalClasses
public 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. 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- array of illegal classes
-
setRegexp
public void setRegexp(boolean regexp)
Setter to control whether theillegalPkgsandillegalClassesshould be interpreted as regular expressions.- Parameters:
regexp- aBooleanvalue
-
getDefaultTokens
public int[] 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:
TokenTypes
-
getAcceptableTokens
public int[] 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:
TokenTypes
-
getRequiredTokens
public int[] 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:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
-