1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2021 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.Collection; | |
24 | import java.util.HashSet; | |
25 | import java.util.List; | |
26 | import java.util.Set; | |
27 | import java.util.regex.Matcher; | |
28 | import java.util.regex.Pattern; | |
29 | ||
30 | import com.puppycrawl.tools.checkstyle.FileStatefulCheck; | |
31 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
32 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
33 | import com.puppycrawl.tools.checkstyle.api.FileContents; | |
34 | import com.puppycrawl.tools.checkstyle.api.FullIdent; | |
35 | import com.puppycrawl.tools.checkstyle.api.TextBlock; | |
36 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
37 | import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag; | |
38 | import com.puppycrawl.tools.checkstyle.utils.CommonUtil; | |
39 | import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; | |
40 | import com.puppycrawl.tools.checkstyle.utils.TokenUtil; | |
41 | ||
42 | /** | |
43 | * <p> | |
44 | * Checks for unused import statements. Checkstyle uses a simple but very | |
45 | * reliable algorithm to report on unused import statements. An import statement | |
46 | * is considered unused if: | |
47 | * </p> | |
48 | * <ul> | |
49 | * <li> | |
50 | * It is not referenced in the file. The algorithm does not support wild-card | |
51 | * imports like {@code import java.io.*;}. Most IDE's provide very sophisticated | |
52 | * checks for imports that handle wild-card imports. | |
53 | * </li> | |
54 | * <li> | |
55 | * It is a duplicate of another import. This is when a class is imported more | |
56 | * than once. | |
57 | * </li> | |
58 | * <li> | |
59 | * The class imported is from the {@code java.lang} package. For example | |
60 | * importing {@code java.lang.String}. | |
61 | * </li> | |
62 | * <li> | |
63 | * The class imported is from the same package. | |
64 | * </li> | |
65 | * <li> | |
66 | * <b>Optionally:</b> it is referenced in Javadoc comments. This check is on by | |
67 | * default, but it is considered bad practice to introduce a compile time | |
68 | * dependency for documentation purposes only. As an example, the import | |
69 | * {@code java.util.List} would be considered referenced with the Javadoc | |
70 | * comment {@code {@link List}}. The alternative to avoid introducing a compile | |
71 | * time dependency would be to write the Javadoc comment as {@code {@link java.util.List}}. | |
72 | * </li> | |
73 | * </ul> | |
74 | * <p> | |
75 | * The main limitation of this check is handling the case where an imported type | |
76 | * has the same name as a declaration, such as a member variable. | |
77 | * </p> | |
78 | * <p> | |
79 | * For example, in the following case the import {@code java.awt.Component} | |
80 | * will not be flagged as unused: | |
81 | * </p> | |
82 | * <pre> | |
83 | * import java.awt.Component; | |
84 | * class FooBar { | |
85 | * private Object Component; // a bad practice in my opinion | |
86 | * ... | |
87 | * } | |
88 | * </pre> | |
89 | * <ul> | |
90 | * <li> | |
91 | * Property {@code processJavadoc} - Control whether to process Javadoc comments. | |
92 | * Type is {@code boolean}. | |
93 | * Default value is {@code true}. | |
94 | * </li> | |
95 | * </ul> | |
96 | * <p> | |
97 | * To configure the check: | |
98 | * </p> | |
99 | * <pre> | |
100 | * <module name="UnusedImports"/> | |
101 | * </pre> | |
102 | * <p> | |
103 | * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} | |
104 | * </p> | |
105 | * <p> | |
106 | * Violation Message Keys: | |
107 | * </p> | |
108 | * <ul> | |
109 | * <li> | |
110 | * {@code import.unused} | |
111 | * </li> | |
112 | * </ul> | |
113 | * | |
114 | * @since 3.0 | |
115 | */ | |
116 | @FileStatefulCheck | |
117 | public class UnusedImportsCheck extends AbstractCheck { | |
118 | ||
119 | /** | |
120 | * A key is pointing to the warning message text in "messages.properties" | |
121 | * file. | |
122 | */ | |
123 | public static final String MSG_KEY = "import.unused"; | |
124 | ||
125 | /** Regex to match class names. */ | |
126 | private static final Pattern CLASS_NAME = CommonUtil.createPattern( | |
127 | "((:?[\\p{L}_$][\\p{L}\\p{N}_$]*\\.)*[\\p{L}_$][\\p{L}\\p{N}_$]*)"); | |
128 | /** Regex to match the first class name. */ | |
129 | private static final Pattern FIRST_CLASS_NAME = CommonUtil.createPattern( | |
130 | "^" + CLASS_NAME); | |
131 | /** Regex to match argument names. */ | |
132 | private static final Pattern ARGUMENT_NAME = CommonUtil.createPattern( | |
133 | "[(,]\\s*" + CLASS_NAME.pattern()); | |
134 | ||
135 | /** Regexp pattern to match java.lang package. */ | |
136 | private static final Pattern JAVA_LANG_PACKAGE_PATTERN = | |
137 | CommonUtil.createPattern("^java\\.lang\\.[a-zA-Z]+$"); | |
138 | ||
139 | /** Suffix for the star import. */ | |
140 | private static final String STAR_IMPORT_SUFFIX = ".*"; | |
141 | ||
142 | /** Set of the imports. */ | |
143 |
1
1. <init> : removed call to java/util/HashSet::<init> → KILLED |
private final Set<FullIdent> imports = new HashSet<>(); |
144 | ||
145 | /** Flag to indicate when time to start collecting references. */ | |
146 | private boolean collect; | |
147 | /** Control whether to process Javadoc comments. */ | |
148 | private boolean processJavadoc = true; | |
149 | ||
150 | /** | |
151 | * The scope is being processed. | |
152 | * Types declared in a scope can shadow imported types. | |
153 | */ | |
154 | private Frame currentFrame; | |
155 | ||
156 | /** | |
157 | * Setter to control whether to process Javadoc comments. | |
158 | * | |
159 | * @param value Flag for processing Javadoc comments. | |
160 | */ | |
161 | public void setProcessJavadoc(boolean value) { | |
162 | processJavadoc = value; | |
163 | } | |
164 | ||
165 | @Override | |
166 | public void beginTree(DetailAST rootAST) { | |
167 | collect = false; | |
168 | currentFrame = Frame.compilationUnit(); | |
169 |
1
1. beginTree : removed call to java/util/Set::clear → KILLED |
imports.clear(); |
170 | } | |
171 | ||
172 | @Override | |
173 | public void finishTree(DetailAST rootAST) { | |
174 |
1
1. finishTree : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::finish → KILLED |
currentFrame.finish(); |
175 | // loop over all the imports to see if referenced. | |
176 | imports.stream() | |
177 |
3
1. lambda$finishTree$0 : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::lambda$finishTree$0 → KILLED 2. lambda$finishTree$0 : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::lambda$finishTree$0 → KILLED 3. lambda$finishTree$0 : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
.filter(imprt -> isUnusedImport(imprt.getText())) |
178 |
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.getDetailAst(), MSG_KEY, imprt.getText())); |
179 | } | |
180 | ||
181 | @Override | |
182 | public int[] getDefaultTokens() { | |
183 |
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 getRequiredTokens(); |
184 | } | |
185 | ||
186 | @Override | |
187 | public int[] getRequiredTokens() { | |
188 |
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 new int[] { |
189 | TokenTypes.IDENT, | |
190 | TokenTypes.IMPORT, | |
191 | TokenTypes.STATIC_IMPORT, | |
192 | // Definitions that may contain Javadoc... | |
193 | TokenTypes.PACKAGE_DEF, | |
194 | TokenTypes.ANNOTATION_DEF, | |
195 | TokenTypes.ANNOTATION_FIELD_DEF, | |
196 | TokenTypes.ENUM_DEF, | |
197 | TokenTypes.ENUM_CONSTANT_DEF, | |
198 | TokenTypes.CLASS_DEF, | |
199 | TokenTypes.INTERFACE_DEF, | |
200 | TokenTypes.METHOD_DEF, | |
201 | TokenTypes.CTOR_DEF, | |
202 | TokenTypes.VARIABLE_DEF, | |
203 | TokenTypes.RECORD_DEF, | |
204 | TokenTypes.COMPACT_CTOR_DEF, | |
205 | // Tokens for creating a new frame | |
206 | TokenTypes.OBJBLOCK, | |
207 | TokenTypes.SLIST, | |
208 | }; | |
209 | } | |
210 | ||
211 | @Override | |
212 | public int[] getAcceptableTokens() { | |
213 |
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 getRequiredTokens(); |
214 | } | |
215 | ||
216 | @Override | |
217 | public void visitToken(DetailAST ast) { | |
218 | switch (ast.getType()) { | |
219 | case TokenTypes.IDENT: | |
220 |
3
1. visitToken : negated conditional → KILLED 2. visitToken : removed conditional - replaced equality check with false → KILLED 3. visitToken : removed conditional - replaced equality check with true → KILLED |
if (collect) { |
221 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::processIdent → KILLED |
processIdent(ast); |
222 | } | |
223 | break; | |
224 | case TokenTypes.IMPORT: | |
225 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::processImport → KILLED |
processImport(ast); |
226 | break; | |
227 | case TokenTypes.STATIC_IMPORT: | |
228 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::processStaticImport → KILLED |
processStaticImport(ast); |
229 | break; | |
230 | case TokenTypes.OBJBLOCK: | |
231 | case TokenTypes.SLIST: | |
232 | currentFrame = currentFrame.push(); | |
233 | break; | |
234 | default: | |
235 | collect = true; | |
236 |
3
1. visitToken : negated conditional → KILLED 2. visitToken : removed conditional - replaced equality check with false → KILLED 3. visitToken : removed conditional - replaced equality check with true → KILLED |
if (processJavadoc) { |
237 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::collectReferencesFromJavadoc → KILLED |
collectReferencesFromJavadoc(ast); |
238 | } | |
239 | break; | |
240 | } | |
241 | } | |
242 | ||
243 | @Override | |
244 | public void leaveToken(DetailAST ast) { | |
245 |
3
1. leaveToken : negated conditional → KILLED 2. leaveToken : removed conditional - replaced equality check with false → KILLED 3. leaveToken : removed conditional - replaced equality check with true → KILLED |
if (TokenUtil.isOfType(ast, TokenTypes.OBJBLOCK, TokenTypes.SLIST)) { |
246 | currentFrame = currentFrame.pop(); | |
247 | } | |
248 | } | |
249 | ||
250 | /** | |
251 | * Checks whether an import is unused. | |
252 | * | |
253 | * @param imprt an import. | |
254 | * @return true if an import is unused. | |
255 | */ | |
256 | private boolean isUnusedImport(String imprt) { | |
257 | final Matcher javaLangPackageMatcher = JAVA_LANG_PACKAGE_PATTERN.matcher(imprt); | |
258 |
5
1. isUnusedImport : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::isUnusedImport → KILLED 2. isUnusedImport : negated conditional → KILLED 3. isUnusedImport : removed conditional - replaced equality check with false → KILLED 4. isUnusedImport : removed conditional - replaced equality check with true → KILLED 5. isUnusedImport : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return !currentFrame.isReferencedType(CommonUtil.baseClassName(imprt)) |
259 |
3
1. isUnusedImport : negated conditional → KILLED 2. isUnusedImport : removed conditional - replaced equality check with false → KILLED 3. isUnusedImport : removed conditional - replaced equality check with true → KILLED |
|| javaLangPackageMatcher.matches(); |
260 | } | |
261 | ||
262 | /** | |
263 | * Collects references made by IDENT. | |
264 | * | |
265 | * @param ast the IDENT node to process | |
266 | */ | |
267 | private void processIdent(DetailAST ast) { | |
268 | final DetailAST parent = ast.getParent(); | |
269 | final int parentType = parent.getType(); | |
270 |
3
1. processIdent : negated conditional → KILLED 2. processIdent : removed conditional - replaced equality check with false → KILLED 3. processIdent : removed conditional - replaced equality check with true → KILLED |
if (TokenUtil.isTypeDeclaration(parentType)) { |
271 |
1
1. processIdent : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::addDeclaredType → KILLED |
currentFrame.addDeclaredType(ast.getText()); |
272 | } | |
273 |
9
1. processIdent : negated conditional → KILLED 2. processIdent : negated conditional → KILLED 3. processIdent : negated conditional → KILLED 4. processIdent : removed conditional - replaced equality check with false → KILLED 5. processIdent : removed conditional - replaced equality check with false → KILLED 6. processIdent : removed conditional - replaced equality check with false → KILLED 7. processIdent : removed conditional - replaced equality check with true → KILLED 8. processIdent : removed conditional - replaced equality check with true → KILLED 9. processIdent : removed conditional - replaced equality check with true → KILLED |
else if (parentType != TokenTypes.DOT && parentType != TokenTypes.METHOD_DEF |
274 |
3
1. processIdent : negated conditional → KILLED 2. processIdent : removed conditional - replaced equality check with false → KILLED 3. processIdent : removed conditional - replaced equality check with true → KILLED |
|| parentType == TokenTypes.DOT && ast.getNextSibling() != null) { |
275 |
1
1. processIdent : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::addReferencedType → KILLED |
currentFrame.addReferencedType(ast.getText()); |
276 | } | |
277 | } | |
278 | ||
279 | /** | |
280 | * Collects the details of imports. | |
281 | * | |
282 | * @param ast node containing the import details | |
283 | */ | |
284 | private void processImport(DetailAST ast) { | |
285 | final FullIdent name = FullIdent.createFullIdentBelow(ast); | |
286 |
3
1. processImport : negated conditional → KILLED 2. processImport : removed conditional - replaced equality check with false → KILLED 3. processImport : removed conditional - replaced equality check with true → KILLED |
if (!name.getText().endsWith(STAR_IMPORT_SUFFIX)) { |
287 | imports.add(name); | |
288 | } | |
289 | } | |
290 | ||
291 | /** | |
292 | * Collects the details of static imports. | |
293 | * | |
294 | * @param ast node containing the static import details | |
295 | */ | |
296 | private void processStaticImport(DetailAST ast) { | |
297 | final FullIdent name = | |
298 | FullIdent.createFullIdent( | |
299 | ast.getFirstChild().getNextSibling()); | |
300 |
3
1. processStaticImport : negated conditional → KILLED 2. processStaticImport : removed conditional - replaced equality check with false → KILLED 3. processStaticImport : removed conditional - replaced equality check with true → KILLED |
if (!name.getText().endsWith(STAR_IMPORT_SUFFIX)) { |
301 | imports.add(name); | |
302 | } | |
303 | } | |
304 | ||
305 | /** | |
306 | * Collects references made in Javadoc comments. | |
307 | * | |
308 | * @param ast node to inspect for Javadoc | |
309 | */ | |
310 | private void collectReferencesFromJavadoc(DetailAST ast) { | |
311 | final FileContents contents = getFileContents(); | |
312 | final int lineNo = ast.getLineNo(); | |
313 | final TextBlock textBlock = contents.getJavadocBefore(lineNo); | |
314 |
3
1. collectReferencesFromJavadoc : negated conditional → KILLED 2. collectReferencesFromJavadoc : removed conditional - replaced equality check with false → KILLED 3. collectReferencesFromJavadoc : removed conditional - replaced equality check with true → KILLED |
if (textBlock != null) { |
315 |
1
1. collectReferencesFromJavadoc : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::addReferencedTypes → KILLED |
currentFrame.addReferencedTypes(collectReferencesFromJavadoc(textBlock)); |
316 | } | |
317 | } | |
318 | ||
319 | /** | |
320 | * Process a javadoc {@link TextBlock} and return the set of classes | |
321 | * referenced within. | |
322 | * | |
323 | * @param textBlock The javadoc block to parse | |
324 | * @return a set of classes referenced in the javadoc block | |
325 | */ | |
326 | private static Set<String> collectReferencesFromJavadoc(TextBlock textBlock) { | |
327 |
1
1. collectReferencesFromJavadoc : removed call to java/util/ArrayList::<init> → KILLED |
final List<JavadocTag> tags = new ArrayList<>(); |
328 | // gather all the inline tags, like @link | |
329 | // INLINE tags inside BLOCKs get hidden when using ALL | |
330 | tags.addAll(getValidTags(textBlock, JavadocUtil.JavadocTagType.INLINE)); | |
331 | // gather all the block-level tags, like @throws and @see | |
332 | tags.addAll(getValidTags(textBlock, JavadocUtil.JavadocTagType.BLOCK)); | |
333 | ||
334 |
1
1. collectReferencesFromJavadoc : removed call to java/util/HashSet::<init> → KILLED |
final Set<String> references = new HashSet<>(); |
335 | ||
336 | tags.stream() | |
337 | .filter(JavadocTag::canReferenceImports) | |
338 |
1
1. collectReferencesFromJavadoc : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(tag -> references.addAll(processJavadocTag(tag))); |
339 |
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; |
340 | } | |
341 | ||
342 | /** | |
343 | * Returns the list of valid tags found in a javadoc {@link TextBlock}. | |
344 | * | |
345 | * @param cmt The javadoc block to parse | |
346 | * @param tagType The type of tags we're interested in | |
347 | * @return the list of tags | |
348 | */ | |
349 | private static List<JavadocTag> getValidTags(TextBlock cmt, | |
350 | JavadocUtil.JavadocTagType tagType) { | |
351 |
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 JavadocUtil.getJavadocTags(cmt, tagType).getValidTags(); |
352 | } | |
353 | ||
354 | /** | |
355 | * Returns a list of references found in a javadoc {@link JavadocTag}. | |
356 | * | |
357 | * @param tag The javadoc tag to parse | |
358 | * @return A list of references found in this tag | |
359 | */ | |
360 | private static Set<String> processJavadocTag(JavadocTag tag) { | |
361 |
1
1. processJavadocTag : removed call to java/util/HashSet::<init> → KILLED |
final Set<String> references = new HashSet<>(); |
362 | final String identifier = tag.getFirstArg().trim(); | |
363 | for (Pattern pattern : new Pattern[] | |
364 | {FIRST_CLASS_NAME, ARGUMENT_NAME}) { | |
365 | references.addAll(matchPattern(identifier, pattern)); | |
366 | } | |
367 |
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; |
368 | } | |
369 | ||
370 | /** | |
371 | * Extracts a list of texts matching a {@link Pattern} from a | |
372 | * {@link String}. | |
373 | * | |
374 | * @param identifier The String to match the pattern against | |
375 | * @param pattern The Pattern used to extract the texts | |
376 | * @return A list of texts which matched the pattern | |
377 | */ | |
378 | private static Set<String> matchPattern(String identifier, Pattern pattern) { | |
379 |
1
1. matchPattern : removed call to java/util/HashSet::<init> → KILLED |
final Set<String> references = new HashSet<>(); |
380 | final Matcher matcher = pattern.matcher(identifier); | |
381 |
3
1. matchPattern : negated conditional → KILLED 2. matchPattern : removed conditional - replaced equality check with false → KILLED 3. matchPattern : removed conditional - replaced equality check with true → KILLED |
while (matcher.find()) { |
382 | references.add(topLevelType(matcher.group(1))); | |
383 | } | |
384 |
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; |
385 | } | |
386 | ||
387 | /** | |
388 | * If the given type string contains "." (e.g. "Map.Entry"), returns the | |
389 | * top level type (e.g. "Map"), as that is what must be imported for the | |
390 | * type to resolve. Otherwise, returns the type as-is. | |
391 | * | |
392 | * @param type A possibly qualified type name | |
393 | * @return The simple name of the top level type | |
394 | */ | |
395 | private static String topLevelType(String type) { | |
396 | final String topLevelType; | |
397 | final int dotIndex = type.indexOf('.'); | |
398 |
3
1. topLevelType : negated conditional → KILLED 2. topLevelType : removed conditional - replaced equality check with false → KILLED 3. topLevelType : removed conditional - replaced equality check with true → KILLED |
if (dotIndex == -1) { |
399 | topLevelType = type; | |
400 | } | |
401 | else { | |
402 | topLevelType = type.substring(0, dotIndex); | |
403 | } | |
404 |
1
1. topLevelType : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck::topLevelType to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return topLevelType; |
405 | } | |
406 | ||
407 | /** | |
408 | * Holds the names of referenced types and names of declared inner types. | |
409 | */ | |
410 | private static final class Frame { | |
411 | ||
412 | /** Parent frame. */ | |
413 | private final Frame parent; | |
414 | ||
415 | /** Nested types declared in the current scope. */ | |
416 | private final Set<String> declaredTypes; | |
417 | ||
418 | /** Set of references - possibly to imports or locally declared types. */ | |
419 | private final Set<String> referencedTypes; | |
420 | ||
421 | /** | |
422 | * Private constructor. Use {@link #compilationUnit()} to create a new top-level frame. | |
423 | * | |
424 | * @param parent the parent frame | |
425 | */ | |
426 | private Frame(Frame parent) { | |
427 | this.parent = parent; | |
428 |
1
1. <init> : removed call to java/util/HashSet::<init> → KILLED |
declaredTypes = new HashSet<>(); |
429 |
1
1. <init> : removed call to java/util/HashSet::<init> → KILLED |
referencedTypes = new HashSet<>(); |
430 | } | |
431 | ||
432 | /** | |
433 | * Adds new inner type. | |
434 | * | |
435 | * @param type the type name | |
436 | */ | |
437 | public void addDeclaredType(String type) { | |
438 | declaredTypes.add(type); | |
439 | } | |
440 | ||
441 | /** | |
442 | * Adds new type reference to the current frame. | |
443 | * | |
444 | * @param type the type name | |
445 | */ | |
446 | public void addReferencedType(String type) { | |
447 | referencedTypes.add(type); | |
448 | } | |
449 | ||
450 | /** | |
451 | * Adds new inner types. | |
452 | * | |
453 | * @param types the type names | |
454 | */ | |
455 | public void addReferencedTypes(Collection<String> types) { | |
456 | referencedTypes.addAll(types); | |
457 | } | |
458 | ||
459 | /** | |
460 | * Filters out all references to locally defined types. | |
461 | * | |
462 | */ | |
463 | public void finish() { | |
464 | referencedTypes.removeAll(declaredTypes); | |
465 | } | |
466 | ||
467 | /** | |
468 | * Creates new inner frame. | |
469 | * | |
470 | * @return a new frame. | |
471 | */ | |
472 | public Frame push() { | |
473 |
2
1. push : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::<init> → KILLED 2. push : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::push to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new Frame(this); |
474 | } | |
475 | ||
476 | /** | |
477 | * Pulls all referenced types up, except those that are declared in this scope. | |
478 | * | |
479 | * @return the parent frame | |
480 | */ | |
481 | public Frame pop() { | |
482 |
1
1. pop : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::finish → KILLED |
finish(); |
483 |
1
1. pop : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::addReferencedTypes → KILLED |
parent.addReferencedTypes(referencedTypes); |
484 |
1
1. pop : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::pop to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return parent; |
485 | } | |
486 | ||
487 | /** | |
488 | * Checks whether this type name is used in this frame. | |
489 | * | |
490 | * @param type the type name | |
491 | * @return {@code true} if the type is used | |
492 | */ | |
493 | public boolean isReferencedType(String type) { | |
494 |
3
1. isReferencedType : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::isReferencedType → KILLED 2. isReferencedType : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::isReferencedType → KILLED 3. isReferencedType : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return referencedTypes.contains(type); |
495 | } | |
496 | ||
497 | /** | |
498 | * Creates a new top-level frame for the compilation unit. | |
499 | * | |
500 | * @return a new frame. | |
501 | */ | |
502 | public static Frame compilationUnit() { | |
503 |
2
1. compilationUnit : removed call to com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::<init> → KILLED 2. compilationUnit : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck$Frame::compilationUnit to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new Frame(null); |
504 | } | |
505 | ||
506 | } | |
507 | ||
508 | } | |
Mutations | ||
143 |
1.1 |
|
169 |
1.1 |
|
174 |
1.1 |
|
177 |
1.1 2.2 3.3 |
|
178 |
1.1 2.2 |
|
183 |
1.1 |
|
188 |
1.1 |
|
213 |
1.1 |
|
220 |
1.1 2.2 3.3 |
|
221 |
1.1 |
|
225 |
1.1 |
|
228 |
1.1 |
|
236 |
1.1 2.2 3.3 |
|
237 |
1.1 |
|
245 |
1.1 2.2 3.3 |
|
258 |
1.1 2.2 3.3 4.4 5.5 |
|
259 |
1.1 2.2 3.3 |
|
270 |
1.1 2.2 3.3 |
|
271 |
1.1 |
|
273 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 |
|
274 |
1.1 2.2 3.3 |
|
275 |
1.1 |
|
286 |
1.1 2.2 3.3 |
|
300 |
1.1 2.2 3.3 |
|
314 |
1.1 2.2 3.3 |
|
315 |
1.1 |
|
327 |
1.1 |
|
334 |
1.1 |
|
338 |
1.1 |
|
339 |
1.1 |
|
351 |
1.1 |
|
361 |
1.1 |
|
367 |
1.1 |
|
379 |
1.1 |
|
381 |
1.1 2.2 3.3 |
|
384 |
1.1 |
|
398 |
1.1 2.2 3.3 |
|
404 |
1.1 |
|
428 |
1.1 |
|
429 |
1.1 |
|
473 |
1.1 2.2 |
|
482 |
1.1 |
|
483 |
1.1 |
|
484 |
1.1 |
|
494 |
1.1 2.2 3.3 |
|
503 |
1.1 2.2 |