| 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 java.util.Locale; | |
| 23 | ||
| 24 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
| 25 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 26 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 27 | ||
| 28 | /** | |
| 29 | * <p>Checks the padding of an empty for iterator; that is whether a | |
| 30 | * space is required at an empty for iterator, or such spaces are | |
| 31 | * forbidden. No check occurs if there is a line wrap at the iterator, as in | |
| 32 | * </p> | |
| 33 | * <pre class="body"> | |
| 34 | for (Iterator foo = very.long.line.iterator(); | |
| 35 | foo.hasNext(); | |
| 36 | ) | |
| 37 | </pre> | |
| 38 | * <p> | |
| 39 | * The policy to verify is specified using the {@link PadOption} class and | |
| 40 | * defaults to {@link PadOption#NOSPACE}. | |
| 41 | * </p> | |
| 42 | * <p> | |
| 43 | * An example of how to configure the check is: | |
| 44 | * </p> | |
| 45 | * <pre> | |
| 46 | * <module name="EmptyForIteratorPad"/> | |
| 47 | * </pre> | |
| 48 | * | |
| 49 | * @author Rick Giles | |
| 50 | */ | |
| 51 | public class EmptyForIteratorPadCheck | |
| 52 | extends AbstractCheck { | |
| 53 | ||
| 54 | /** | |
| 55 | * A key is pointing to the warning message text in "messages.properties" | |
| 56 | * file. | |
| 57 | */ | |
| 58 | public static final String MSG_WS_FOLLOWED = "ws.followed"; | |
| 59 | ||
| 60 | /** | |
| 61 | * A key is pointing to the warning message text in "messages.properties" | |
| 62 | * file. | |
| 63 | */ | |
| 64 | public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; | |
| 65 | ||
| 66 | /** Semicolon literal. */ | |
| 67 | private static final String SEMICOLON = ";"; | |
| 68 | ||
| 69 | /** The policy to enforce. */ | |
| 70 | private PadOption option = PadOption.NOSPACE; | |
| 71 | ||
| 72 | /** | |
| 73 | * Set the option to enforce. | |
| 74 | * @param optionStr string to decode option from | |
| 75 | * @throws IllegalArgumentException if unable to decode | |
| 76 | */ | |
| 77 | public void setOption(String optionStr) { | |
| 78 | try { | |
| 79 | option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); | |
| 80 | } | |
| 81 | catch (IllegalArgumentException iae) { | |
| 82 | throw new IllegalArgumentException("unable to parse " + optionStr, iae); | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | @Override | |
| 87 | public int[] getDefaultTokens() { | |
| 88 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
| 89 | } | |
| 90 | ||
| 91 | @Override | |
| 92 | public int[] getAcceptableTokens() { | |
| 93 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.FOR_ITERATOR}; |
| 94 | } | |
| 95 | ||
| 96 | @Override | |
| 97 | public int[] getRequiredTokens() { | |
| 98 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
| 99 | } | |
| 100 | ||
| 101 | @Override | |
| 102 | public void visitToken(DetailAST ast) { | |
| 103 |
1
1. visitToken : negated conditional → KILLED |
if (ast.getChildCount() == 0) { |
| 104 | //empty for iterator. test pad after semi. | |
| 105 | final DetailAST semi = ast.getPreviousSibling(); | |
| 106 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final String line = getLines()[semi.getLineNo() - 1]; |
| 107 |
1
1. visitToken : Replaced integer addition with subtraction → KILLED |
final int after = semi.getColumnNo() + 1; |
| 108 | //don't check if at end of line | |
| 109 |
2
1. visitToken : changed conditional boundary → KILLED 2. visitToken : negated conditional → KILLED |
if (after < line.length()) { |
| 110 |
1
1. visitToken : negated conditional → KILLED |
if (option == PadOption.NOSPACE |
| 111 |
1
1. visitToken : negated conditional → KILLED |
&& Character.isWhitespace(line.charAt(after))) { |
| 112 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck::log → KILLED |
log(semi.getLineNo(), after, MSG_WS_FOLLOWED, SEMICOLON); |
| 113 | } | |
| 114 |
1
1. visitToken : negated conditional → KILLED |
else if (option == PadOption.SPACE |
| 115 |
1
1. visitToken : negated conditional → KILLED |
&& !Character.isWhitespace(line.charAt(after))) { |
| 116 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck::log → KILLED |
log(semi.getLineNo(), after, MSG_WS_NOT_FOLLOWED, SEMICOLON); |
| 117 | } | |
| 118 | } | |
| 119 | } | |
| 120 | } | |
| 121 | } | |
Mutations | ||
| 88 |
1.1 |
|
| 93 |
1.1 |
|
| 98 |
1.1 |
|
| 103 |
1.1 |
|
| 106 |
1.1 |
|
| 107 |
1.1 |
|
| 109 |
1.1 2.2 |
|
| 110 |
1.1 |
|
| 111 |
1.1 |
|
| 112 |
1.1 |
|
| 114 |
1.1 |
|
| 115 |
1.1 |
|
| 116 |
1.1 |