1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2017 the original author or authors. | |
4 | // | |
5 | // This library is free software; you can redistribute it and/or | |
6 | // modify it under the terms of the GNU Lesser General Public | |
7 | // License as published by the Free Software Foundation; either | |
8 | // version 2.1 of the License, or (at your option) any later version. | |
9 | // | |
10 | // This library is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | // Lesser General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU Lesser General Public | |
16 | // License along with this library; if not, write to the Free Software | |
17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | //////////////////////////////////////////////////////////////////////////////// | |
19 | ||
20 | package com.puppycrawl.tools.checkstyle.checks.imports; | |
21 | ||
22 | import java.util.ArrayList; | |
23 | import java.util.HashSet; | |
24 | import java.util.List; | |
25 | import java.util.Set; | |
26 | import java.util.regex.Matcher; | |
27 | import java.util.regex.Pattern; | |
28 | ||
29 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
30 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
31 | import com.puppycrawl.tools.checkstyle.api.FileContents; | |
32 | import com.puppycrawl.tools.checkstyle.api.FullIdent; | |
33 | import com.puppycrawl.tools.checkstyle.api.TextBlock; | |
34 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
35 | import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag; | |
36 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
37 | import com.puppycrawl.tools.checkstyle.utils.JavadocUtils; | |
38 | ||
39 | /** | |
40 | * <p> | |
41 | * Checks for unused import statements. | |
42 | * </p> | |
43 | * <p> | |
44 | * An example of how to configure the check is: | |
45 | * </p> | |
46 | * <pre> | |
47 | * <module name="UnusedImports"/> | |
48 | * </pre> | |
49 | * Compatible with Java 1.5 source. | |
50 | * | |
51 | * @author Oliver Burn | |
52 | */ | |
53 | public class UnusedImportsCheck extends AbstractCheck { | |
54 | ||
55 | /** | |
56 | * A key is pointing to the warning message text in "messages.properties" | |
57 | * file. | |
58 | */ | |
59 | public static final String MSG_KEY = "import.unused"; | |
60 | ||
61 | /** Regex to match class names. */ | |
62 | private static final Pattern CLASS_NAME = CommonUtils.createPattern( | |
63 | "((:?[\\p{L}_$][\\p{L}\\p{N}_$]*\\.)*[\\p{L}_$][\\p{L}\\p{N}_$]*)"); | |
64 | /** Regex to match the first class name. */ | |
65 | private static final Pattern FIRST_CLASS_NAME = CommonUtils.createPattern( | |
66 | "^" + CLASS_NAME); | |
67 | /** Regex to match argument names. */ | |
68 | private static final Pattern ARGUMENT_NAME = CommonUtils.createPattern( | |
69 | "[(,]\\s*" + CLASS_NAME.pattern()); | |
70 | ||
71 | /** Regexp pattern to match java.lang package. */ | |
72 | private static final Pattern JAVA_LANG_PACKAGE_PATTERN = | |
73 | CommonUtils.createPattern("^java\\.lang\\.[a-zA-Z]+$"); | |
74 | ||
75 | /** Suffix for the star import. */ | |
76 | private static final String STAR_IMPORT_SUFFIX = ".*"; | |
77 | ||
78 | /** Set of the imports. */ | |
79 | private final Set<FullIdent> imports = new HashSet<>(); | |
80 | ||
81 | /** Set of references - possibly to imports or other things. */ | |
82 | private final Set<String> referenced = new HashSet<>(); | |
83 | ||
84 | /** Flag to indicate when time to start collecting references. */ | |
85 | private boolean collect; | |
86 | /** Flag whether to process Javadoc comments. */ | |
87 | private boolean processJavadoc = true; | |
88 | ||
89 | /** | |
90 | * Sets whether to process JavaDoc or not. | |
91 | * | |
92 | * @param value Flag for processing JavaDoc. | |
93 | */ | |
94 | public void setProcessJavadoc(boolean value) { | |
95 | processJavadoc = value; | |
96 | } | |
97 | ||
98 | @Override | |
99 | public void beginTree(DetailAST rootAST) { | |
100 | collect = false; | |
101 |
1
1. beginTree : removed call to java/util/Set::clear → KILLED |
imports.clear(); |
102 |
1
1. beginTree : removed call to java/util/Set::clear → KILLED |
referenced.clear(); |
103 | } | |
104 | ||
105 | @Override | |
106 | public void finishTree(DetailAST rootAST) { | |
107 | // loop over all the imports to see if referenced. | |
108 | imports.stream() | |
109 |
1
1. lambda$finishTree$0 : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
.filter(imprt -> isUnusedImport(imprt.getText())) |
110 |
2
1. finishTree : removed call to java/util/stream/Stream::forEach → KILLED 2. lambda$finishTree$1 : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::log → KILLED |
.forEach(imprt -> log(imprt.getLineNo(), |
111 | imprt.getColumnNo(), | |
112 | MSG_KEY, imprt.getText())); | |
113 | } | |
114 | ||
115 | @Override | |
116 | public int[] getDefaultTokens() { | |
117 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
118 | TokenTypes.IDENT, | |
119 | TokenTypes.IMPORT, | |
120 | TokenTypes.STATIC_IMPORT, | |
121 | // Definitions that may contain Javadoc... | |
122 | TokenTypes.PACKAGE_DEF, | |
123 | TokenTypes.ANNOTATION_DEF, | |
124 | TokenTypes.ANNOTATION_FIELD_DEF, | |
125 | TokenTypes.ENUM_DEF, | |
126 | TokenTypes.ENUM_CONSTANT_DEF, | |
127 | TokenTypes.CLASS_DEF, | |
128 | TokenTypes.INTERFACE_DEF, | |
129 | TokenTypes.METHOD_DEF, | |
130 | TokenTypes.CTOR_DEF, | |
131 | TokenTypes.VARIABLE_DEF, | |
132 | }; | |
133 | } | |
134 | ||
135 | @Override | |
136 | public int[] getRequiredTokens() { | |
137 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getDefaultTokens(); |
138 | } | |
139 | ||
140 | @Override | |
141 | public int[] getAcceptableTokens() { | |
142 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
143 | TokenTypes.IDENT, | |
144 | TokenTypes.IMPORT, | |
145 | TokenTypes.STATIC_IMPORT, | |
146 | // Definitions that may contain Javadoc... | |
147 | TokenTypes.PACKAGE_DEF, | |
148 | TokenTypes.ANNOTATION_DEF, | |
149 | TokenTypes.ANNOTATION_FIELD_DEF, | |
150 | TokenTypes.ENUM_DEF, | |
151 | TokenTypes.ENUM_CONSTANT_DEF, | |
152 | TokenTypes.CLASS_DEF, | |
153 | TokenTypes.INTERFACE_DEF, | |
154 | TokenTypes.METHOD_DEF, | |
155 | TokenTypes.CTOR_DEF, | |
156 | TokenTypes.VARIABLE_DEF, | |
157 | }; | |
158 | } | |
159 | ||
160 | @Override | |
161 | public void visitToken(DetailAST ast) { | |
162 |
1
1. visitToken : negated conditional → KILLED |
if (ast.getType() == TokenTypes.IDENT) { |
163 |
1
1. visitToken : negated conditional → KILLED |
if (collect) { |
164 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::processIdent → KILLED |
processIdent(ast); |
165 | } | |
166 | } | |
167 |
1
1. visitToken : negated conditional → KILLED |
else if (ast.getType() == TokenTypes.IMPORT) { |
168 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::processImport → KILLED |
processImport(ast); |
169 | } | |
170 |
1
1. visitToken : negated conditional → KILLED |
else if (ast.getType() == TokenTypes.STATIC_IMPORT) { |
171 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::processStaticImport → KILLED |
processStaticImport(ast); |
172 | } | |
173 | else { | |
174 | collect = true; | |
175 |
1
1. visitToken : negated conditional → KILLED |
if (processJavadoc) { |
176 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::collectReferencesFromJavadoc → KILLED |
collectReferencesFromJavadoc(ast); |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | /** | |
182 | * Checks whether an import is unused. | |
183 | * @param imprt an import. | |
184 | * @return true if an import is unused. | |
185 | */ | |
186 | private boolean isUnusedImport(String imprt) { | |
187 | final Matcher javaLangPackageMatcher = JAVA_LANG_PACKAGE_PATTERN.matcher(imprt); | |
188 |
2
1. isUnusedImport : negated conditional → KILLED 2. isUnusedImport : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return !referenced.contains(CommonUtils.baseClassName(imprt)) |
189 |
1
1. isUnusedImport : negated conditional → KILLED |
|| javaLangPackageMatcher.matches(); |
190 | } | |
191 | ||
192 | /** | |
193 | * Collects references made by IDENT. | |
194 | * @param ast the IDENT node to process | |
195 | */ | |
196 | private void processIdent(DetailAST ast) { | |
197 | final DetailAST parent = ast.getParent(); | |
198 | final int parentType = parent.getType(); | |
199 |
3
1. processIdent : negated conditional → KILLED 2. processIdent : negated conditional → KILLED 3. processIdent : negated conditional → KILLED |
if (parentType != TokenTypes.DOT |
200 | && parentType != TokenTypes.METHOD_DEF | |
201 | || parentType == TokenTypes.DOT | |
202 |
1
1. processIdent : negated conditional → KILLED |
&& ast.getNextSibling() != null) { |
203 | referenced.add(ast.getText()); | |
204 | } | |
205 | } | |
206 | ||
207 | /** | |
208 | * Collects the details of imports. | |
209 | * @param ast node containing the import details | |
210 | */ | |
211 | private void processImport(DetailAST ast) { | |
212 | final FullIdent name = FullIdent.createFullIdentBelow(ast); | |
213 |
1
1. processImport : negated conditional → KILLED |
if (!name.getText().endsWith(STAR_IMPORT_SUFFIX)) { |
214 | imports.add(name); | |
215 | } | |
216 | } | |
217 | ||
218 | /** | |
219 | * Collects the details of static imports. | |
220 | * @param ast node containing the static import details | |
221 | */ | |
222 | private void processStaticImport(DetailAST ast) { | |
223 | final FullIdent name = | |
224 | FullIdent.createFullIdent( | |
225 | ast.getFirstChild().getNextSibling()); | |
226 |
1
1. processStaticImport : negated conditional → KILLED |
if (!name.getText().endsWith(STAR_IMPORT_SUFFIX)) { |
227 | imports.add(name); | |
228 | } | |
229 | } | |
230 | ||
231 | /** | |
232 | * Collects references made in Javadoc comments. | |
233 | * @param ast node to inspect for Javadoc | |
234 | */ | |
235 | private void collectReferencesFromJavadoc(DetailAST ast) { | |
236 | final FileContents contents = getFileContents(); | |
237 | final int lineNo = ast.getLineNo(); | |
238 | final TextBlock textBlock = contents.getJavadocBefore(lineNo); | |
239 |
1
1. collectReferencesFromJavadoc : negated conditional → KILLED |
if (textBlock != null) { |
240 | referenced.addAll(collectReferencesFromJavadoc(textBlock)); | |
241 | } | |
242 | } | |
243 | ||
244 | /** | |
245 | * Process a javadoc {@link TextBlock} and return the set of classes | |
246 | * referenced within. | |
247 | * @param textBlock The javadoc block to parse | |
248 | * @return a set of classes referenced in the javadoc block | |
249 | */ | |
250 | private static Set<String> collectReferencesFromJavadoc(TextBlock textBlock) { | |
251 | final List<JavadocTag> tags = new ArrayList<>(); | |
252 | // gather all the inline tags, like @link | |
253 | // INLINE tags inside BLOCKs get hidden when using ALL | |
254 | tags.addAll(getValidTags(textBlock, JavadocUtils.JavadocTagType.INLINE)); | |
255 | // gather all the block-level tags, like @throws and @see | |
256 | tags.addAll(getValidTags(textBlock, JavadocUtils.JavadocTagType.BLOCK)); | |
257 | ||
258 | final Set<String> references = new HashSet<>(); | |
259 | ||
260 | tags.stream() | |
261 | .filter(JavadocTag::canReferenceImports) | |
262 |
1
1. collectReferencesFromJavadoc : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(tag -> references.addAll(processJavadocTag(tag))); |
263 |
1
1. collectReferencesFromJavadoc : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::collectReferencesFromJavadoc to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return references; |
264 | } | |
265 | ||
266 | /** | |
267 | * Returns the list of valid tags found in a javadoc {@link TextBlock}. | |
268 | * @param cmt The javadoc block to parse | |
269 | * @param tagType The type of tags we're interested in | |
270 | * @return the list of tags | |
271 | */ | |
272 | private static List<JavadocTag> getValidTags(TextBlock cmt, | |
273 | JavadocUtils.JavadocTagType tagType) { | |
274 |
1
1. getValidTags : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::getValidTags to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return JavadocUtils.getJavadocTags(cmt, tagType).getValidTags(); |
275 | } | |
276 | ||
277 | /** | |
278 | * Returns a list of references found in a javadoc {@link JavadocTag}. | |
279 | * @param tag The javadoc tag to parse | |
280 | * @return A list of references found in this tag | |
281 | */ | |
282 | private static Set<String> processJavadocTag(JavadocTag tag) { | |
283 | final Set<String> references = new HashSet<>(); | |
284 | final String identifier = tag.getFirstArg().trim(); | |
285 |
3
1. processJavadocTag : changed conditional boundary → KILLED 2. processJavadocTag : Changed increment from 1 to -1 → KILLED 3. processJavadocTag : negated conditional → KILLED |
for (Pattern pattern : new Pattern[] |
286 | {FIRST_CLASS_NAME, ARGUMENT_NAME}) { | |
287 | references.addAll(matchPattern(identifier, pattern)); | |
288 | } | |
289 |
1
1. processJavadocTag : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::processJavadocTag to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return references; |
290 | } | |
291 | ||
292 | /** | |
293 | * Extracts a list of texts matching a {@link Pattern} from a | |
294 | * {@link String}. | |
295 | * @param identifier The String to match the pattern against | |
296 | * @param pattern The Pattern used to extract the texts | |
297 | * @return A list of texts which matched the pattern | |
298 | */ | |
299 | private static Set<String> matchPattern(String identifier, Pattern pattern) { | |
300 | final Set<String> references = new HashSet<>(); | |
301 | final Matcher matcher = pattern.matcher(identifier); | |
302 |
1
1. matchPattern : negated conditional → KILLED |
while (matcher.find()) { |
303 | references.add(matcher.group(1)); | |
304 | } | |
305 |
1
1. matchPattern : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::matchPattern to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return references; |
306 | } | |
307 | } | |
Mutations | ||
101 |
1.1 |
|
102 |
1.1 |
|
109 |
1.1 |
|
110 |
1.1 2.2 |
|
117 |
1.1 |
|
137 |
1.1 |
|
142 |
1.1 |
|
162 |
1.1 |
|
163 |
1.1 |
|
164 |
1.1 |
|
167 |
1.1 |
|
168 |
1.1 |
|
170 |
1.1 |
|
171 |
1.1 |
|
175 |
1.1 |
|
176 |
1.1 |
|
188 |
1.1 2.2 |
|
189 |
1.1 |
|
199 |
1.1 2.2 3.3 |
|
202 |
1.1 |
|
213 |
1.1 |
|
226 |
1.1 |
|
239 |
1.1 |
|
262 |
1.1 |
|
263 |
1.1 |
|
274 |
1.1 |
|
285 |
1.1 2.2 3.3 |
|
289 |
1.1 |
|
302 |
1.1 |
|
305 |
1.1 |