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 | import java.util.regex.Pattern; | |
25 | ||
26 | /** | |
27 | * Represents a tree of import rules for a specific package. | |
28 | * Each instance may have zero or more children. A child may | |
29 | * be a sub-package, a class, or an allow/disallow rule. | |
30 | */ | |
31 | class PkgImportControl extends AbstractImportControl { | |
32 | /** The package separator: "." */ | |
33 | private static final String DOT = "."; | |
34 | /** A pattern matching the package separator: "." */ | |
35 | private static final Pattern DOT_PATTERN = Pattern.compile(DOT, Pattern.LITERAL); | |
36 | /** The regex for the package separator: "\\.". */ | |
37 | private static final String DOT_REGEX = "\\."; | |
38 | ||
39 | /** List of children {@link AbstractImportControl} objects. */ | |
40 |
2
1. <init> : removed call to java/util/ArrayList::<init> → KILLED 2. <init> : removed call to java/util/ArrayList::<init> → KILLED |
private final List<AbstractImportControl> children = new ArrayList<>(); |
41 | ||
42 | /** The full name for the package. */ | |
43 | private final String fullPackageName; | |
44 | /** | |
45 | * The regex pattern for partial match (exact and for subpackages) - only not | |
46 | * null if regex is true. | |
47 | */ | |
48 | private final Pattern patternForPartialMatch; | |
49 | /** The regex pattern for exact matches - only not null if regex is true. */ | |
50 | private final Pattern patternForExactMatch; | |
51 | /** If this package represents a regular expression. */ | |
52 | private final boolean regex; | |
53 | ||
54 | /** | |
55 | * Construct a root, package node. | |
56 | * | |
57 | * @param packageName the name of the package. | |
58 | * @param regex flags interpretation of name as regex pattern. | |
59 | * @param strategyOnMismatch strategy in a case if matching allow/disallow rule was not found. | |
60 | */ | |
61 | /* package */ PkgImportControl(String packageName, boolean regex, | |
62 | MismatchStrategy strategyOnMismatch) { | |
63 | super(null, strategyOnMismatch); | |
64 | ||
65 | this.regex = regex; | |
66 |
3
1. <init> : negated conditional → KILLED 2. <init> : removed conditional - replaced equality check with false → KILLED 3. <init> : removed conditional - replaced equality check with true → KILLED |
if (regex) { |
67 | // ensure that fullName is a self-contained regular expression | |
68 | fullPackageName = encloseInGroup(packageName); | |
69 | patternForPartialMatch = createPatternForPartialMatch(fullPackageName); | |
70 | patternForExactMatch = createPatternForExactMatch(fullPackageName); | |
71 | } | |
72 | else { | |
73 | fullPackageName = packageName; | |
74 | patternForPartialMatch = null; | |
75 | patternForExactMatch = null; | |
76 | } | |
77 | } | |
78 | ||
79 | /** | |
80 | * Construct a sub-package node. The concatenation of regular expressions needs special care: | |
81 | * see {@link #ensureSelfContainedRegex(String, boolean)} for more details. | |
82 | * | |
83 | * @param parent the parent package. | |
84 | * @param subPackageName the name of the current sub-package. | |
85 | * @param regex flags interpretation of name as regex pattern. | |
86 | * @param strategyOnMismatch strategy in a case if matching allow/disallow rule was not found. | |
87 | */ | |
88 | /* package */ PkgImportControl(PkgImportControl parent, String subPackageName, boolean regex, | |
89 | MismatchStrategy strategyOnMismatch) { | |
90 | super(parent, strategyOnMismatch); | |
91 |
6
1. <init> : removed conditional - replaced equality check with false → SURVIVED 2. <init> : removed conditional - replaced equality check with true → SURVIVED 3. <init> : negated conditional → KILLED 4. <init> : negated conditional → KILLED 5. <init> : removed conditional - replaced equality check with false → KILLED 6. <init> : removed conditional - replaced equality check with true → KILLED |
if (regex || parent.regex) { |
92 | // regex gets inherited | |
93 | final String parentRegex = ensureSelfContainedRegex(parent.fullPackageName, | |
94 | parent.regex); | |
95 | final String thisRegex = ensureSelfContainedRegex(subPackageName, regex); | |
96 |
1
1. <init> : removed call to java/lang/StringBuilder::<init> → KILLED |
fullPackageName = parentRegex + DOT_REGEX + thisRegex; |
97 | patternForPartialMatch = createPatternForPartialMatch(fullPackageName); | |
98 | patternForExactMatch = createPatternForExactMatch(fullPackageName); | |
99 | this.regex = true; | |
100 | } | |
101 | else { | |
102 |
1
1. <init> : removed call to java/lang/StringBuilder::<init> → KILLED |
fullPackageName = parent.fullPackageName + DOT + subPackageName; |
103 | patternForPartialMatch = null; | |
104 | patternForExactMatch = null; | |
105 | this.regex = false; | |
106 | } | |
107 | } | |
108 | ||
109 | /** | |
110 | * Returns a regex that is suitable for concatenation by 1) either converting a plain string | |
111 | * into a regular expression (handling special characters) or 2) by enclosing {@code input} in | |
112 | * a (non-capturing) group if {@code input} already is a regular expression. | |
113 | * | |
114 | * <p>1) When concatenating a non-regex package component (like "org.google") with a regex | |
115 | * component (like "[^.]+") the other component has to be converted into a regex too, see | |
116 | * {@link #toRegex(String)}. | |
117 | * | |
118 | * <p>2) The grouping is strictly necessary if a) {@code input} is a regular expression that b) | |
119 | * contains the alteration character ('|') and if c) the pattern is not already enclosed in a | |
120 | * group - as you see in this example: {@code parent="com|org", child="common|uncommon"} will | |
121 | * result in the pattern {@code "(?:org|com)\.(?common|uncommon)"} what will match | |
122 | * {@code "com.common"}, {@code "com.uncommon"}, {@code "org.common"}, and {@code | |
123 | * "org.uncommon"}. Without the grouping it would be {@code "com|org.common|uncommon"} which | |
124 | * would match {@code "com"}, {@code "org.common"}, and {@code "uncommon"}, which clearly is | |
125 | * undesirable. Adding the group fixes this. | |
126 | * | |
127 | * <p>For simplicity the grouping is added to regular expressions unconditionally. | |
128 | * | |
129 | * @param input the input string. | |
130 | * @param alreadyRegex signals if input already is a regular expression. | |
131 | * @return a regex string. | |
132 | */ | |
133 | private static String ensureSelfContainedRegex(String input, boolean alreadyRegex) { | |
134 | final String result; | |
135 |
3
1. ensureSelfContainedRegex : removed conditional - replaced equality check with true → SURVIVED 2. ensureSelfContainedRegex : negated conditional → KILLED 3. ensureSelfContainedRegex : removed conditional - replaced equality check with false → KILLED |
if (alreadyRegex) { |
136 | result = encloseInGroup(input); | |
137 | } | |
138 | else { | |
139 | result = toRegex(input); | |
140 | } | |
141 |
1
1. ensureSelfContainedRegex : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::ensureSelfContainedRegex to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return result; |
142 | } | |
143 | ||
144 | /** | |
145 | * Enclose {@code expression} in a (non-capturing) group. | |
146 | * | |
147 | * @param expression the input regular expression | |
148 | * @return a grouped pattern. | |
149 | */ | |
150 | private static String encloseInGroup(String expression) { | |
151 |
2
1. encloseInGroup : removed call to java/lang/StringBuilder::<init> → KILLED 2. encloseInGroup : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::encloseInGroup to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return "(?:" + expression + ")"; |
152 | } | |
153 | ||
154 | /** | |
155 | * Converts a normal package name into a regex pattern by escaping all | |
156 | * special characters that may occur in a java package name. | |
157 | * | |
158 | * @param input the input string. | |
159 | * @return a regex string. | |
160 | */ | |
161 | private static String toRegex(String input) { | |
162 |
1
1. toRegex : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::toRegex to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return DOT_PATTERN.matcher(input).replaceAll(DOT_REGEX); |
163 | } | |
164 | ||
165 | /** | |
166 | * Creates a Pattern from {@code expression} that matches exactly and child packages. | |
167 | * | |
168 | * @param expression a self-contained regular expression matching the full package exactly. | |
169 | * @return a Pattern. | |
170 | */ | |
171 | private static Pattern createPatternForPartialMatch(String expression) { | |
172 | // javadoc of encloseInGroup() explains how to concatenate regular expressions | |
173 | // no grouping needs to be added to fullPackage since this already have been done. | |
174 |
2
1. createPatternForPartialMatch : removed call to java/lang/StringBuilder::<init> → KILLED 2. createPatternForPartialMatch : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::createPatternForPartialMatch to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Pattern.compile(expression + "(?:\\..*)?"); |
175 | } | |
176 | ||
177 | /** | |
178 | * Creates a Pattern from {@code expression}. | |
179 | * | |
180 | * @param expression a self-contained regular expression matching the full package exactly. | |
181 | * @return a Pattern. | |
182 | */ | |
183 | private static Pattern createPatternForExactMatch(String expression) { | |
184 |
1
1. createPatternForExactMatch : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::createPatternForExactMatch to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Pattern.compile(expression); |
185 | } | |
186 | ||
187 | @Override | |
188 | public AbstractImportControl locateFinest(String forPkg, String forFileName) { | |
189 | AbstractImportControl finestMatch = null; | |
190 | // Check if we are a match. | |
191 |
3
1. locateFinest : negated conditional → KILLED 2. locateFinest : removed conditional - replaced equality check with false → KILLED 3. locateFinest : removed conditional - replaced equality check with true → KILLED |
if (matchesAtFront(forPkg)) { |
192 | // If there won't be match so I am the best there is. | |
193 | finestMatch = this; | |
194 | // Check if any of the children match. | |
195 | for (AbstractImportControl child : children) { | |
196 | final AbstractImportControl match = child.locateFinest(forPkg, forFileName); | |
197 |
3
1. locateFinest : negated conditional → KILLED 2. locateFinest : removed conditional - replaced equality check with false → KILLED 3. locateFinest : removed conditional - replaced equality check with true → KILLED |
if (match != null) { |
198 | finestMatch = match; | |
199 | break; | |
200 | } | |
201 | } | |
202 | } | |
203 |
1
1. locateFinest : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::locateFinest to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return finestMatch; |
204 | } | |
205 | ||
206 | /** | |
207 | * Adds new child import control. | |
208 | * | |
209 | * @param importControl child import control | |
210 | */ | |
211 | public void addChild(AbstractImportControl importControl) { | |
212 | children.add(importControl); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Matches other package name exactly or partially at front. | |
217 | * | |
218 | * @param pkg the package to compare with. | |
219 | * @return if it matches. | |
220 | */ | |
221 | private boolean matchesAtFront(String pkg) { | |
222 | final boolean result; | |
223 |
3
1. matchesAtFront : negated conditional → KILLED 2. matchesAtFront : removed conditional - replaced equality check with false → KILLED 3. matchesAtFront : removed conditional - replaced equality check with true → KILLED |
if (regex) { |
224 | result = patternForPartialMatch.matcher(pkg).matches(); | |
225 | } | |
226 | else { | |
227 | result = matchesAtFrontNoRegex(pkg); | |
228 | } | |
229 |
3
1. matchesAtFront : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::matchesAtFront → KILLED 2. matchesAtFront : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::matchesAtFront → KILLED 3. matchesAtFront : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
230 | } | |
231 | ||
232 | /** | |
233 | * Non-regex case. Ensure a trailing dot for subpackages, i.e. "com.puppy" | |
234 | * will match "com.puppy.crawl" but not "com.puppycrawl.tools". | |
235 | * | |
236 | * @param pkg the package to compare with. | |
237 | * @return if it matches. | |
238 | */ | |
239 | private boolean matchesAtFrontNoRegex(String pkg) { | |
240 |
5
1. matchesAtFrontNoRegex : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::matchesAtFrontNoRegex → KILLED 2. matchesAtFrontNoRegex : negated conditional → KILLED 3. matchesAtFrontNoRegex : removed conditional - replaced equality check with false → KILLED 4. matchesAtFrontNoRegex : removed conditional - replaced equality check with true → KILLED 5. matchesAtFrontNoRegex : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return pkg.startsWith(fullPackageName) |
241 |
3
1. matchesAtFrontNoRegex : negated conditional → KILLED 2. matchesAtFrontNoRegex : removed conditional - replaced equality check with false → KILLED 3. matchesAtFrontNoRegex : removed conditional - replaced equality check with true → KILLED |
&& (pkg.length() == fullPackageName.length() |
242 |
3
1. matchesAtFrontNoRegex : negated conditional → KILLED 2. matchesAtFrontNoRegex : removed conditional - replaced equality check with false → KILLED 3. matchesAtFrontNoRegex : removed conditional - replaced equality check with true → KILLED |
|| pkg.charAt(fullPackageName.length()) == '.'); |
243 | } | |
244 | ||
245 | @Override | |
246 | protected boolean matchesExactly(String pkg, String fileName) { | |
247 | final boolean result; | |
248 |
3
1. matchesExactly : removed conditional - replaced equality check with false → SURVIVED 2. matchesExactly : negated conditional → KILLED 3. matchesExactly : removed conditional - replaced equality check with true → KILLED |
if (regex) { |
249 | result = patternForExactMatch.matcher(pkg).matches(); | |
250 | } | |
251 | else { | |
252 | result = fullPackageName.equals(pkg); | |
253 | } | |
254 |
3
1. matchesExactly : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::matchesExactly → KILLED 2. matchesExactly : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/PkgImportControl::matchesExactly → KILLED 3. matchesExactly : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
255 | } | |
256 | } | |
Mutations | ||
40 |
1.1 2.2 |
|
66 |
1.1 2.2 3.3 |
|
91 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
96 |
1.1 |
|
102 |
1.1 |
|
135 |
1.1 2.2 3.3 |
|
141 |
1.1 |
|
151 |
1.1 2.2 |
|
162 |
1.1 |
|
174 |
1.1 2.2 |
|
184 |
1.1 |
|
191 |
1.1 2.2 3.3 |
|
197 |
1.1 2.2 3.3 |
|
203 |
1.1 |
|
223 |
1.1 2.2 3.3 |
|
229 |
1.1 2.2 3.3 |
|
240 |
1.1 2.2 3.3 4.4 5.5 |
|
241 |
1.1 2.2 3.3 |
|
242 |
1.1 2.2 3.3 |
|
248 |
1.1 2.2 3.3 |
|
254 |
1.1 2.2 3.3 |