AvoidStarImportCheck.java

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.ArrayList;
23
import java.util.List;
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
 * Check that finds import statements that use the * notation.
33
 * </p>
34
 * <p>
35
 * Rationale: Importing all classes from a package or static
36
 * members from a class leads to tight coupling between packages
37
 * or classes and might lead to problems when a new version of a
38
 * library introduces name clashes.
39
 * </p>
40
 * <p>
41
 * An example of how to configure the check is:
42
 * </p>
43
 * <pre>
44
 * &lt;module name="AvoidStarImport"&gt;
45
 *   &lt;property name="excludes" value="java.io,java.net,java.lang.Math"/&gt;
46
 *   &lt;property name="allowClassImports" value="false"/&gt;
47
 *   &lt;property name="allowStaticMemberImports" value="false"/&gt;
48
 * &lt;/module&gt;
49
 * </pre>
50
 * The optional "excludes" property allows for certain packages like
51
 * java.io or java.net to be exempted from the rule. It also is used to
52
 * allow certain classes like java.lang.Math or java.io.File to be
53
 * excluded in order to support static member imports.
54
 *
55
 * <p>The optional "allowClassImports" when set to true, will allow starred
56
 * class imports but will not affect static member imports.
57
 *
58
 * <p>The optional "allowStaticMemberImports" when set to true will allow
59
 * starred static member imports but will not affect class imports.
60
 *
61
 * @author Oliver Burn
62
 * @author <a href="bschneider@vecna.com">Bill Schneider</a>
63
 * @author Travis Schneeberger
64
 */
65
public class AvoidStarImportCheck
66
    extends AbstractCheck {
67
68
    /**
69
     * A key is pointing to the warning message text in "messages.properties"
70
     * file.
71
     */
72
    public static final String MSG_KEY = "import.avoidStar";
73
74
    /** Suffix for the star import. */
75
    private static final String STAR_IMPORT_SUFFIX = ".*";
76
77
    /** The packages/classes to exempt from this check. */
78
    private final List<String> excludes = new ArrayList<>();
79
80
    /** Whether to allow all class imports. */
81
    private boolean allowClassImports;
82
83
    /** Whether to allow all static member imports. */
84
    private boolean allowStaticMemberImports;
85
86
    @Override
87
    public int[] getDefaultTokens() {
88 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 new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
89
    }
90
91
    @Override
92
    public int[] getAcceptableTokens() {
93 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 new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
94
    }
95
96
    @Override
97
    public int[] getRequiredTokens() {
98
        // original implementation checks both IMPORT and STATIC_IMPORT tokens to avoid ".*" imports
99
        // however user can allow using "import" or "import static"
100
        // by configuring allowClassImports and allowStaticMemberImports
101
        // To avoid potential confusion when user specifies conflicting options on configuration
102
        // (see example below) we are adding both tokens to Required list
103
        //   <module name="AvoidStarImport">
104
        //      <property name="tokens" value="IMPORT"/>
105
        //      <property name="allowStaticMemberImports" value="false"/>
106
        //   </module>
107 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};
108
    }
109
110
    /**
111
     * Sets the list of packages or classes to be exempt from the check.
112
     * The excludes can contain a .* or not.
113
     * @param excludesParam a list of package names/fully-qualifies class names
114
     *     where star imports are ok.
115
     */
116
    public void setExcludes(String... excludesParam) {
117 3 1. setExcludes : changed conditional boundary → KILLED
2. setExcludes : Changed increment from 1 to -1 → KILLED
3. setExcludes : negated conditional → KILLED
        for (final String exclude : excludesParam) {
118 1 1. setExcludes : negated conditional → KILLED
            if (exclude.endsWith(STAR_IMPORT_SUFFIX)) {
119
                excludes.add(exclude);
120
            }
121
            else {
122
                excludes.add(exclude + STAR_IMPORT_SUFFIX);
123
            }
124
        }
125
    }
126
127
    /**
128
     * Sets whether or not to allow all non-static class imports.
129
     * @param allow true to allow false to disallow
130
     */
131
    public void setAllowClassImports(boolean allow) {
132
        allowClassImports = allow;
133
    }
134
135
    /**
136
     * Sets whether or not to allow all static member imports.
137
     * @param allow true to allow false to disallow
138
     */
139
    public void setAllowStaticMemberImports(boolean allow) {
140
        allowStaticMemberImports = allow;
141
    }
142
143
    @Override
144
    public void visitToken(final DetailAST ast) {
145 2 1. visitToken : negated conditional → KILLED
2. visitToken : negated conditional → KILLED
        if (!allowClassImports && ast.getType() == TokenTypes.IMPORT) {
146
            final DetailAST startingDot = ast.getFirstChild();
147 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED
            logsStarredImportViolation(startingDot);
148
        }
149 1 1. visitToken : negated conditional → KILLED
        else if (!allowStaticMemberImports
150 1 1. visitToken : negated conditional → KILLED
            && ast.getType() == TokenTypes.STATIC_IMPORT) {
151
            // must navigate past the static keyword
152
            final DetailAST startingDot = ast.getFirstChild().getNextSibling();
153 1 1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED
            logsStarredImportViolation(startingDot);
154
        }
155
    }
156
157
    /**
158
     * Gets the full import identifier.  If the import is a starred import and
159
     * it's not excluded then a violation is logged.
160
     * @param startingDot the starting dot for the import statement
161
     */
162
    private void logsStarredImportViolation(DetailAST startingDot) {
163
        final FullIdent name = FullIdent.createFullIdent(startingDot);
164
        final String importText = name.getText();
165 2 1. logsStarredImportViolation : negated conditional → KILLED
2. logsStarredImportViolation : negated conditional → KILLED
        if (importText.endsWith(STAR_IMPORT_SUFFIX) && !excludes.contains(importText)) {
166 1 1. logsStarredImportViolation : removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::log → KILLED
            log(startingDot.getLineNo(), MSG_KEY, importText);
167
        }
168
    }
169
170
}

Mutations

88

1.1
Location : getDefaultTokens
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

93

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

107

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

117

1.1
Location : setExcludes
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
changed conditional boundary → KILLED

2.2
Location : setExcludes
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
Changed increment from 1 to -1 → KILLED

3.3
Location : setExcludes
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

118

1.1
Location : setExcludes
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

145

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

2.2
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

147

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED

149

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

150

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

153

1.1
Location : visitToken
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::logsStarredImportViolation → KILLED

165

1.1
Location : logsStarredImportViolation
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

2.2
Location : logsStarredImportViolation
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
negated conditional → KILLED

166

1.1
Location : logsStarredImportViolation
Killed by : com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest.testExcludes(com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheckTest)
removed call to com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck::log → KILLED

Active mutators

Tests examined


Report generated by PIT 1.2.2