public class AvoidStarImportCheck extends AbstractCheck
Check that finds 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.
An example of how to configure the check is:
<module name="AvoidStarImport"> <property name="excludes" value="java.io,java.net,java.lang.Math"/> <property name="allowClassImports" value="false"/> <property name="allowStaticMemberImports" value="false"/> </module>The optional "excludes" property allows for certain packages like java.io or java.net to be exempted from the rule. It also is used to allow certain classes like java.lang.Math or java.io.File to be excluded in order to support static member imports.
The optional "allowClassImports" when set to true, will allow starred class imports but will not affect static member imports.
The optional "allowStaticMemberImports" when set to true will allow starred static member imports but will not affect class imports.
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)
Sets whether or not to allow all non-static class imports.
|
void |
setAllowStaticMemberImports(boolean allow)
Sets whether or not to allow all static member imports.
|
void |
setExcludes(String... excludesParam)
Sets the list of packages or classes to be exempt from the check.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setClassLoader, 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)
allow
- true to allow false to disallowpublic void setAllowStaticMemberImports(boolean allow)
allow
- true to allow false to disallowpublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processCopyright © 2001–2018. All rights reserved.