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 |
| maxAllowedStarImports | Control how many star imports are allowed. | int | 0 |
13.5.0 |
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, 'Using the '.*' form of import should be avoided.'
import static java.lang.Math.*; // violation, 'form of import should be avoided.'
import java.util.*; // violation, 'Using the '.*' form of import should be avoided.'
import java.net.*; // violation, 'Using the '.*' form of import should be avoided.'
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, 'Using the '.*' form of import should be avoided.'
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, 'form of import should be avoided.'
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, 'Using the '.*' form of import should be avoided.'
import static java.lang.Math.*;
import java.util.*; // violation, 'Using the '.*' form of import should be avoided.'
import java.net.*; // violation, 'Using the '.*' form of import should be avoided.'
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, 'form of import should be avoided.'
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, 'Using the '.*' form of import should be avoided.'
import java.net.*;
To configure the check according to OpenJDK guidelines, where at most one star import is allowed per file:
<module name="Checker">
<module name="TreeWalker">
<module name="AvoidStarImport">
<property name="maxAllowedStarImports" value="1"/>
</module>
</module>
</module>
Example:
import java.util.Scanner;
import java.io.*;
import static java.lang.Math.*; // violation, 'Only '1' star import is allowed per file.'
import java.util.*; // violation, 'Only '1' star import is allowed per file.'
import java.net.*; // violation, 'Only '1' star import is allowed per file.'
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.
Fully Qualified Name
com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck
Use this fully qualified class name in configuration when an exact class reference is required.






