RedundantImportCheck.java

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.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
 * &lt;module name="RedundantImport"/&gt;
49
 * </pre>
50
 * <p>
51
 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
52
 * </p>
53
 * <p>
54
 * Violation Message Keys:
55
 * </p>
56
 * <ul>
57
 * <li>
58
 * {@code import.duplicate}
59
 * </li>
60
 * <li>
61
 * {@code import.lang}
62
 * </li>
63
 * <li>
64
 * {@code import.same}
65
 * </li>
66
 * </ul>
67
 *
68
 * @since 3.0
69
 */
70
@FileStatefulCheck
71
public class RedundantImportCheck
72
    extends AbstractCheck {
73
74
    /**
75
     * A key is pointing to the warning message text in "messages.properties"
76
     * file.
77
     */
78
    public static final String MSG_LANG = "import.lang";
79
80
    /**
81
     * A key is pointing to the warning message text in "messages.properties"
82
     * file.
83
     */
84
    public static final String MSG_SAME = "import.same";
85
86
    /**
87
     * A key is pointing to the warning message text in "messages.properties"
88
     * file.
89
     */
90
    public static final String MSG_DUPLICATE = "import.duplicate";
91
92
    /** Set of the imports. */
93 1 1. <init> : removed call to java/util/HashSet::<init> → KILLED
    private final Set<FullIdent> imports = new HashSet<>();
94
    /** Set of static imports. */
95 1 1. <init> : removed call to java/util/HashSet::<init> → KILLED
    private final Set<FullIdent> staticImports = new HashSet<>();
96
97
    /** Name of package in file. */
98
    private String pkgName;
99
100
    @Override
101
    public void beginTree(DetailAST aRootAST) {
102
        pkgName = null;
103 1 1. beginTree : removed call to java/util/Set::clear → KILLED
        imports.clear();
104 1 1. beginTree : removed call to java/util/Set::clear → KILLED
        staticImports.clear();
105
    }
106
107
    @Override
108
    public int[] getDefaultTokens() {
109 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();
110
    }
111
112
    @Override
113
    public int[] getAcceptableTokens() {
114 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();
115
    }
116
117
    @Override
118
    public int[] getRequiredTokens() {
119 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[] {
120
            TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT, TokenTypes.PACKAGE_DEF,
121
        };
122
    }
123
124
    @Override
125
    public void visitToken(DetailAST ast) {
126 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) {
127
            pkgName = FullIdent.createFullIdent(
128
                    ast.getLastChild().getPreviousSibling()).getText();
129
        }
130 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) {
131
            final FullIdent imp = FullIdent.createFullIdentBelow(ast);
132 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")) {
133 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED
                log(ast, MSG_LANG, imp.getText());
134
            }
135
            // imports from unnamed package are not allowed,
136
            // so we are checking SAME rule only for named packages
137 6 1. visitToken : negated conditional → KILLED
2. visitToken : negated conditional → KILLED
3. visitToken : removed conditional - replaced equality check with false → KILLED
4. visitToken : removed conditional - replaced equality check with false → KILLED
5. visitToken : removed conditional - replaced equality check with true → KILLED
6. visitToken : removed conditional - replaced equality check with true → KILLED
            else if (pkgName != null && isFromPackage(imp.getText(), pkgName)) {
138 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED
                log(ast, MSG_SAME, imp.getText());
139
            }
140
            // Check for a duplicate import
141 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()))
142 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()));
143
144
            imports.add(imp);
145
        }
146
        else {
147
            // Check for a duplicate static import
148
            final FullIdent imp =
149
                FullIdent.createFullIdent(
150
                    ast.getLastChild().getPreviousSibling());
151 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()))
152 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()));
153
154
            staticImports.add(imp);
155
        }
156
    }
157
158
    /**
159
     * Determines if an import statement is for types from a specified package.
160
     *
161
     * @param importName the import name
162
     * @param pkg the package name
163
     * @return whether from the package
164
     */
165
    private static boolean isFromPackage(String importName, String pkg) {
166
        // imports from unnamed package are not allowed:
167
        // https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5
168
        // So '.' must be present in member name and we are not checking for it
169
        final int index = importName.lastIndexOf('.');
170
        final String front = importName.substring(0, index);
171 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 pkg.equals(front);
172
    }
173
174
}

Mutations

93

1.1
Location : <init>
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed call to java/util/HashSet::<init> → KILLED

95

1.1
Location : <init>
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed call to java/util/HashSet::<init> → KILLED

103

1.1
Location : beginTree
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
removed call to java/util/Set::clear → KILLED

104

1.1
Location : beginTree
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
removed call to java/util/Set::clear → KILLED

109

1.1
Location : getDefaultTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

114

1.1
Location : getAcceptableTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testGetAcceptableTokens()]
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

119

1.1
Location : getRequiredTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testGetRequiredTokens()]
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

126

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
negated conditional → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testWithChecker()]
removed conditional - replaced equality check with false → KILLED

3.3
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed conditional - replaced equality check with true → KILLED

130

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
negated conditional → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed conditional - replaced equality check with false → KILLED

3.3
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed conditional - replaced equality check with true → KILLED

132

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
negated conditional → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed conditional - replaced equality check with false → KILLED

3.3
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed conditional - replaced equality check with true → KILLED

133

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED

137

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
negated conditional → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
negated conditional → KILLED

3.3
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testWithChecker()]
removed conditional - replaced equality check with false → KILLED

4.4
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testWithChecker()]
removed conditional - replaced equality check with false → KILLED

5.5
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed conditional - replaced equality check with true → KILLED

6.6
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
removed conditional - replaced equality check with true → KILLED

138

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testWithChecker()]
removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED

141

1.1
Location : lambda$visitToken$0
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$0 → KILLED

2.2
Location : lambda$visitToken$0
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$0 → KILLED

3.3
Location : lambda$visitToken$0
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

142

1.1
Location : lambda$visitToken$1
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
removed call to java/util/stream/Stream::forEach → KILLED

151

1.1
Location : lambda$visitToken$2
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$2 → KILLED

2.2
Location : lambda$visitToken$2
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testWithChecker()]
replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::lambda$visitToken$2 → KILLED

3.3
Location : lambda$visitToken$2
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

152

1.1
Location : lambda$visitToken$3
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
removed call to com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::log → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testStateIsClearedOnBeginTree1()]
removed call to java/util/stream/Stream::forEach → KILLED

171

1.1
Location : isFromPackage
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::isFromPackage → KILLED

2.2
Location : isFromPackage
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck::isFromPackage → KILLED

3.3
Location : isFromPackage
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheckTest]/[method:testUnnamedPackage()]
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

Active mutators

Tests examined


Report generated by PIT 1.6.3