AvoidStarImportCheck.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.ArrayList;
23
import java.util.List;
24
25
import com.puppycrawl.tools.checkstyle.StatelessCheck;
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 that there are no import statements that use the {@code *} notation.
34
 * </p>
35
 * <p>
36
 * Rationale: Importing all classes from a package or static
37
 * members from a class leads to tight coupling between packages
38
 * or classes and might lead to problems when a new version of a
39
 * library introduces name clashes.
40
 * </p>
41
 * <p>
42
 * Note that property {@code excludes} is not recursive, subpackages of excluded
43
 * packages are not automatically excluded.
44
 * </p>
45
 * <ul>
46
 * <li>
47
 * Property {@code excludes} - Specify packages where star imports are allowed.
48
 * Type is {@code java.lang.String[]}.
49
 * Default value is {@code ""}.
50
 * </li>
51
 * <li>
52
 * Property {@code allowClassImports} - Control whether to allow starred class
53
 * imports like {@code import java.util.*;}.
54
 * Type is {@code boolean}.
55
 * Default value is {@code false}.
56
 * </li>
57
 * <li>
58
 * Property {@code allowStaticMemberImports} - Control whether to allow starred
59
 * static member imports like {@code import static org.junit.Assert.*;}.
60
 * Type is {@code boolean}.
61
 * Default value is {@code false}.
62
 * </li>
63
 * </ul>
64
 * <p>
65
 * To configure the check:
66
 * </p>
67
 * <pre>
68
 * &lt;module name="AvoidStarImport"/&gt;
69
 * </pre>
70
 * <p>Example:</p>
71
 * <pre>
72
 * import java.util.Scanner;         // OK
73
 * import java.io.*;                 // violation
74
 * import static java.lang.Math.*;   // violation
75
 * import java.util.*;               // violation
76
 * import java.net.*;                // violation
77
 * </pre>
78
 * <p>
79
 * To configure the check so that star imports from packages
80
 * {@code java.io and java.net} as well as static members from class
81
 * {@code java.lang.Math} are allowed:
82
 * </p>
83
 * <pre>
84
 * &lt;module name="AvoidStarImport"&gt;
85
 *   &lt;property name="excludes" value="java.io,java.net,java.lang.Math"/&gt;
86
 * &lt;/module&gt;
87
 * </pre>
88
 * <p>Example:</p>
89
 * <pre>
90
 * import java.util.Scanner;         // OK
91
 * import java.io.*;                 // OK
92
 * import static java.lang.Math.*;   // OK
93
 * import java.util.*;               // violation
94
 * import java.net.*;                // OK
95
 * </pre>
96
 * <p>
97
 * To configure the check so that star imports from all packages are allowed:
98
 * </p>
99
 * <pre>
100
 * &lt;module name="AvoidStarImport"&gt;
101
 *   &lt;property name="allowClassImports" value="true"/&gt;
102
 * &lt;/module&gt;
103
 * </pre>
104
 * <p>Example:</p>
105
 * <pre>
106
 * import java.util.Scanner;         // OK
107
 * import java.io.*;                 // OK
108
 * import static java.lang.Math.*;   // violation
109
 * import java.util.*;               // OK
110
 * import java.net.*;                // OK
111
 * </pre>
112
 * <p>
113
 * To configure the check so that starred static member imports from all packages are allowed:
114
 * </p>
115
 * <pre>
116
 * &lt;module name="AvoidStarImport"&gt;
117
 *   &lt;property name="allowStaticMemberImports" value="true"/&gt;
118
 * &lt;/module&gt;
119
 * </pre>
120
 * <p>Example:</p>
121
 * <pre>
122
 * import java.util.Scanner;         // OK
123
 * import java.io.*;                 // violation
124
 * import static java.lang.Math.*;   // OK
125
 * import java.util.*;               // violation
126
 * import java.net.*;                // violation
127
 * </pre>
128
 * <p>
129
 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
130
 * </p>
131
 * <p>
132
 * Violation Message Keys:
133
 * </p>
134
 * <ul>
135
 * <li>
136
 * {@code import.avoidStar}
137
 * </li>
138
 * </ul>
139
 *
140
 * @since 3.0
141
 */
