public class AvoidStarImportCheck extends AbstractCheck
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.
excludes
- Specify packages where star imports are allowed.
Type is java.lang.String[]
.
Default value is {}
.
allowClassImports
- Control whether to allow starred class
imports like import java.util.*;
.
Type is boolean
.
Default value is false
.
allowStaticMemberImports
- Control whether to allow starred
static member imports like import static org.junit.Assert.*;
.
Type is boolean
.
Default value is false
.
To configure the check:
<module name="AvoidStarImport"/>
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="AvoidStarImport"> <property name="excludes" value="java.io,java.net,java.lang.Math"/> </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="AvoidStarImport"> <property name="allowClassImports" value="true"/> </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="AvoidStarImport"> <property name="allowStaticMemberImports" value="true"/> </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
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
import.avoidStar
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
AvoidStarImportCheck() |
Modifier and Type | Method and 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.
|
void |
setAllowClassImports(boolean allow)
Setter to control whether to allow starred class imports like
import java.util.*; . |
void |
setAllowStaticMemberImports(boolean allow)
Setter to control whether to allow starred static member imports like
import static org.junit.Assert.*; . |
void |
setExcludes(String... excludesParam)
Setter to specify packages where star imports are allowed.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY
public AvoidStarImportCheck()
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void setExcludes(String... excludesParam)
excludesParam
- a list of package names/fully-qualifies class names
where star imports are ok.public void setAllowClassImports(boolean allow)
import java.util.*;
.allow
- true to allow false to disallowpublic void setAllowStaticMemberImports(boolean allow)
import static org.junit.Assert.*;
.allow
- true to allow false to disallowpublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processCopyright © 2001–2020. All rights reserved.