Class AvoidStarImportCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
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
excludesis not recursive, subpackages of excluded packages are not automatically excluded.-
Property
excludes- Specify packages where starred class imports are allowed and classes where starred static member imports are allowed. Type isjava.lang.String[]. Default value is"". -
Property
allowClassImports- Control whether to allow starred class imports likeimport java.util.*;. Type isboolean. Default value isfalse. -
Property
allowStaticMemberImports- Control whether to allow starred static member imports likeimport static org.junit.Assert.*;. Type isboolean. Default value isfalse.
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.netas well as static members from classjava.lang.Mathare 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
To configure the check so that star imports from packages
java.io and java.netare allowed:<module name="AvoidStarImport"> <property name="allowClassImports" value="true"/> <property name="excludes" value="java.io,java.net"/> </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.netas well as static members imports from all packages are allowed:<module name="AvoidStarImport"> <property name="allowStaticMemberImports" value="true"/> <property name="excludes" value="java.io,java.net"/> </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
Parent is
com.puppycrawl.tools.checkstyle.TreeWalkerViolation Message Keys:
-
import.avoidStar
- Since:
- 3.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallowClassImportsControl whether to allow starred class imports likeimport java.util.*;.private booleanallowStaticMemberImportsControl whether to allow starred static member imports likeimport static org.junit.Assert.*;.private java.util.List<java.lang.String>excludesSpecify packages where starred class imports are allowed and classes where starred static member imports are allowed.static java.lang.StringMSG_KEYA key is pointing to the warning message text in "messages.properties" file.private static java.lang.StringSTAR_IMPORT_SUFFIXSuffix for the star import.
-
Constructor Summary
Constructors Constructor Description AvoidStarImportCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.private voidlogsStarredImportViolation(DetailAST startingDot)Gets the full import identifier.voidsetAllowClassImports(boolean allow)Setter to control whether to allow starred class imports likeimport java.util.*;.voidsetAllowStaticMemberImports(boolean allow)Setter to control whether to allow starred static member imports likeimport static org.junit.Assert.*;.voidsetExcludes(java.lang.String... excludesParam)Setter to specify packages where starred class imports are allowed and classes where starred static member imports are allowed.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
STAR_IMPORT_SUFFIX
private static final java.lang.String STAR_IMPORT_SUFFIX
Suffix for the star import.- See Also:
- Constant Field Values
-
excludes
private final java.util.List<java.lang.String> excludes
Specify packages where starred class imports are allowed and classes where starred static member imports are allowed.
-
allowClassImports
private boolean allowClassImports
Control whether to allow starred class imports likeimport java.util.*;.
-
allowStaticMemberImports
private boolean allowStaticMemberImports
Control whether to allow starred static member imports likeimport static org.junit.Assert.*;.
-
-
Constructor Detail
-
AvoidStarImportCheck
public AvoidStarImportCheck()
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
setExcludes
public void setExcludes(java.lang.String... excludesParam)
Setter to specify packages where starred class imports are allowed and classes where starred static member imports are allowed.- Parameters:
excludesParam- a list of package names/fully-qualifies class names where star imports are ok.
-
setAllowClassImports
public void setAllowClassImports(boolean allow)
Setter to control whether to allow starred class imports likeimport java.util.*;.- Parameters:
allow- true to allow false to disallow
-
setAllowStaticMemberImports
public void setAllowStaticMemberImports(boolean allow)
Setter to control whether to allow starred static member imports likeimport static org.junit.Assert.*;.- Parameters:
allow- true to allow false to disallow
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
logsStarredImportViolation
private void logsStarredImportViolation(DetailAST startingDot)
Gets the full import identifier. If the import is a starred import and it's not excluded then a violation is logged.- Parameters:
startingDot- the starting dot for the import statement
-
-