142
@StatelessCheck
143
public class AvoidStarImportCheck
144
    extends AbstractCheck {
145
146
    /**
147
     * A key is pointing to the warning message text in "messages.properties"
148
     * file.
149
     */
150
    public static final String MSG_KEY = "import.avoidStar";
151
152
    /** Suffix for the star import. */
153
    private static final String STAR_IMPORT_SUFFIX = ".*";
154
155
    /** Specify packages where star imports are allowed. */
156 1 1. <init> : removed call to java/util/ArrayList::<init> → KILLED
    private final List<String> excludes = new ArrayList<>();
157
158
    /**
159
     * Control whether to allow starred class imports like
160
     * {@code import java.util.*;}.
161
     */
162
    private boolean allowClassImports;
163
164
    /**
165
     * Control whether to allow starred static member imports like
166
     * {@code import static org.junit.Assert.*;}.
167
     */
168
    private boolean allowStaticMemberImports;
169
170
    @Override
171
    public int[] getDefaultTokens() {
172 1 1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
173
    }
174
175
    @Override
176
    public int[] getAcceptableTokens() {
177 1 1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return getRequiredTokens();
178
    }
179
180
    @Override
181
    public int[] getRequiredTokens() {
182
        // original implementation checks both IMPORT and STATIC_IMPORT tokens to avoid ".*" imports
183
        // however user can allow using "import" or "import static"
184
        // by configuring allowClassImports and allowStaticMemberImports
185
        // To avoid potential confusion when user specifies conflicting options on configuration
186
        // (see example below) we are adding both tokens to Required list
187
        //   <module name="AvoidStarImport">
188
        //      <property name="tokens" value="IMPORT"/>
189
        //      <property name="allowStaticMemberImports" value="false"/>
190
        //   </module>
191 1 1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
        return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
192
    }
193
194
    /**
195
     * Setter to specify packages where star imports are allowed.
196
     *
197
     * @param excludesParam a list of package names/fully-qualifies class names
198
     *     where star imports are ok.
199
     */
200
    public void setExcludes(String... excludesParam) {
201
        for (final String exclude : excludesParam) {
202 3 1. setExcludes : negated conditional → KILLED
2. setExcludes : removed conditional - replaced equality check with false → KILLED
3. setExcludes : removed conditional - replaced equality check with true → KILLED
            if (exclude.endsWith(STAR_IMPORT_SUFFIX)) {
203
                excludes.add(exclude);
204
            }
205
            else {
206 1 1. setExcludes : removed call to java/lang/StringBuilder::<init> → KILLED
                excludes.add(exclude + STAR_IMPORT_SUFFIX);
207
            }
208
        }
209
    }
210
211
    /**
212
     * Setter to control whether to allow starred class imports like
213
     * {@code import java.util.*;}.
214
     *
215
     * @param allow true to allow false to disallow
216
     */
217
    public void setAllowClassImports(boolean allow) {
218
        allowClassImports = allow;
219
    }
220
221
    /**
222
     * Setter to control whether to allow starred static member imports like
223
     * {@code import static org.junit.Assert.*;}.
224
     *
225
     * @param allow true to allow false to disallow
226
     */
227
    public void setAllowStaticMemberImports(boolean allow) {
228
        allowStaticMemberImports = allow;
229
    }
230
231
    @Override
232
    public void visitToken(final DetailAST ast) {
233 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.IMPORT) {
234 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 (!allowClassImports) {
235
                final DetailAST startingDot = ast.getFirstChild();
236 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED
                logsStarredImportViolation(startingDot);
237
            }
238
        }
239 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 (!allowStaticMemberImports) {
240
            // must navigate past the static keyword
241
            final DetailAST startingDot = ast.getFirstChild().getNextSibling();
242 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED
            logsStarredImportViolation(startingDot);
243
        }
244
    }
245
246
    /**
247
     * Gets the full import identifier.  If the import is a starred import and
248
     * it's not excluded then a violation is logged.
249
     *
250
     * @param startingDot the starting dot for the import statement
251
     */
252
    private void logsStarredImportViolation(DetailAST startingDot) {
253
        final FullIdent name = FullIdent.createFullIdent(startingDot);
254
        final String importText = name.getText();
255 6 1. logsStarredImportViolation : negated conditional → KILLED
2. logsStarredImportViolation : negated conditional → KILLED
3. logsStarredImportViolation : removed conditional - replaced equality check with false → KILLED
4. logsStarredImportViolation : removed conditional - replaced equality check with false → KILLED
5. logsStarredImportViolation : removed conditional - replaced equality check with true → KILLED
6. logsStarredImportViolation : removed conditional - replaced equality check with true → KILLED
        if (importText.endsWith(STAR_IMPORT_SUFFIX) && !excludes.contains(importText)) {
256 1 1. logsStarredImportViolation : removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::log → KILLED
            log(startingDot, MSG_KEY, importText);
257
        }
258
    }
259
260
}

Mutations

156

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

172

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

177

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

191

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

202

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

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

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

206

1.1
Location : setExcludes
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest]/[method:testExcludes()]
removed call to java/lang/StringBuilder::<init> → KILLED

233

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

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

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

234

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

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

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

236

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest]/[method:testAllowStaticMemberImports()]
removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED

239

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

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

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

242

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest]/[method:testExcludes()]
removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED

255

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

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

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

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

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

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

256

1.1
Location : logsStarredImportViolation
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.[engine:junit-jupiter]/[class:com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest]/[method:testAllowStaticMemberImports()]
removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::log → KILLED

Active mutators

Tests examined


Report generated by PIT 1.6.3