| 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.regex.Pattern; | |
| 23 | ||
| 24 | /** | |
| 25 | * Represents an import rules for a specific file. Only the file name is | |
| 26 | * considered and only files processed by TreeWalker. The file's | |
| 27 | * extension is ignored. | |
| 28 | */ | |
| 29 | class FileImportControl extends AbstractImportControl { | |
| 30 | /** The name for the file. */ | |
| 31 | private final String name; | |
| 32 | /** The regex pattern for exact matches - only not null if regex is true. */ | |
| 33 | private final Pattern patternForExactMatch; | |
| 34 | /** If this file name represents a regular expression. */ | |
| 35 | private final boolean regex; | |
| 36 | ||
| 37 | /** | |
| 38 | * Construct a file node. | |
| 39 | * | |
| 40 | * @param parent the parent node. | |
| 41 | * @param name the name of the file. | |
| 42 | * @param regex flags interpretation of name as regex pattern. | |
| 43 | */ | |
| 44 | /* package */ FileImportControl(PkgImportControl parent, String name, boolean regex) { | |
| 45 | super(parent, MismatchStrategy.DELEGATE_TO_PARENT); | |
| 46 | ||
| 47 | this.regex = regex; | |
| 48 |
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) { |
| 49 | this.name = encloseInGroup(name); | |
| 50 | patternForExactMatch = createPatternForExactMatch(this.name); | |
| 51 | } | |
| 52 | else { | |
| 53 | this.name = name; | |
| 54 | patternForExactMatch = null; | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Enclose {@code expression} in a (non-capturing) group. | |
| 60 | * | |
| 61 | * @param expression the input regular expression | |
| 62 | * @return a grouped pattern. | |
| 63 | */ | |
| 64 | private static String encloseInGroup(String expression) { | |
| 65 |
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/FileImportControl::encloseInGroup to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return "(?:" + expression + ")"; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Creates a Pattern from {@code expression}. | |
| 70 | * | |
| 71 | * @param expression a self-contained regular expression matching the full | |
| 72 | * file name exactly. | |
| 73 | * @return a Pattern. | |
| 74 | */ | |
| 75 | private static Pattern createPatternForExactMatch(String expression) { | |
| 76 |
1
1. createPatternForExactMatch : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/FileImportControl::createPatternForExactMatch to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Pattern.compile(expression); |
| 77 | } | |
| 78 | ||
| 79 | @Override | |
| 80 | public AbstractImportControl locateFinest(String forPkg, String forFileName) { | |
| 81 | AbstractImportControl finestMatch = null; | |
| 82 | // Check if we are a match. | |
| 83 |
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 (matchesExactly(forPkg, forFileName)) { |
| 84 | finestMatch = this; | |
| 85 | } | |
| 86 |
1
1. locateFinest : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/imports/FileImportControl::locateFinest to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return finestMatch; |
| 87 | } | |
| 88 | ||
| 89 | @Override | |
| 90 | protected boolean matchesExactly(String pkg, String fileName) { | |
| 91 | final boolean result; | |
| 92 |
3
1. matchesExactly : negated conditional → KILLED 2. matchesExactly : removed conditional - replaced equality check with false → KILLED 3. matchesExactly : removed conditional - replaced equality check with true → KILLED |
if (fileName == null) { |
| 93 | result = false; | |
| 94 | } | |
| 95 |
3
1. matchesExactly : negated conditional → KILLED 2. matchesExactly : removed conditional - replaced equality check with false → KILLED 3. matchesExactly : removed conditional - replaced equality check with true → KILLED |
else if (regex) { |
| 96 | result = patternForExactMatch.matcher(fileName).matches(); | |
| 97 | } | |
| 98 | else { | |
| 99 | result = name.equals(fileName); | |
| 100 | } | |
| 101 |
3
1. matchesExactly : replaced boolean return with false for com/puppycrawl/tools/checkstyle/checks/imports/FileImportControl::matchesExactly → KILLED 2. matchesExactly : replaced boolean return with true for com/puppycrawl/tools/checkstyle/checks/imports/FileImportControl::matchesExactly → KILLED 3. matchesExactly : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 102 | } | |
| 103 | } | |
Mutations | ||
| 48 |
1.1 2.2 3.3 |
|
| 65 |
1.1 2.2 |
|
| 76 |
1.1 |
|
| 83 |
1.1 2.2 3.3 |
|
| 86 |
1.1 |
|
| 92 |
1.1 2.2 3.3 |
|
| 95 |
1.1 2.2 3.3 |
|
| 101 |
1.1 2.2 3.3 |