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