| 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.Arrays; | |
| 23 | ||
| 24 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 25 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 26 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
| 27 | ||
| 28 | /** | |
| 29 | * <p>Checks the padding of parentheses; that is whether a space is required | |
| 30 | * after a left parenthesis and before a right parenthesis, or such spaces are | |
| 31 | * forbidden. No check occurs at the right parenthesis after an empty for | |
| 32 | * iterator, at the left parenthesis before an empty for initialization, or at | |
| 33 | * the right parenthesis of a try-with-resources resource specification where | |
| 34 | * the last resource variable has a trailing semi-colon. | |
| 35 | * Use Check {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate | |
| 36 | * empty for iterators and {@link EmptyForInitializerPadCheck EmptyForInitializerPad} | |
| 37 | * to validate empty for initializers. Typecasts are also not checked, as there is | |
| 38 | * {@link TypecastParenPadCheck TypecastParenPad} to validate them. | |
| 39 | * </p> | |
| 40 | * <p> | |
| 41 | * The policy to verify is specified using the {@link PadOption} class and | |
| 42 | * defaults to {@link PadOption#NOSPACE}. | |
| 43 | * </p> | |
| 44 | * <p> By default the check will check parentheses that occur with the following | |
| 45 | * tokens: | |
| 46 | * {@link TokenTypes#ANNOTATION ANNOTATION}, | |
| 47 | * {@link TokenTypes#ANNOTATION_FIELD_DEF ANNOTATION_FIELD_DEF}, | |
| 48 | * {@link TokenTypes#CTOR_DEF CTOR_DEF}, | |
| 49 | * {@link TokenTypes#CTOR_CALL CTOR_CALL}, | |
| 50 | * {@link TokenTypes#DOT DOT}, | |
| 51 | * {@link TokenTypes#ENUM_CONSTANT_DEF ENUM_CONSTANT_DEF}, | |
| 52 | * {@link TokenTypes#EXPR EXPR}, | |
| 53 | * {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH}, | |
| 54 | * {@link TokenTypes#LITERAL_DO LITERAL_DO}, | |
| 55 | * {@link TokenTypes#LITERAL_FOR LITERAL_FOR}, | |
| 56 | * {@link TokenTypes#LITERAL_IF LITERAL_IF}, | |
| 57 | * {@link TokenTypes#LITERAL_NEW LITERAL_NEW}, | |
| 58 | * {@link TokenTypes#LITERAL_SWITCH LITERAL_SWITCH}, | |
| 59 | * {@link TokenTypes#LITERAL_SYNCHRONIZED LITERAL_SYNCHRONIZED}, | |
| 60 | * {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE}, | |
| 61 | * {@link TokenTypes#METHOD_CALL METHOD_CALL}, | |
| 62 | * {@link TokenTypes#METHOD_DEF METHOD_DEF}, | |
| 63 | * {@link TokenTypes#RESOURCE_SPECIFICATION RESOURCE_SPECIFICATION}, | |
| 64 | * {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL}, | |
| 65 | * {@link TokenTypes#QUESTION QUESTION}, | |
| 66 | * {@link TokenTypes#LAMBDA LAMBDA}, | |
| 67 | * </p> | |
| 68 | * <p> | |
| 69 | * An example of how to configure the check is: | |
| 70 | * </p> | |
| 71 | * <pre> | |
| 72 | * <module name="ParenPad"/> | |
| 73 | * </pre> | |
| 74 | * <p> | |
| 75 | * An example of how to configure the check to require spaces for the | |
| 76 | * parentheses of constructor, method, and super constructor invocations is: | |
| 77 | * </p> | |
| 78 | * <pre> | |
| 79 | * <module name="ParenPad"> | |
| 80 | * <property name="tokens" | |
| 81 | * value="CTOR_CALL, METHOD_CALL, SUPER_CTOR_CALL"/> | |
| 82 | * <property name="option" value="space"/> | |
| 83 | * </module> | |
| 84 | * </pre> | |
| 85 | * @author Oliver Burn | |
| 86 | * @author Vladislav Lisetskiy | |
| 87 | */ | |
| 88 | public class ParenPadCheck extends AbstractParenPadCheck { | |
| 89 | ||
| 90 | /** | |
| 91 | * The array of Acceptable Tokens. | |
| 92 | */ | |
| 93 | private final int[] acceptableTokens; | |
| 94 | ||
| 95 | /** | |
| 96 | * Initializes and sorts acceptableTokens to make binary search over it possible. | |
| 97 | */ | |
| 98 | public ParenPadCheck() { | |
| 99 | acceptableTokens = makeAcceptableTokens(); | |
| 100 |
1
1. |
Arrays.sort(acceptableTokens); |
| 101 | } | |
| 102 | ||
| 103 | @Override | |
| 104 | public int[] getDefaultTokens() { | |
| 105 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return makeAcceptableTokens(); |
| 106 | } | |
| 107 | ||
| 108 | @Override | |
| 109 | public int[] getAcceptableTokens() { | |
| 110 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return makeAcceptableTokens(); |
| 111 | } | |
| 112 | ||
| 113 | @Override | |
| 114 | public int[] getRequiredTokens() { | |
| 115 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 116 | } | |
| 117 | ||
| 118 | @Override | |
| 119 | public void visitToken(DetailAST ast) { | |
| 120 | switch (ast.getType()) { | |
| 121 | case TokenTypes.METHOD_CALL: | |
| 122 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processLeft → KILLED |
processLeft(ast); |
| 123 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processRight → KILLED |
processRight(ast.findFirstToken(TokenTypes.RPAREN)); |
| 124 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processExpression → SURVIVED |
processExpression(ast); |
| 125 | break; | |
| 126 | case TokenTypes.DOT: | |
| 127 | case TokenTypes.EXPR: | |
| 128 | case TokenTypes.QUESTION: | |
| 129 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processExpression → KILLED |
processExpression(ast); |
| 130 | break; | |
| 131 | case TokenTypes.LITERAL_FOR: | |
| 132 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::visitLiteralFor → KILLED |
visitLiteralFor(ast); |
| 133 | break; | |
| 134 | case TokenTypes.ANNOTATION: | |
| 135 | case TokenTypes.ENUM_CONSTANT_DEF: | |
| 136 | case TokenTypes.LITERAL_NEW: | |
| 137 | case TokenTypes.LITERAL_SYNCHRONIZED: | |
| 138 | case TokenTypes.LAMBDA: | |
| 139 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::visitTokenWithOptionalParentheses → KILLED |
visitTokenWithOptionalParentheses(ast); |
| 140 | break; | |
| 141 | case TokenTypes.RESOURCE_SPECIFICATION: | |
| 142 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::visitResourceSpecification → KILLED |
visitResourceSpecification(ast); |
| 143 | break; | |
| 144 | default: | |
| 145 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processLeft → KILLED |
processLeft(ast.findFirstToken(TokenTypes.LPAREN)); |
| 146 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processRight → KILLED |
processRight(ast.findFirstToken(TokenTypes.RPAREN)); |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * Checks parens in token which may not contain parens, e.g. | |
| 152 | * {@link TokenTypes#ENUM_CONSTANT_DEF}, {@link TokenTypes#ANNOTATION} | |
| 153 | * {@link TokenTypes#LITERAL_SYNCHRONIZED}, {@link TokenTypes#LITERAL_NEW} and | |
| 154 | * {@link TokenTypes#LAMBDA}. | |
| 155 | * @param ast the token to check. | |
| 156 | */ | |
| 157 | private void visitTokenWithOptionalParentheses(DetailAST ast) { | |
| 158 | final DetailAST parenAst = ast.findFirstToken(TokenTypes.LPAREN); | |
| 159 |
1
1. visitTokenWithOptionalParentheses : negated conditional → KILLED |
if (parenAst != null) { |
| 160 |
1
1. visitTokenWithOptionalParentheses : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processLeft → KILLED |
processLeft(parenAst); |
| 161 |
1
1. visitTokenWithOptionalParentheses : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processRight → KILLED |
processRight(ast.findFirstToken(TokenTypes.RPAREN)); |
| 162 | } | |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * Checks parens in {@link TokenTypes#RESOURCE_SPECIFICATION}. | |
| 167 | * @param ast the token to check. | |
| 168 | */ | |
| 169 | private void visitResourceSpecification(DetailAST ast) { | |
| 170 |
1
1. visitResourceSpecification : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processLeft → KILLED |
processLeft(ast.findFirstToken(TokenTypes.LPAREN)); |
| 171 | final DetailAST rparen = ast.findFirstToken(TokenTypes.RPAREN); | |
| 172 |
1
1. visitResourceSpecification : negated conditional → KILLED |
if (!hasPrecedingSemiColon(rparen)) { |
| 173 |
1
1. visitResourceSpecification : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processRight → KILLED |
processRight(rparen); |
| 174 | } | |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * Checks that a token is preceded by a semi-colon. | |
| 179 | * @param ast the token to check | |
| 180 | * @return whether a token is preceded by a semi-colon | |
| 181 | */ | |
| 182 | private static boolean hasPrecedingSemiColon(DetailAST ast) { | |
| 183 |
2
1. hasPrecedingSemiColon : negated conditional → KILLED 2. hasPrecedingSemiColon : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return ast.getPreviousSibling().getType() == TokenTypes.SEMI; |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Checks parens in {@link TokenTypes#LITERAL_FOR}. | |
| 188 | * @param ast the token to check. | |
| 189 | */ | |
| 190 | private void visitLiteralFor(DetailAST ast) { | |
| 191 | final DetailAST lparen = ast.findFirstToken(TokenTypes.LPAREN); | |
| 192 |
1
1. visitLiteralFor : negated conditional → KILLED |
if (!isPrecedingEmptyForInit(lparen)) { |
| 193 |
1
1. visitLiteralFor : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processLeft → KILLED |
processLeft(lparen); |
| 194 | } | |
| 195 | final DetailAST rparen = ast.findFirstToken(TokenTypes.RPAREN); | |
| 196 |
1
1. visitLiteralFor : negated conditional → KILLED |
if (!isFollowsEmptyForIterator(rparen)) { |
| 197 |
1
1. visitLiteralFor : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processRight → KILLED |
processRight(rparen); |
| 198 | } | |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * Checks parens inside {@link TokenTypes#EXPR}, {@link TokenTypes#QUESTION} | |
| 203 | * and {@link TokenTypes#METHOD_CALL}. | |
| 204 | * @param ast the token to check. | |
| 205 | */ | |
| 206 | private void processExpression(DetailAST ast) { | |
| 207 |
1
1. processExpression : negated conditional → KILLED |
if (ast.branchContains(TokenTypes.LPAREN)) { |
| 208 | DetailAST childAst = ast.getFirstChild(); | |
| 209 |
1
1. processExpression : negated conditional → KILLED |
while (childAst != null) { |
| 210 |
1
1. processExpression : negated conditional → KILLED |
if (childAst.getType() == TokenTypes.LPAREN) { |
| 211 |
1
1. processExpression : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processLeft → KILLED |
processLeft(childAst); |
| 212 |
1
1. processExpression : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processExpression → SURVIVED |
processExpression(childAst); |
| 213 | } | |
| 214 |
2
1. processExpression : negated conditional → KILLED 2. processExpression : negated conditional → KILLED |
else if (childAst.getType() == TokenTypes.RPAREN && !isInTypecast(childAst)) { |
| 215 |
1
1. processExpression : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processRight → KILLED |
processRight(childAst); |
| 216 | } | |
| 217 |
1
1. processExpression : negated conditional → KILLED |
else if (!isAcceptableToken(childAst)) { |
| 218 | //Traverse all subtree tokens which will never be configured | |
| 219 | //to be launched in visitToken() | |
| 220 |
1
1. processExpression : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::processExpression → KILLED |
processExpression(childAst); |
| 221 | } | |
| 222 | childAst = childAst.getNextSibling(); | |
| 223 | } | |
| 224 | } | |
| 225 | } | |
| 226 | ||
| 227 | /** | |
| 228 | * Checks whether AcceptableTokens contains the given ast. | |
| 229 | * @param ast the token to check. | |
| 230 | * @return true if the ast is in AcceptableTokens. | |
| 231 | */ | |
| 232 | private boolean isAcceptableToken(DetailAST ast) { | |
| 233 | boolean result = false; | |
| 234 |
2
1. isAcceptableToken : changed conditional boundary → SURVIVED 2. isAcceptableToken : negated conditional → KILLED |
if (Arrays.binarySearch(acceptableTokens, ast.getType()) >= 0) { |
| 235 | result = true; | |
| 236 | } | |
| 237 |
1
1. isAcceptableToken : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 238 | } | |
| 239 | ||
| 240 | /** | |
| 241 | * Returns array of acceptable tokens. | |
| 242 | * @return acceptableTokens. | |
| 243 | */ | |
| 244 | private static int[] makeAcceptableTokens() { | |
| 245 |
1
1. makeAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck::makeAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.ANNOTATION, |
| 246 | TokenTypes.ANNOTATION_FIELD_DEF, | |
| 247 | TokenTypes.CTOR_CALL, | |
| 248 | TokenTypes.CTOR_DEF, | |
| 249 | TokenTypes.DOT, | |
| 250 | TokenTypes.ENUM_CONSTANT_DEF, | |
| 251 | TokenTypes.EXPR, | |
| 252 | TokenTypes.LITERAL_CATCH, | |
| 253 | TokenTypes.LITERAL_DO, | |
| 254 | TokenTypes.LITERAL_FOR, | |
| 255 | TokenTypes.LITERAL_IF, | |
| 256 | TokenTypes.LITERAL_NEW, | |
| 257 | TokenTypes.LITERAL_SWITCH, | |
| 258 | TokenTypes.LITERAL_SYNCHRONIZED, | |
| 259 | TokenTypes.LITERAL_WHILE, | |
| 260 | TokenTypes.METHOD_CALL, | |
| 261 | TokenTypes.METHOD_DEF, | |
| 262 | TokenTypes.QUESTION, | |
| 263 | TokenTypes.RESOURCE_SPECIFICATION, | |
| 264 | TokenTypes.SUPER_CTOR_CALL, | |
| 265 | TokenTypes.LAMBDA, | |
| 266 | }; | |
| 267 | } | |
| 268 | ||
| 269 | /** | |
| 270 | * Checks whether {@link TokenTypes#RPAREN} is a closing paren | |
| 271 | * of a {@link TokenTypes#TYPECAST}. | |
| 272 | * @param ast of a {@link TokenTypes#RPAREN} to check. | |
| 273 | * @return true if ast is a closing paren of a {@link TokenTypes#TYPECAST}. | |
| 274 | */ | |
| 275 | private static boolean isInTypecast(DetailAST ast) { | |
| 276 | boolean result = false; | |
| 277 |
1
1. isInTypecast : negated conditional → KILLED |
if (ast.getParent().getType() == TokenTypes.TYPECAST) { |
| 278 | final DetailAST firstRparen = ast.getParent().findFirstToken(TokenTypes.RPAREN); | |
| 279 |
1
1. isInTypecast : negated conditional → KILLED |
if (firstRparen.getLineNo() == ast.getLineNo() |
| 280 |
1
1. isInTypecast : negated conditional → KILLED |
&& firstRparen.getColumnNo() == ast.getColumnNo()) { |
| 281 | result = true; | |
| 282 | } | |
| 283 | } | |
| 284 |
1
1. isInTypecast : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 285 | } | |
| 286 | ||
| 287 | /** | |
| 288 | * Checks that a token follows an empty for iterator. | |
| 289 | * @param ast the token to check | |
| 290 | * @return whether a token follows an empty for iterator | |
| 291 | */ | |
| 292 | private static boolean isFollowsEmptyForIterator(DetailAST ast) { | |
| 293 | boolean result = false; | |
| 294 | final DetailAST parent = ast.getParent(); | |
| 295 | //Only traditional for statements are examined, not for-each statements | |
| 296 |
1
1. isFollowsEmptyForIterator : negated conditional → KILLED |
if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) { |
| 297 | final DetailAST forIterator = | |
| 298 | parent.findFirstToken(TokenTypes.FOR_ITERATOR); | |
| 299 |
1
1. isFollowsEmptyForIterator : negated conditional → KILLED |
result = forIterator.getChildCount() == 0; |
| 300 | } | |
| 301 |
1
1. isFollowsEmptyForIterator : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 302 | } | |
| 303 | ||
| 304 | /** | |
| 305 | * Checks that a token precedes an empty for initializer. | |
| 306 | * @param ast the token to check | |
| 307 | * @return whether a token precedes an empty for initializer | |
| 308 | */ | |
| 309 | private static boolean isPrecedingEmptyForInit(DetailAST ast) { | |
| 310 | boolean result = false; | |
| 311 | final DetailAST parent = ast.getParent(); | |
| 312 | //Only traditional for statements are examined, not for-each statements | |
| 313 |
1
1. isPrecedingEmptyForInit : negated conditional → KILLED |
if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) { |
| 314 | final DetailAST forIterator = | |
| 315 | parent.findFirstToken(TokenTypes.FOR_INIT); | |
| 316 |
1
1. isPrecedingEmptyForInit : negated conditional → KILLED |
result = forIterator.getChildCount() == 0; |
| 317 | } | |
| 318 |
1
1. isPrecedingEmptyForInit : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 319 | } | |
| 320 | } | |
Mutations | ||
| 100 |
1.1 |
|
| 105 |
1.1 |
|
| 110 |
1.1 |
|
| 115 |
1.1 |
|
| 122 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 129 |
1.1 |
|
| 132 |
1.1 |
|
| 139 |
1.1 |
|
| 142 |
1.1 |
|
| 145 |
1.1 |
|
| 146 |
1.1 |
|
| 159 |
1.1 |
|
| 160 |
1.1 |
|
| 161 |
1.1 |
|
| 170 |
1.1 |
|
| 172 |
1.1 |
|
| 173 |
1.1 |
|
| 183 |
1.1 2.2 |
|
| 192 |
1.1 |
|
| 193 |
1.1 |
|
| 196 |
1.1 |
|
| 197 |
1.1 |
|
| 207 |
1.1 |
|
| 209 |
1.1 |
|
| 210 |
1.1 |
|
| 211 |
1.1 |
|
| 212 |
1.1 |
|
| 214 |
1.1 2.2 |
|
| 215 |
1.1 |
|
| 217 |
1.1 |
|
| 220 |
1.1 |
|
| 234 |
1.1 2.2 |
|
| 237 |
1.1 |
|
| 245 |
1.1 |
|
| 277 |
1.1 |
|
| 279 |
1.1 |
|
| 280 |
1.1 |
|
| 284 |
1.1 |
|
| 296 |
1.1 |
|
| 299 |
1.1 |
|
| 301 |
1.1 |
|
| 313 |
1.1 |
|
| 316 |
1.1 |
|
| 318 |
1.1 |