| 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.whitespace; | |
| 21 | ||
| 22 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 24 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 25 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
| 26 | ||
| 27 | /** | |
| 28 | * <p> | |
| 29 | * Checks that there is no whitespace before a token. | |
| 30 | * More specifically, it checks that it is not preceded with whitespace, | |
| 31 | * or (if line breaks are allowed) all characters on the line before are | |
| 32 | * whitespace. To allow line breaks before a token, set property | |
| 33 | * allowLineBreaks to true. No check occurs before semi-colons in empty | |
| 34 | * for loop initializers or conditions. | |
| 35 | * </p> | |
| 36 | * <p> By default the check will check the following operators: | |
| 37 | * {@link TokenTypes#COMMA COMMA}, | |
| 38 | * {@link TokenTypes#SEMI SEMI}, | |
| 39 | * {@link TokenTypes#POST_DEC POST_DEC}, | |
| 40 | * {@link TokenTypes#POST_INC POST_INC}, | |
| 41 | * {@link TokenTypes#ELLIPSIS ELLIPSIS}. | |
| 42 | * {@link TokenTypes#DOT DOT} is also an acceptable token in a configuration | |
| 43 | * of this check. | |
| 44 | * </p> | |
| 45 | * | |
| 46 | * <p> | |
| 47 | * An example of how to configure the check is: | |
| 48 | * </p> | |
| 49 | * <pre> | |
| 50 | * <module name="NoWhitespaceBefore"/> | |
| 51 | * </pre> | |
| 52 | * <p> An example of how to configure the check to allow line breaks before | |
| 53 | * a {@link TokenTypes#DOT DOT} token is: | |
| 54 | * </p> | |
| 55 | * <pre> | |
| 56 | * <module name="NoWhitespaceBefore"> | |
| 57 | * <property name="tokens" value="DOT"/> | |
| 58 | * <property name="allowLineBreaks" value="true"/> | |
| 59 | * </module> | |
| 60 | * </pre> | |
| 61 | * @author Rick Giles | |
| 62 | * @author lkuehne | |
| 63 | */ | |
| 64 | public class NoWhitespaceBeforeCheck | |
| 65 | extends AbstractCheck { | |
| 66 | ||
| 67 | /** | |
| 68 | * A key is pointing to the warning message text in "messages.properties" | |
| 69 | * file. | |
| 70 | */ | |
| 71 | public static final String MSG_KEY = "ws.preceded"; | |
| 72 | ||
| 73 | /** Whether whitespace is allowed if the AST is at a linebreak. */ | |
| 74 | private boolean allowLineBreaks; | |
| 75 | ||
| 76 | @Override | |
| 77 | public int[] getDefaultTokens() { | |
| 78 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
| 79 | TokenTypes.COMMA, | |
| 80 | TokenTypes.SEMI, | |
| 81 | TokenTypes.POST_INC, | |
| 82 | TokenTypes.POST_DEC, | |
| 83 | TokenTypes.ELLIPSIS, | |
| 84 | }; | |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public int[] getAcceptableTokens() { | |
| 89 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
| 90 | TokenTypes.COMMA, | |
| 91 | TokenTypes.SEMI, | |
| 92 | TokenTypes.POST_INC, | |
| 93 | TokenTypes.POST_DEC, | |
| 94 | TokenTypes.DOT, | |
| 95 | TokenTypes.GENERIC_START, | |
| 96 | TokenTypes.GENERIC_END, | |
| 97 | TokenTypes.ELLIPSIS, | |
| 98 | TokenTypes.METHOD_REF, | |
| 99 | }; | |
| 100 | } | |
| 101 | ||
| 102 | @Override | |
| 103 | public int[] getRequiredTokens() { | |
| 104 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 105 | } | |
| 106 | ||
| 107 | @Override | |
| 108 | public void visitToken(DetailAST ast) { | |
| 109 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final String line = getLine(ast.getLineNo() - 1); |
| 110 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final int before = ast.getColumnNo() - 1; |
| 111 | ||
| 112 |
2
1. visitToken : negated conditional → KILLED 2. visitToken : negated conditional → KILLED |
if ((before == -1 || Character.isWhitespace(line.charAt(before))) |
| 113 |
1
1. visitToken : negated conditional → KILLED |
&& !isInEmptyForInitializerOrCondition(ast)) { |
| 114 | ||
| 115 |
1
1. visitToken : negated conditional → KILLED |
boolean flag = !allowLineBreaks; |
| 116 | // verify all characters before '.' are whitespace | |
| 117 |
5
1. visitToken : changed conditional boundary → KILLED 2. visitToken : Changed increment from 1 to -1 → KILLED 3. visitToken : Replaced integer subtraction with addition → KILLED 4. visitToken : negated conditional → KILLED 5. visitToken : negated conditional → KILLED |
for (int i = 0; !flag && i <= before - 1; i++) { |
| 118 |
1
1. visitToken : negated conditional → KILLED |
if (!Character.isWhitespace(line.charAt(i))) { |
| 119 | flag = true; | |
| 120 | break; | |
| 121 | } | |
| 122 | } | |
| 123 |
1
1. visitToken : negated conditional → KILLED |
if (flag) { |
| 124 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck::log → KILLED |
log(ast.getLineNo(), before, MSG_KEY, ast.getText()); |
| 125 | } | |
| 126 | } | |
| 127 | } | |
| 128 | ||
| 129 | /** | |
| 130 | * Checks that semicolon is in empty for initializer or condition. | |
| 131 | * @param semicolonAst DetailAST of semicolon. | |
| 132 | * @return true if semicolon is in empty for initializer or condition. | |
| 133 | */ | |
| 134 | private static boolean isInEmptyForInitializerOrCondition(DetailAST semicolonAst) { | |
| 135 | boolean result = false; | |
| 136 |
1
1. isInEmptyForInitializerOrCondition : negated conditional → KILLED |
if (semicolonAst.getType() == TokenTypes.SEMI) { |
| 137 | final DetailAST sibling = semicolonAst.getPreviousSibling(); | |
| 138 |
1
1. isInEmptyForInitializerOrCondition : negated conditional → KILLED |
if (sibling != null |
| 139 |
1
1. isInEmptyForInitializerOrCondition : negated conditional → KILLED |
&& (sibling.getType() == TokenTypes.FOR_INIT |
| 140 |
1
1. isInEmptyForInitializerOrCondition : negated conditional → KILLED |
|| sibling.getType() == TokenTypes.FOR_CONDITION) |
| 141 |
1
1. isInEmptyForInitializerOrCondition : negated conditional → KILLED |
&& sibling.getChildCount() == 0) { |
| 142 | result = true; | |
| 143 | } | |
| 144 | } | |
| 145 |
1
1. isInEmptyForInitializerOrCondition : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * Control whether whitespace is flagged at line breaks. | |
| 150 | * @param allowLineBreaks whether whitespace should be | |
| 151 | * flagged at line breaks. | |
| 152 | */ | |
| 153 | public void setAllowLineBreaks(boolean allowLineBreaks) { | |
| 154 | this.allowLineBreaks = allowLineBreaks; | |
| 155 | } | |
| 156 | } | |
Mutations | ||
| 78 |
1.1 |
|
| 89 |
1.1 |
|
| 104 |
1.1 |
|
| 109 |
1.1 |
|
| 110 |
1.1 |
|
| 112 |
1.1 2.2 |
|
| 113 |
1.1 |
|
| 115 |
1.1 |
|
| 117 |
1.1 2.2 3.3 4.4 5.5 |
|
| 118 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 136 |
1.1 |
|
| 138 |
1.1 |
|
| 139 |
1.1 |
|
| 140 |
1.1 |
|
| 141 |
1.1 |
|
| 145 |
1.1 |