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 com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
24 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
25 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
26 | ||
27 | /** | |
28 | * <p> | |
29 | * Checks that the whitespace around the Generic tokens (angle brackets) | |
30 | * "<" and ">" are correct to the <i>typical</i> convention. | |
31 | * The convention is not configurable. | |
32 | * </p> | |
33 | * <br> | |
34 | * <p> | |
35 | * Left angle bracket ("<"): | |
36 | * </p> | |
37 | * <br> | |
38 | * <ul> | |
39 | * <li> should be preceded with whitespace only | |
40 | * in generic methods definitions.</li> | |
41 | * <li> should not be preceded with whitespace | |
42 | * when it is precede method name or following type name.</li> | |
43 | * <li> should not be followed with whitespace in all cases.</li> | |
44 | * </ul> | |
45 | * <br> | |
46 | * <p> | |
47 | * Right angle bracket (">"): | |
48 | * </p> | |
49 | * <br> | |
50 | * <ul> | |
51 | * <li> should not be preceded with whitespace in all cases.</li> | |
52 | * <li> should be followed with whitespace in almost all cases, | |
53 | * except diamond operators and when preceding method name.</li></ul> | |
54 | * <br> | |
55 | * <p> | |
56 | * Examples with correct spacing: | |
57 | * </p> | |
58 | * <br> | |
59 | * <pre> | |
60 | * public void <K, V extends Number> boolean foo(K, V) {} // Generic methods definitions | |
61 | * class name<T1, T2, ..., Tn> {} // Generic type definition | |
62 | * OrderedPair<String, Box<Integer>> p; // Generic type reference | |
63 | * boolean same = Util.<Integer, String>compare(p1, p2); // Generic preceded method name | |
64 | * Pair<Integer, String> p1 = new Pair<>(1, "apple");// Diamond operator | |
65 | * List<T> list = ImmutableList.Builder<T>::new; // Method reference | |
66 | * sort(list, Comparable::<String>compareTo); // Method reference | |
67 | * </pre> | |
68 | * @author Oliver Burn | |
69 | */ | |
70 | public class GenericWhitespaceCheck extends AbstractCheck { | |
71 | ||
72 | /** | |
73 | * A key is pointing to the warning message text in "messages.properties" | |
74 | * file. | |
75 | */ | |
76 | public static final String MSG_WS_PRECEDED = "ws.preceded"; | |
77 | ||
78 | /** | |
79 | * A key is pointing to the warning message text in "messages.properties" | |
80 | * file. | |
81 | */ | |
82 | public static final String MSG_WS_FOLLOWED = "ws.followed"; | |
83 | ||
84 | /** | |
85 | * A key is pointing to the warning message text in "messages.properties" | |
86 | * file. | |
87 | */ | |
88 | public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; | |
89 | ||
90 | /** | |
91 | * A key is pointing to the warning message text in "messages.properties" | |
92 | * file. | |
93 | */ | |
94 | public static final String MSG_WS_ILLEGAL_FOLLOW = "ws.illegalFollow"; | |
95 | ||
96 | /** Open angle bracket literal. */ | |
97 | private static final String OPEN_ANGLE_BRACKET = "<"; | |
98 | ||
99 | /** Close angle bracket literal. */ | |
100 | private static final String CLOSE_ANGLE_BRACKET = ">"; | |
101 | ||
102 | /** Used to count the depth of a Generic expression. */ | |
103 | private int depth; | |
104 | ||
105 | @Override | |
106 | public int[] getDefaultTokens() { | |
107 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
108 | } | |
109 | ||
110 | @Override | |
111 | public int[] getAcceptableTokens() { | |
112 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.GENERIC_START, TokenTypes.GENERIC_END}; |
113 | } | |
114 | ||
115 | @Override | |
116 | public int[] getRequiredTokens() { | |
117 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getAcceptableTokens(); |
118 | } | |
119 | ||
120 | @Override | |
121 | public void beginTree(DetailAST rootAST) { | |
122 | // Reset for each tree, just increase there are errors in preceding | |
123 | // trees. | |
124 | depth = 0; | |
125 | } | |
126 | ||
127 | @Override | |
128 | public void visitToken(DetailAST ast) { | |
129 | switch (ast.getType()) { | |
130 | case TokenTypes.GENERIC_START: | |
131 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::processStart → KILLED |
processStart(ast); |
132 |
1
1. visitToken : Replaced integer addition with subtraction → KILLED |
depth++; |
133 | break; | |
134 | case TokenTypes.GENERIC_END: | |
135 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::processEnd → KILLED |
processEnd(ast); |
136 |
1
1. visitToken : Replaced integer subtraction with addition → KILLED |
depth--; |
137 | break; | |
138 | default: | |
139 | throw new IllegalArgumentException("Unknown type " + ast); | |
140 | } | |
141 | } | |
142 | ||
143 | /** | |
144 | * Checks the token for the end of Generics. | |
145 | * @param ast the token to check | |
146 | */ | |
147 | private void processEnd(DetailAST ast) { | |
148 |
1
1. processEnd : Replaced integer subtraction with addition → KILLED |
final String line = getLine(ast.getLineNo() - 1); |
149 |
1
1. processEnd : Replaced integer subtraction with addition → KILLED |
final int before = ast.getColumnNo() - 1; |
150 |
1
1. processEnd : Replaced integer addition with subtraction → KILLED |
final int after = ast.getColumnNo() + 1; |
151 | ||
152 |
3
1. processEnd : changed conditional boundary → KILLED 2. processEnd : negated conditional → KILLED 3. processEnd : negated conditional → KILLED |
if (before >= 0 && Character.isWhitespace(line.charAt(before)) |
153 |
1
1. processEnd : negated conditional → KILLED |
&& !containsWhitespaceBefore(before, line)) { |
154 |
1
1. processEnd : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), before, MSG_WS_PRECEDED, CLOSE_ANGLE_BRACKET); |
155 | } | |
156 | ||
157 |
2
1. processEnd : changed conditional boundary → KILLED 2. processEnd : negated conditional → KILLED |
if (after < line.length()) { |
158 | ||
159 | // Check if the last Generic, in which case must be a whitespace | |
160 | // or a '(),[.'. | |
161 |
1
1. processEnd : negated conditional → KILLED |
if (depth == 1) { |
162 |
1
1. processEnd : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::processSingleGeneric → KILLED |
processSingleGeneric(ast, line, after); |
163 | } | |
164 | else { | |
165 |
1
1. processEnd : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::processNestedGenerics → KILLED |
processNestedGenerics(ast, line, after); |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | /** | |
171 | * Process Nested generics. | |
172 | * @param ast token | |
173 | * @param line line content | |
174 | * @param after position after | |
175 | */ | |
176 | private void processNestedGenerics(DetailAST ast, String line, int after) { | |
177 | // In a nested Generic type, so can only be a '>' or ',' or '&' | |
178 | ||
179 | // In case of several extends definitions: | |
180 | // | |
181 | // class IntEnumValueType<E extends Enum<E> & IntEnum> | |
182 | // ^ | |
183 | // should be whitespace if followed by & -+ | |
184 | // | |
185 | final int indexOfAmp = line.indexOf('&', after); | |
186 |
2
1. processNestedGenerics : changed conditional boundary → KILLED 2. processNestedGenerics : negated conditional → KILLED |
if (indexOfAmp >= 1 |
187 |
1
1. processNestedGenerics : negated conditional → KILLED |
&& containsWhitespaceBetween(after, indexOfAmp, line)) { |
188 |
2
1. processNestedGenerics : Replaced integer subtraction with addition → KILLED 2. processNestedGenerics : negated conditional → KILLED |
if (indexOfAmp - after == 0) { |
189 |
1
1. processNestedGenerics : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), after, MSG_WS_NOT_PRECEDED, "&"); |
190 | } | |
191 |
2
1. processNestedGenerics : Replaced integer subtraction with addition → KILLED 2. processNestedGenerics : negated conditional → KILLED |
else if (indexOfAmp - after != 1) { |
192 |
1
1. processNestedGenerics : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), after, MSG_WS_FOLLOWED, CLOSE_ANGLE_BRACKET); |
193 | } | |
194 | } | |
195 |
1
1. processNestedGenerics : negated conditional → KILLED |
else if (line.charAt(after) == ' ') { |
196 |
1
1. processNestedGenerics : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), after, MSG_WS_FOLLOWED, CLOSE_ANGLE_BRACKET); |
197 | } | |
198 | } | |
199 | ||
200 | /** | |
201 | * Process Single-generic. | |
202 | * @param ast token | |
203 | * @param line line content | |
204 | * @param after position after | |
205 | */ | |
206 | private void processSingleGeneric(DetailAST ast, String line, int after) { | |
207 | final char charAfter = line.charAt(after); | |
208 | ||
209 | // Need to handle a number of cases. First is: | |
210 | // Collections.<Object>emptySet(); | |
211 | // ^ | |
212 | // +--- whitespace not allowed | |
213 |
1
1. processSingleGeneric : negated conditional → KILLED |
if (isGenericBeforeMethod(ast)) { |
214 |
1
1. processSingleGeneric : negated conditional → KILLED |
if (Character.isWhitespace(charAfter)) { |
215 |
1
1. processSingleGeneric : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), after, MSG_WS_FOLLOWED, CLOSE_ANGLE_BRACKET); |
216 | } | |
217 | } | |
218 |
1
1. processSingleGeneric : negated conditional → KILLED |
else if (!isCharacterValidAfterGenericEnd(charAfter)) { |
219 |
1
1. processSingleGeneric : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), after, MSG_WS_ILLEGAL_FOLLOW, CLOSE_ANGLE_BRACKET); |
220 | } | |
221 | } | |
222 | ||
223 | /** | |
224 | * Is generic before method reference. | |
225 | * @param ast ast | |
226 | * @return true if generic before a method ref | |
227 | */ | |
228 | private static boolean isGenericBeforeMethod(DetailAST ast) { | |
229 |
2
1. isGenericBeforeMethod : negated conditional → KILLED 2. isGenericBeforeMethod : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return ast.getParent().getType() == TokenTypes.TYPE_ARGUMENTS |
230 |
1
1. isGenericBeforeMethod : negated conditional → KILLED |
&& ast.getParent().getParent().getType() == TokenTypes.DOT |
231 |
1
1. isGenericBeforeMethod : negated conditional → KILLED |
&& ast.getParent().getParent().getParent().getType() == TokenTypes.METHOD_CALL |
232 |
1
1. isGenericBeforeMethod : negated conditional → KILLED |
|| isAfterMethodReference(ast); |
233 | } | |
234 | ||
235 | /** | |
236 | * Checks if current generic end ('>') is located after | |
237 | * {@link TokenTypes#METHOD_REF method reference operator}. | |
238 | * @param genericEnd {@link TokenTypes#GENERIC_END} | |
239 | * @return true if '>' follows after method reference. | |
240 | */ | |
241 | private static boolean isAfterMethodReference(DetailAST genericEnd) { | |
242 |
2
1. isAfterMethodReference : negated conditional → KILLED 2. isAfterMethodReference : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return genericEnd.getParent().getParent().getType() == TokenTypes.METHOD_REF; |
243 | } | |
244 | ||
245 | /** | |
246 | * Checks the token for the start of Generics. | |
247 | * @param ast the token to check | |
248 | */ | |
249 | private void processStart(DetailAST ast) { | |
250 |
1
1. processStart : Replaced integer subtraction with addition → KILLED |
final String line = getLine(ast.getLineNo() - 1); |
251 |
1
1. processStart : Replaced integer subtraction with addition → KILLED |
final int before = ast.getColumnNo() - 1; |
252 |
1
1. processStart : Replaced integer addition with subtraction → KILLED |
final int after = ast.getColumnNo() + 1; |
253 | ||
254 | // Need to handle two cases as in: | |
255 | // | |
256 | // public static <T> Callable<T> callable(Runnable task, T result) | |
257 | // ^ ^ | |
258 | // ws reqd ---+ +--- whitespace NOT required | |
259 | // | |
260 |
2
1. processStart : changed conditional boundary → KILLED 2. processStart : negated conditional → KILLED |
if (before >= 0) { |
261 | // Detect if the first case | |
262 | final DetailAST parent = ast.getParent(); | |
263 | final DetailAST grandparent = parent.getParent(); | |
264 |
1
1. processStart : negated conditional → KILLED |
if (parent.getType() == TokenTypes.TYPE_PARAMETERS |
265 |
1
1. processStart : negated conditional → KILLED |
&& (grandparent.getType() == TokenTypes.CTOR_DEF |
266 |
1
1. processStart : negated conditional → KILLED |
|| grandparent.getType() == TokenTypes.METHOD_DEF)) { |
267 | // Require whitespace | |
268 |
1
1. processStart : negated conditional → KILLED |
if (!Character.isWhitespace(line.charAt(before))) { |
269 |
1
1. processStart : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), before, MSG_WS_NOT_PRECEDED, OPEN_ANGLE_BRACKET); |
270 | } | |
271 | } | |
272 | // Whitespace not required | |
273 |
1
1. processStart : negated conditional → KILLED |
else if (Character.isWhitespace(line.charAt(before)) |
274 |
1
1. processStart : negated conditional → KILLED |
&& !containsWhitespaceBefore(before, line)) { |
275 |
1
1. processStart : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), before, MSG_WS_PRECEDED, OPEN_ANGLE_BRACKET); |
276 | } | |
277 | } | |
278 | ||
279 |
2
1. processStart : changed conditional boundary → KILLED 2. processStart : negated conditional → KILLED |
if (after < line.length() |
280 |
1
1. processStart : negated conditional → KILLED |
&& Character.isWhitespace(line.charAt(after))) { |
281 |
1
1. processStart : removed call to com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck::log → KILLED |
log(ast.getLineNo(), after, MSG_WS_FOLLOWED, OPEN_ANGLE_BRACKET); |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
286 | * Returns whether the specified string contains only whitespace between | |
287 | * specified indices. | |
288 | * | |
289 | * @param fromIndex the index to start the search from. Inclusive | |
290 | * @param toIndex the index to finish the search. Exclusive | |
291 | * @param line the line to check | |
292 | * @return whether there are only whitespaces (or nothing) | |
293 | */ | |
294 | private static boolean containsWhitespaceBetween(int fromIndex, int toIndex, String line) { | |
295 | boolean result = true; | |
296 |
3
1. containsWhitespaceBetween : changed conditional boundary → KILLED 2. containsWhitespaceBetween : Changed increment from 1 to -1 → KILLED 3. containsWhitespaceBetween : negated conditional → KILLED |
for (int i = fromIndex; i < toIndex; i++) { |
297 |
1
1. containsWhitespaceBetween : negated conditional → KILLED |
if (!Character.isWhitespace(line.charAt(i))) { |
298 | result = false; | |
299 | break; | |
300 | } | |
301 | } | |
302 |
1
1. containsWhitespaceBetween : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
303 | } | |
304 | ||
305 | /** | |
306 | * Returns whether the specified string contains only whitespace up to specified index. | |
307 | * | |
308 | * @param before the index to start the search from. Inclusive | |
309 | * @param line the index to finish the search. Exclusive | |
310 | * @return {@code true} if there are only whitespaces, | |
311 | * false if there is nothing before or some other characters | |
312 | */ | |
313 | private static boolean containsWhitespaceBefore(int before, String line) { | |
314 |
3
1. containsWhitespaceBefore : negated conditional → KILLED 2. containsWhitespaceBefore : negated conditional → KILLED 3. containsWhitespaceBefore : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return before != 0 && CommonUtils.hasWhitespaceBefore(before, line); |
315 | } | |
316 | ||
317 | /** | |
318 | * Checks whether given character is valid to be right after generic ends. | |
319 | * @param charAfter character to check | |
320 | * @return checks if given character is valid | |
321 | */ | |
322 | private static boolean isCharacterValidAfterGenericEnd(char charAfter) { | |
323 |
8
1. isCharacterValidAfterGenericEnd : negated conditional → KILLED 2. isCharacterValidAfterGenericEnd : negated conditional → KILLED 3. isCharacterValidAfterGenericEnd : negated conditional → KILLED 4. isCharacterValidAfterGenericEnd : negated conditional → KILLED 5. isCharacterValidAfterGenericEnd : negated conditional → KILLED 6. isCharacterValidAfterGenericEnd : negated conditional → KILLED 7. isCharacterValidAfterGenericEnd : negated conditional → KILLED 8. isCharacterValidAfterGenericEnd : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return charAfter == '(' || charAfter == ')' |
324 | || charAfter == ',' || charAfter == '[' | |
325 | || charAfter == '.' || charAfter == ':' | |
326 | || charAfter == ';' | |
327 |
1
1. isCharacterValidAfterGenericEnd : negated conditional → KILLED |
|| Character.isWhitespace(charAfter); |
328 | } | |
329 | } | |
Mutations | ||
107 |
1.1 |
|
112 |
1.1 |
|
117 |
1.1 |
|
131 |
1.1 |
|
132 |
1.1 |
|
135 |
1.1 |
|
136 |
1.1 |
|
148 |
1.1 |
|
149 |
1.1 |
|
150 |
1.1 |
|
152 |
1.1 2.2 3.3 |
|
153 |
1.1 |
|
154 |
1.1 |
|
157 |
1.1 2.2 |
|
161 |
1.1 |
|
162 |
1.1 |
|
165 |
1.1 |
|
186 |
1.1 2.2 |
|
187 |
1.1 |
|
188 |
1.1 2.2 |
|
189 |
1.1 |
|
191 |
1.1 2.2 |
|
192 |
1.1 |
|
195 |
1.1 |
|
196 |
1.1 |
|
213 |
1.1 |
|
214 |
1.1 |
|
215 |
1.1 |
|
218 |
1.1 |
|
219 |
1.1 |
|
229 |
1.1 2.2 |
|
230 |
1.1 |
|
231 |
1.1 |
|
232 |
1.1 |
|
242 |
1.1 2.2 |
|
250 |
1.1 |
|
251 |
1.1 |
|
252 |
1.1 |
|
260 |
1.1 2.2 |
|
264 |
1.1 |
|
265 |
1.1 |
|
266 |
1.1 |
|
268 |
1.1 |
|
269 |
1.1 |
|
273 |
1.1 |
|
274 |
1.1 |
|
275 |
1.1 |
|
279 |
1.1 2.2 |
|
280 |
1.1 |
|
281 |
1.1 |
|
296 |
1.1 2.2 3.3 |
|
297 |
1.1 |
|
302 |
1.1 |
|
314 |
1.1 2.2 3.3 |
|
323 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
327 |
1.1 |