AvoidStarImport
Since Checkstyle 3.0
Description
*
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.
Notes
Note that property excludes
is not recursive,
subpackages of excluded packages are not automatically excluded.
Properties
name | description | type | default value | since |
---|---|---|---|---|
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 |
excludes | Specify packages where starred class imports are allowed and classes where starred static member imports are allowed. | String[] | {} |
3.2 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="AvoidStarImport"/>
</module>
</module>
Example:
import java.util.Scanner;
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;
import java.io.*;
import static java.lang.Math.*;
import java.util.*; // violation
import java.net.*;
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;
import java.io.*;
import static java.lang.Math.*; // violation
import java.util.*;
import java.net.*;
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;
import java.io.*; // violation
import static java.lang.Math.*;
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;
import java.io.*;
import static java.lang.Math.*; // violation
import java.util.*;
import java.net.*;
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;
import java.io.*;
import static java.lang.Math.*;
import java.util.*; // violation
import java.net.*;
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.imports