Class UnusedImportsCheck
- 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.UnusedImportsCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
public class UnusedImportsCheck extends AbstractCheck
Checks for unused import statements.
An example of how to configure the check is:
<module name="UnusedImports"/>
Compatible with Java 1.5 source.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.regex.PatternARGUMENT_NAMERegex to match argument names.private static java.util.regex.PatternCLASS_NAMERegex to match class names.private booleancollectFlag to indicate when time to start collecting references.private static java.util.regex.PatternFIRST_CLASS_NAMERegex to match the first class name.private java.util.Set<FullIdent>importsSet of the imports.private static java.util.regex.PatternJAVA_LANG_PACKAGE_PATTERNRegexp pattern to match java.lang package.static java.lang.StringMSG_KEYA key is pointing to the warning message text in "messages.properties" file.private booleanprocessJavadocFlag whether to process Javadoc comments.private java.util.Set<java.lang.String>referencedSet of references - possibly to imports or other things.private static java.lang.StringSTAR_IMPORT_SUFFIXSuffix for the star import.
-
Constructor Summary
Constructors Constructor Description UnusedImportsCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTree(DetailAST rootAST)Called before the starting to process a tree.private voidcollectReferencesFromJavadoc(DetailAST ast)Collects references made in Javadoc comments.private static java.util.Set<java.lang.String>collectReferencesFromJavadoc(TextBlock textBlock)Process a javadocTextBlockand return the set of classes referenced within.voidfinishTree(DetailAST rootAST)Called after finished processing a tree.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 static java.util.List<JavadocTag>getValidTags(TextBlock cmt, JavadocUtils.JavadocTagType tagType)Returns the list of valid tags found in a javadocTextBlock.private booleanisUnusedImport(java.lang.String imprt)Checks whether an import is unused.private static java.util.Set<java.lang.String>matchPattern(java.lang.String identifier, java.util.regex.Pattern pattern)Extracts a list of texts matching aPatternfrom aString.private voidprocessIdent(DetailAST ast)Collects references made by IDENT.private voidprocessImport(DetailAST ast)Collects the details of imports.private static java.util.Set<java.lang.String>processJavadocTag(JavadocTag tag)Returns a list of references found in a javadocJavadocTag.private voidprocessStaticImport(DetailAST ast)Collects the details of static imports.voidsetProcessJavadoc(boolean value)Sets whether to process JavaDoc or not.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
destroy, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, 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
-
CLASS_NAME
private static final java.util.regex.Pattern CLASS_NAME
Regex to match class names.
-
FIRST_CLASS_NAME
private static final java.util.regex.Pattern FIRST_CLASS_NAME
Regex to match the first class name.
-
ARGUMENT_NAME
private static final java.util.regex.Pattern ARGUMENT_NAME
Regex to match argument names.
-
JAVA_LANG_PACKAGE_PATTERN
private static final java.util.regex.Pattern JAVA_LANG_PACKAGE_PATTERN
Regexp pattern to match java.lang package.
-
STAR_IMPORT_SUFFIX
private static final java.lang.String STAR_IMPORT_SUFFIX
Suffix for the star import.- See Also:
- Constant Field Values
-
imports
private final java.util.Set<FullIdent> imports
Set of the imports.
-
referenced
private final java.util.Set<java.lang.String> referenced
Set of references - possibly to imports or other things.
-
collect
private boolean collect
Flag to indicate when time to start collecting references.
-
processJavadoc
private boolean processJavadoc
Flag whether to process Javadoc comments.
-
-
Method Detail
-
setProcessJavadoc
public void setProcessJavadoc(boolean value)
Sets whether to process JavaDoc or not.- Parameters:
value- Flag for processing JavaDoc.
-
beginTree
public void beginTree(DetailAST rootAST)
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
finishTree
public void finishTree(DetailAST rootAST)
Description copied from class:AbstractCheckCalled after finished processing a tree. Ideal place to report on information collected whilst processing a tree.- Overrides:
finishTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
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
-
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
-
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
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
isUnusedImport
private boolean isUnusedImport(java.lang.String imprt)
Checks whether an import is unused.- Parameters:
imprt- an import.- Returns:
- true if an import is unused.
-
processIdent
private void processIdent(DetailAST ast)
Collects references made by IDENT.- Parameters:
ast- the IDENT node to process
-
processImport
private void processImport(DetailAST ast)
Collects the details of imports.- Parameters:
ast- node containing the import details
-
processStaticImport
private void processStaticImport(DetailAST ast)
Collects the details of static imports.- Parameters:
ast- node containing the static import details
-
collectReferencesFromJavadoc
private void collectReferencesFromJavadoc(DetailAST ast)
Collects references made in Javadoc comments.- Parameters:
ast- node to inspect for Javadoc
-
collectReferencesFromJavadoc
private static java.util.Set<java.lang.String> collectReferencesFromJavadoc(TextBlock textBlock)
Process a javadocTextBlockand return the set of classes referenced within.- Parameters:
textBlock- The javadoc block to parse- Returns:
- a set of classes referenced in the javadoc block
-
getValidTags
private static java.util.List<JavadocTag> getValidTags(TextBlock cmt, JavadocUtils.JavadocTagType tagType)
Returns the list of valid tags found in a javadocTextBlock.- Parameters:
cmt- The javadoc block to parsetagType- The type of tags we're interested in- Returns:
- the list of tags
-
processJavadocTag
private static java.util.Set<java.lang.String> processJavadocTag(JavadocTag tag)
Returns a list of references found in a javadocJavadocTag.- Parameters:
tag- The javadoc tag to parse- Returns:
- A list of references found in this tag
-
matchPattern
private static java.util.Set<java.lang.String> matchPattern(java.lang.String identifier, java.util.regex.Pattern pattern)Extracts a list of texts matching aPatternfrom aString.- Parameters:
identifier- The String to match the pattern againstpattern- The Pattern used to extract the texts- Returns:
- A list of texts which matched the pattern
-
-