| 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 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
| 28 | ||
| 29 | /** | |
| 30 | * <p> | |
| 31 | * Checks line wrapping with separators. | |
| 32 | * The policy to verify is specified using the {@link WrapOption} class | |
| 33 | * and defaults to {@link WrapOption#EOL}. | |
| 34 | * </p> | |
| 35 | * <p> By default the check will check the following separators: | |
| 36 | * {@link TokenTypes#DOT DOT}, | |
| 37 | * {@link TokenTypes#COMMA COMMA}, | |
| 38 | * Other acceptable tokens are | |
| 39 | * {@link TokenTypes#SEMI SEMI}, | |
| 40 | * {@link TokenTypes#ELLIPSIS ELLIPSIS}, | |
| 41 | * {@link TokenTypes#AT AT}, | |
| 42 | * {@link TokenTypes#LPAREN LPAREN}, | |
| 43 | * {@link TokenTypes#RPAREN RPAREN}, | |
| 44 | * {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR}, | |
| 45 | * {@link TokenTypes#RBRACK RBRACK}, | |
| 46 | * </p> | |
| 47 | * <p> | |
| 48 | * Code example for comma and dot at the new line: | |
| 49 | * </p> | |
| 50 | * <pre> | |
| 51 | * s | |
| 52 | * .isEmpty(); | |
| 53 | * foo(i | |
| 54 | * ,s); | |
| 55 | * </pre> | |
| 56 | * <p> | |
| 57 | * An example of how to configure the check is: | |
| 58 | * </p> | |
| 59 | * <pre> | |
| 60 | * <module name="SeparatorWrap"/> | |
| 61 | * </pre> | |
| 62 | * <p> | |
| 63 | * Code example for comma and dot at the previous line: | |
| 64 | * </p> | |
| 65 | * <pre> | |
| 66 | * s. | |
| 67 | * isEmpty(); | |
| 68 | * foo(i, | |
| 69 | * s); | |
| 70 | * </pre> | |
| 71 | * <p> An example of how to configure the check for comma at the | |
| 72 | * new line is: | |
| 73 | * </p> | |
| 74 | * <pre> | |
| 75 | * <module name="SeparatorWrap"> | |
| 76 | * <property name="tokens" value="COMMA"/> | |
| 77 | * <property name="option" value="nl"/> | |
| 78 | * </module> | |
| 79 | * </pre> | |
| 80 | * | |
| 81 | * @author maxvetrenko | |
| 82 | */ | |
| 83 | public class SeparatorWrapCheck | |
| 84 | extends AbstractCheck { | |
| 85 | ||
| 86 | /** | |
| 87 | * A key is pointing to the warning message text in "messages.properties" | |
| 88 | * file. | |
| 89 | */ | |
| 90 | public static final String MSG_LINE_PREVIOUS = "line.previous"; | |
| 91 | ||
| 92 | /** | |
| 93 | * A key is pointing to the warning message text in "messages.properties" | |
| 94 | * file. | |
| 95 | */ | |
| 96 | public static final String MSG_LINE_NEW = "line.new"; | |
| 97 | ||
| 98 | /** The policy to enforce. */ | |
| 99 | private WrapOption option = WrapOption.EOL; | |
| 100 | ||
| 101 | /** | |
| 102 | * Set the option to enforce. | |
| 103 | * @param optionStr string to decode option from | |
| 104 | * @throws IllegalArgumentException if unable to decode | |
| 105 | */ | |
| 106 | public void setOption(String optionStr) { | |
| 107 | try { | |
| 108 | option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); | |
| 109 | } | |
| 110 | catch (IllegalArgumentException iae) { | |
| 111 | throw new IllegalArgumentException("unable to parse " + optionStr, iae); | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | @Override | |
| 116 | public int[] getDefaultTokens() { | |
| 117 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
| 118 | TokenTypes.DOT, | |
| 119 | TokenTypes.COMMA, | |
| 120 | }; | |
| 121 | } | |
| 122 | ||
| 123 | @Override | |
| 124 | public int[] getAcceptableTokens() { | |
| 125 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
| 126 | TokenTypes.DOT, | |
| 127 | TokenTypes.COMMA, | |
| 128 | TokenTypes.SEMI, | |
| 129 | TokenTypes.ELLIPSIS, | |
| 130 | TokenTypes.AT, | |
| 131 | TokenTypes.LPAREN, | |
| 132 | TokenTypes.RPAREN, | |
| 133 | TokenTypes.ARRAY_DECLARATOR, | |
| 134 | TokenTypes.RBRACK, | |
| 135 | TokenTypes.METHOD_REF, | |
| 136 | }; | |
| 137 | } | |
| 138 | ||
| 139 | @Override | |
| 140 | public int[] getRequiredTokens() { | |
| 141 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 142 | } | |
| 143 | ||
| 144 | @Override | |
| 145 | public void visitToken(DetailAST ast) { | |
| 146 | final String text = ast.getText(); | |
| 147 | final int colNo = ast.getColumnNo(); | |
| 148 | final int lineNo = ast.getLineNo(); | |
| 149 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final String currentLine = getLines()[lineNo - 1]; |
| 150 | final String substringAfterToken = | |
| 151 |
1
1. visitToken : Replaced integer addition with subtraction → KILLED |
currentLine.substring(colNo + text.length()).trim(); |
| 152 | final String substringBeforeToken = | |
| 153 | currentLine.substring(0, colNo).trim(); | |
| 154 | ||
| 155 |
1
1. visitToken : negated conditional → KILLED |
if (option == WrapOption.EOL |
| 156 |
1
1. visitToken : negated conditional → KILLED |
&& substringBeforeToken.isEmpty()) { |
| 157 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheck::log → KILLED |
log(lineNo, colNo, MSG_LINE_PREVIOUS, text); |
| 158 | } | |
| 159 |
1
1. visitToken : negated conditional → KILLED |
else if (option == WrapOption.NL |
| 160 |
1
1. visitToken : negated conditional → KILLED |
&& substringAfterToken.isEmpty()) { |
| 161 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheck::log → KILLED |
log(lineNo, colNo, MSG_LINE_NEW, text); |
| 162 | } | |
| 163 | } | |
| 164 | } | |
Mutations | ||
| 117 |
1.1 |
|
| 125 |
1.1 |
|
| 141 |
1.1 |
|
| 149 |
1.1 |
|
| 151 |
1.1 |
|
| 155 |
1.1 |
|
| 156 |
1.1 |
|
| 157 |
1.1 |
|
| 159 |
1.1 |
|
| 160 |
1.1 |
|
| 161 |
1.1 |