Class AvoidStarImportCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck
- All Implemented Interfaces:
Configurable,Contextualizable
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.
Notes:
Note that property excludes is not recursive, subpackages of excluded
packages are not automatically excluded.
- Since:
- 3.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanControl whether to allow starred class imports likeimport java.util.*;.private booleanControl whether to allow starred static member imports likeimport static org.junit.Assert.*;.Specify packages where starred class imports are allowed and classes where starred static member imports are allowed.static final StringA key is pointing to the warning message text in "messages.properties" file.private static final StringSuffix for the star import. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]The configurable token set.int[]Returns the default token a check is interested in.int[]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(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, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
STAR_IMPORT_SUFFIX
Suffix for the star import.- See Also:
-
excludes
Specify packages where starred class imports are allowed and classes where starred static member imports are allowed. -
allowClassImports
Control whether to allow starred class imports likeimport java.util.*;. -
allowStaticMemberImports
Control whether to allow starred static member imports likeimport static org.junit.Assert.*;.
-
-
Constructor Details
-
AvoidStarImportCheck
public AvoidStarImportCheck()
-
-
Method Details
-
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:
-
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:
-
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:
-
setExcludes
Setter to specify packages where starred class imports are allowed and classes where starred static member imports are allowed.- Parameters:
excludesParam- package names/fully-qualifies class names where star imports are ok- Since:
- 3.2
-
setAllowClassImports
Setter to control whether to allow starred class imports likeimport java.util.*;.- Parameters:
allow- true to allow false to disallow- Since:
- 5.3
-
setAllowStaticMemberImports
Setter to control whether to allow starred static member imports likeimport static org.junit.Assert.*;.- Parameters:
allow- true to allow false to disallow- Since:
- 5.3
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
logsStarredImportViolation
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
-