1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2020 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.HashSet; | |
23 | import java.util.Set; | |
24 | ||
25 | import com.puppycrawl.tools.checkstyle.FileStatefulCheck; | |
26 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
27 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
28 | import com.puppycrawl.tools.checkstyle.api.FullIdent; | |
29 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
30 | ||
31 | /** | |
32 | * <p> | |
33 | * Checks for redundant import statements. An import statement is | |
34 | * considered redundant if: | |
35 | * </p> | |
36 | * <ul> | |
37 | * <li>It is a duplicate of another import. This is, when a class is imported | |
38 | * more than once.</li> | |
39 | * <li>The class non-statically imported is from the {@code java.lang} | |
40 | * package, e.g. importing {@code java.lang.String}.</li> | |
41 | * <li>The class non-statically imported is from the same package as the | |
42 | * current package.</li> | |
43 | * </ul> | |
44 | * <p> | |
45 | * To configure the check: | |
46 | * </p> | |
47 | * <pre> | |
48 | * <module name="RedundantImport"/> | |
49 | * </pre> | |
50 | * | |
51 | * @since 3.0 | |
52 | */ | |
53 | @FileStatefulCheck | |
54 | public class RedundantImportCheck | |
55 | extends AbstractCheck { | |
56 | ||
57 | /** | |
58 | * A key is pointing to the warning message text in "messages.properties" | |
59 | * file. | |
60 | */ | |
61 | public static final String MSG_LANG = "import.lang"; | |
62 | ||
63 | /** | |
64 | * A key is pointing to the warning message text in "messages.properties" | |
65 | * file. | |
66 | */ | |
67 | public static final String MSG_SAME = "import.same"; | |
68 | ||
69 | /** | |
70 | * A key is pointing to the warning message text in "messages.properties" | |
71 | * file. | |
72 | */ | |
73 | public static final String MSG_DUPLICATE = "import.duplicate"; | |
74 | ||
75 | /** Set of the imports. */ | |
76 |
1
1. |
private final Set<FullIdent> imports = new HashSet<>(); |
77 | /** Set of static imports. */ | |
78 |
1
1. |
private final Set<FullIdent> staticImports = new HashSet<>(); |
79 | ||
80 | /** Name of package in file. */ | |
81 | private String pkgName; | |
82 | ||
83 | @Override | |
84 | public void beginTree(DetailAST aRootAST) { | |
85 | pkgName = null; | |
86 |
1
1. beginTree : removed call to java/util/Set::clear → KILLED |
imports.clear(); |
87 |
1
1. beginTree : removed call to java/util/Set::clear → KILLED |
staticImports.clear(); |
88 | } | |
89 | ||
90 | @Override | |
91 | public int[] getDefaultTokens() { | |
92 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
93 | } | |
94 | ||
95 | @Override | |
96 | public int[] getAcceptableTokens() { | |
97 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
98 | } | |
99 | ||
100 | @Override | |
101 | public int[] getRequiredTokens() { | |
102 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
103 | TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT, TokenTypes.PACKAGE_DEF, | |
104 | }; | |
105 | } | |
106 | ||
107 | @Override | |
108 | public void visitToken(DetailAST ast) { | |
109 |
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 (ast.getType() == TokenTypes.PACKAGE_DEF) { |
110 | pkgName = FullIdent.createFullIdent( | |
111 | ast.getLastChild().getPreviousSibling()).getText(); | |
112 | } | |
113 |
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 |
else if (ast.getType() == TokenTypes.IMPORT) { |
114 | final FullIdent imp = FullIdent.createFullIdentBelow(ast); | |
115 |
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 (isFromPackage(imp.getText(), "java.lang")) { |
116 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED |
log(ast, MSG_LANG, imp.getText()); |
117 | } | |
118 | // imports from unnamed package are not allowed, | |
119 | // so we are checking SAME rule only for named packages | |
120 |
6
1. visitToken : removed conditional - replaced equality check with true → SURVIVED 2. visitToken : negated conditional → KILLED 3. visitToken : negated conditional → KILLED 4. visitToken : removed conditional - replaced equality check with false → KILLED 5. visitToken : removed conditional - replaced equality check with false → KILLED 6. visitToken : removed conditional - replaced equality check with true → KILLED |
else if (pkgName != null && isFromPackage(imp.getText(), pkgName)) { |
121 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED |
log(ast, MSG_SAME, imp.getText()); |
122 | } | |
123 | // Check for a duplicate import | |
124 |
3
1. lambda$visitToken$0 : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$0 → KILLED 2. lambda$visitToken$0 : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$0 → KILLED 3. lambda$visitToken$0 : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
imports.stream().filter(full -> imp.getText().equals(full.getText())) |
125 |
2
1. lambda$visitToken$1 : removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED 2. visitToken : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText())); |
126 | ||
127 | imports.add(imp); | |
128 | } | |
129 | else { | |
130 | // Check for a duplicate static import | |
131 | final FullIdent imp = | |
132 | FullIdent.createFullIdent( | |
133 | ast.getLastChild().getPreviousSibling()); | |
134 |
3
1. lambda$visitToken$2 : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$2 → KILLED 2. lambda$visitToken$2 : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$2 → KILLED 3. lambda$visitToken$2 : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
staticImports.stream().filter(full -> imp.getText().equals(full.getText())) |
135 |
2
1. lambda$visitToken$3 : removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED 2. visitToken : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText())); |
136 | ||
137 | staticImports.add(imp); | |
138 | } | |
139 | } | |
140 | ||
141 | /** | |
142 | * Determines if an import statement is for types from a specified package. | |
143 | * @param importName the import name | |
144 | * @param pkg the package name | |
145 | * @return whether from the package | |
146 | */ | |
147 | private static boolean isFromPackage(String importName, String pkg) { | |
148 | // imports from unnamed package are not allowed: | |
149 | // https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5 | |
150 | // So '.' must be present in member name and we are not checking for it | |
151 | final int index = importName.lastIndexOf('.'); | |
152 | final String front = importName.substring(0, index); | |
153 |
3
1. isFromPackage : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::isFromPackage → KILLED 2. isFromPackage : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::isFromPackage → KILLED 3. isFromPackage : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return front.equals(pkg); |
154 | } | |
155 | ||
156 | } | |
Mutations | ||
76 |
1.1 |
|
78 |
1.1 |
|
86 |
1.1 |
|
87 |
1.1 |
|
92 |
1.1 |
|
97 |
1.1 |
|
102 |
1.1 |
|
109 |
1.1 2.2 3.3 |
|
113 |
1.1 2.2 3.3 |
|
115 |
1.1 2.2 3.3 |
|
116 |
1.1 |
|
120 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
121 |
1.1 |
|
124 |
1.1 2.2 3.3 |
|
125 |
1.1 2.2 |
|
134 |
1.1 2.2 3.3 |
|
135 |
1.1 2.2 |
|
153 |
1.1 2.2 3.3 |