Since Checkstyle 3.0
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.
name | description | type | default value | since |
---|---|---|---|---|
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. | String[] | {} |
7.8 |
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. | String[] | sun |
3.0 |
regexp | Control whether the illegalPkgs and illegalClasses should be interpreted as regular expressions. |
boolean | false |
7.8 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalImport"/> </module> </module>
The following example shows class with no illegal imports
import java.io.*; import java.lang.ArithmeticException; import java.sql.Connection; import java.util.List; import java.util.Enumeration; import java.util.Arrays; import java.util.Date; import sun.applet.*; // violation, 'Illegal import' public class Example1 {}
To configure the check so that it rejects packages java.io.*
and java.sql.*
:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalImport"> <property name="illegalPkgs" value="java.io, java.sql"/> </module> </module> </module>
The following example shows class with two illegal imports
import java.io.*; // violation, 'Illegal import' import java.lang.ArithmeticException; import java.sql.Connection; // violation, 'Illegal import' import java.util.List; import java.util.Enumeration; import java.util.Arrays; import java.util.Date; import sun.applet.*; public class Example2 {}
To configure the check so that it rejects classes java.util.Date
and
java.sql.Connection
:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalImport"> <property name="illegalClasses" value="java.util.Date, java.sql.Connection"/> </module> </module> </module>
The following example shows class with two illegal imports
import java.io.*; import java.lang.ArithmeticException; import java.sql.Connection; // violation, 'Illegal import' import java.util.List; import java.util.Enumeration; import java.util.Arrays; import java.util.Date; // violation, 'Illegal import' import sun.applet.*; // violation, 'Illegal import' public class Example3 {}
To configure the check so that it rejects packages not satisfying to regular
expression java\.util
:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalImport"> <property name="regexp" value="true"/> <property name="illegalPkgs" value="java\.util"/> </module> </module> </module>
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, 'Illegal import' import java.util.Enumeration; // violation, 'Illegal import' import java.util.Arrays; // violation, 'Illegal import' import java.util.Date; // violation, 'Illegal import' import sun.applet.*; public class Example4 {}
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="Checker"> <module name="TreeWalker"> <module name="IllegalImport"> <property name="regexp" value="true"/> <property name="illegalClasses" value="^java\.util\.(List|Arrays), ^java\.sql\.Connection"/> </module> </module> </module>
The following example shows class with three illegal imports
import java.io.*; import java.lang.ArithmeticException; import java.sql.Connection; // violation, 'Illegal import' import java.util.List; // violation, 'Illegal import' import java.util.Enumeration; import java.util.Arrays; // violation, 'Illegal import' import java.util.Date; import sun.applet.*; // violation, 'Illegal import' public class Example5 {}
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports