Since Checkstyle 3.0
Checks that there are no import statements that use the *
notation.
Rationale: Importing all classes from a package or static members from a class leads to tight coupling between packages or classes and might lead to problems when a new version of a library introduces name clashes.
Note that property excludes
is not recursive,
subpackages of excluded packages are not automatically excluded.
name | description | type | default value | since |
---|---|---|---|---|
excludes | Specify packages where starred class imports are allowed and classes where starred static member imports are allowed. | String[] | {} |
3.2 |
allowClassImports |
Control whether to allow starred class imports like
import java.util.*; .
|
boolean | false |
5.3 |
allowStaticMemberImports |
Control whether to allow starred static member imports like
import static org.junit.Assert.*; .
|
boolean | false |
5.3 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="AvoidStarImport"/> </module> </module>
Example:
import java.util.Scanner; // OK import java.io.*; // violation import static java.lang.Math.*; // violation import java.util.*; // violation import java.net.*; // violation
To configure the check so that star imports from packages
java.io and java.net
as well as static members from class
java.lang.Math
are allowed:
<module name="Checker"> <module name="TreeWalker"> <module name="AvoidStarImport"> <property name="excludes" value="java.io,java.net,java.lang.Math"/> </module> </module> </module>
Example:
import java.util.Scanner; // OK import java.io.*; // OK import static java.lang.Math.*; // OK import java.util.*; // violation import java.net.*; // OK
To configure the check so that star imports from all packages are allowed:
<module name="Checker"> <module name="TreeWalker"> <module name="AvoidStarImport"> <property name="allowClassImports" value="true"/> </module> </module> </module>
Example:
import java.util.Scanner; // OK import java.io.*; // OK import static java.lang.Math.*; // violation import java.util.*; // OK import java.net.*; // OK
To configure the check so that starred static member imports from all packages are allowed:
<module name="Checker"> <module name="TreeWalker"> <module name="AvoidStarImport"> <property name="allowStaticMemberImports" value="true"/> </module> </module> </module>
Example:
import java.util.Scanner; // OK import java.io.*; // violation import static java.lang.Math.*; // OK import java.util.*; // violation import java.net.*; // violation
To configure the check so that star imports from packages
java.io and java.net
are allowed:
<module name="Checker"> <module name="TreeWalker"> <module name="AvoidStarImport"> <property name="allowClassImports" value="true"/> <property name="excludes" value="java.io,java.net"/> </module> </module> </module>
Example:
import java.util.Scanner; // OK import java.io.*; // OK import static java.lang.Math.*; // violation import java.util.*; // OK import java.net.*; // OK
To configure the check so that star imports from packages
java.io and java.net
as well as static members imports
from all packages are allowed:
<module name="Checker"> <module name="TreeWalker"> <module name="AvoidStarImport"> <property name="allowStaticMemberImports" value="true"/> <property name="excludes" value="java.io,java.net"/> </module> </module> </module>
Example:
import java.util.Scanner; // OK import java.io.*; // OK import static java.lang.Math.*; // OK import java.util.*; // violation import java.net.*; // OK
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