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