| 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 the padding between the identifier of a method definition, | |
| 32 | * constructor definition, method call, or constructor invocation; | |
| 33 | * and the left parenthesis of the parameter list. | |
| 34 | * That is, if the identifier and left parenthesis are on the same line, | |
| 35 | * checks whether a space is required immediately after the identifier or | |
| 36 | * such a space is forbidden. | |
| 37 | * If they are not on the same line, reports an error, unless configured to | |
| 38 | * allow line breaks. | |
| 39 | * </p> | |
| 40 | * <p> By default the check will check the following tokens: | |
| 41 | * {@link TokenTypes#CTOR_DEF CTOR_DEF}, | |
| 42 | * {@link TokenTypes#LITERAL_NEW LITERAL_NEW}, | |
| 43 | * {@link TokenTypes#METHOD_CALL METHOD_CALL}, | |
| 44 | * {@link TokenTypes#METHOD_DEF METHOD_DEF}, | |
| 45 | * {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL}. | |
| 46 | * </p> | |
| 47 | * <p> | |
| 48 | * An example of how to configure the check is: | |
| 49 | * </p> | |
| 50 | * <pre> | |
| 51 | * <module name="MethodParamPad"/> | |
| 52 | * </pre> | |
| 53 | * <p> An example of how to configure the check to require a space | |
| 54 | * after the identifier of a method definition, except if the left | |
| 55 | * parenthesis occurs on a new line, is: | |
| 56 | * </p> | |
| 57 | * <pre> | |
| 58 | * <module name="MethodParamPad"> | |
| 59 | * <property name="tokens" value="METHOD_DEF"/> | |
| 60 | * <property name="option" value="space"/> | |
| 61 | * <property name="allowLineBreaks" value="true"/> | |
| 62 | * </module> | |
| 63 | * </pre> | |
| 64 | * @author Rick Giles | |
| 65 | */ | |
| 66 | ||
| 67 | public class MethodParamPadCheck | |
| 68 | extends AbstractCheck { | |
| 69 | ||
| 70 | /** | |
| 71 | * A key is pointing to the warning message text in "messages.properties" | |
| 72 | * file. | |
| 73 | */ | |
| 74 | public static final String MSG_LINE_PREVIOUS = "line.previous"; | |
| 75 | ||
| 76 | /** | |
| 77 | * A key is pointing to the warning message text in "messages.properties" | |
| 78 | * file. | |
| 79 | */ | |
| 80 | public static final String MSG_WS_PRECEDED = "ws.preceded"; | |
| 81 | ||
| 82 | /** | |
| 83 | * A key is pointing to the warning message text in "messages.properties" | |
| 84 | * file. | |
| 85 | */ | |
| 86 | public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; | |
| 87 | ||
| 88 | /** | |
| 89 | * Whether whitespace is allowed if the method identifier is at a | |
| 90 | * linebreak. | |
| 91 | */ | |
| 92 | private boolean allowLineBreaks; | |
| 93 | ||
| 94 | /** The policy to enforce. */ | |
| 95 | private PadOption option = PadOption.NOSPACE; | |
| 96 | ||
| 97 | @Override | |
| 98 | public int[] getDefaultTokens() { | |
| 99 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
| 100 | } | |
| 101 | ||
| 102 | @Override | |
| 103 | public int[] getAcceptableTokens() { | |
| 104 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
| 105 | TokenTypes.CTOR_DEF, | |
| 106 | TokenTypes.LITERAL_NEW, | |
| 107 | TokenTypes.METHOD_CALL, | |
| 108 | TokenTypes.METHOD_DEF, | |
| 109 | TokenTypes.SUPER_CTOR_CALL, | |
| 110 | TokenTypes.ENUM_CONSTANT_DEF, | |
| 111 | }; | |
| 112 | } | |
| 113 | ||
| 114 | @Override | |
| 115 | public int[] getRequiredTokens() { | |
| 116 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 117 | } | |
| 118 | ||
| 119 | @Override | |
| 120 | public void visitToken(DetailAST ast) { | |
| 121 | final DetailAST parenAST; | |
| 122 |
1
1. visitToken : negated conditional → KILLED |
if (ast.getType() == TokenTypes.METHOD_CALL) { |
| 123 | parenAST = ast; | |
| 124 | } | |
| 125 | else { | |
| 126 | parenAST = ast.findFirstToken(TokenTypes.LPAREN); | |
| 127 | // array construction => parenAST == null | |
| 128 | } | |
| 129 | ||
| 130 |
1
1. visitToken : negated conditional → KILLED |
if (parenAST != null) { |
| 131 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final String line = getLines()[parenAST.getLineNo() - 1]; |
| 132 |
1
1. visitToken : negated conditional → KILLED |
if (CommonUtils.hasWhitespaceBefore(parenAST.getColumnNo(), line)) { |
| 133 |
1
1. visitToken : negated conditional → KILLED |
if (!allowLineBreaks) { |
| 134 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck::log → KILLED |
log(parenAST, MSG_LINE_PREVIOUS, parenAST.getText()); |
| 135 | } | |
| 136 | } | |
| 137 | else { | |
| 138 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
final int before = parenAST.getColumnNo() - 1; |
| 139 |
1
1. visitToken : negated conditional → KILLED |
if (option == PadOption.NOSPACE |
| 140 |
1
1. visitToken : negated conditional → KILLED |
&& Character.isWhitespace(line.charAt(before))) { |
| 141 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck::log → KILLED |
log(parenAST, MSG_WS_PRECEDED, parenAST.getText()); |
| 142 | } | |
| 143 |
1
1. visitToken : negated conditional → KILLED |
else if (option == PadOption.SPACE |
| 144 |
1
1. visitToken : negated conditional → KILLED |
&& !Character.isWhitespace(line.charAt(before))) { |
| 145 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck::log → KILLED |
log(parenAST, MSG_WS_NOT_PRECEDED, parenAST.getText()); |
| 146 | } | |
| 147 | } | |
| 148 | } | |
| 149 | } | |
| 150 | ||
| 151 | /** | |
| 152 | * Control whether whitespace is flagged at line breaks. | |
| 153 | * @param allowLineBreaks whether whitespace should be | |
| 154 | * flagged at line breaks. | |
| 155 | */ | |
| 156 | public void setAllowLineBreaks(boolean allowLineBreaks) { | |
| 157 | this.allowLineBreaks = allowLineBreaks; | |
| 158 | } | |
| 159 | ||
| 160 | /** | |
| 161 | * Set the option to enforce. | |
| 162 | * @param optionStr string to decode option from | |
| 163 | * @throws IllegalArgumentException if unable to decode | |
| 164 | */ | |
| 165 | public void setOption(String optionStr) { | |
| 166 | try { | |
| 167 | option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); | |
| 168 | } | |
| 169 | catch (IllegalArgumentException iae) { | |
| 170 | throw new IllegalArgumentException("unable to parse " + optionStr, iae); | |
| 171 | } | |
| 172 | } | |
| 173 | } | |
Mutations | ||
| 99 |
1.1 |
|
| 104 |
1.1 |
|
| 116 |
1.1 |
|
| 122 |
1.1 |
|
| 130 |
1.1 |
|
| 131 |
1.1 |
|
| 132 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |
|
| 138 |
1.1 |
|
| 139 |
1.1 |
|
| 140 |
1.1 |
|
| 141 |
1.1 |
|
| 143 |
1.1 |
|
| 144 |
1.1 |
|
| 145 |
1.1 |