1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.whitespace;
21
22 import java.util.stream.IntStream;
23
24 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26 import com.puppycrawl.tools.checkstyle.api.DetailAST;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28 import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @FileStatefulCheck
62 public class GenericWhitespaceCheck extends AbstractCheck {
63
64
65
66
67
68 public static final String MSG_WS_PRECEDED = "ws.preceded";
69
70
71
72
73
74 public static final String MSG_WS_FOLLOWED = "ws.followed";
75
76
77
78
79
80 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
81
82
83
84
85
86 public static final String MSG_WS_ILLEGAL_FOLLOW = "ws.illegalFollow";
87
88
89 private static final String OPEN_ANGLE_BRACKET = "<";
90
91
92 private static final String CLOSE_ANGLE_BRACKET = ">";
93
94
95 private int depth;
96
97 @Override
98 public int[] getDefaultTokens() {
99 return getRequiredTokens();
100 }
101
102 @Override
103 public int[] getAcceptableTokens() {
104 return getRequiredTokens();
105 }
106
107 @Override
108 public int[] getRequiredTokens() {
109 return new int[] {TokenTypes.GENERIC_START, TokenTypes.GENERIC_END};
110 }
111
112 @Override
113 public void beginTree(DetailAST rootAST) {
114
115
116 depth = 0;
117 }
118
119 @Override
120 public void visitToken(DetailAST ast) {
121 switch (ast.getType()) {
122 case TokenTypes.GENERIC_START -> {
123 processStart(ast);
124 depth++;
125 }
126 case TokenTypes.GENERIC_END -> {
127 processEnd(ast);
128 depth--;
129 }
130 default -> throw new IllegalArgumentException("Unknown type " + ast);
131 }
132 }
133
134
135
136
137
138
139 private void processEnd(DetailAST ast) {
140 final int[] line = getLineCodePoints(ast.getLineNo() - 1);
141 final int before = ast.getColumnNo() - 1;
142 final int after = ast.getColumnNo() + 1;
143
144 if (before >= 0 && CommonUtil.isCodePointWhitespace(line, before)
145 && !containsWhitespaceBefore(before, line)) {
146 log(ast, MSG_WS_PRECEDED, CLOSE_ANGLE_BRACKET);
147 }
148
149 if (after < line.length) {
150
151
152 if (depth == 1) {
153 processSingleGeneric(ast, line, after);
154 }
155 else {
156 processNestedGenerics(ast, line, after);
157 }
158 }
159 }
160
161
162
163
164
165
166
167
168 private void processNestedGenerics(DetailAST ast, int[] line, int after) {
169
170
171
172
173
174
175
176
177 final int indexOfAmp = IntStream.range(after, line.length)
178 .filter(index -> line[index] == '&')
179 .findFirst()
180 .orElse(-1);
181 if (indexOfAmp >= 1
182 && containsWhitespaceBetween(after, indexOfAmp, line)) {
183 if (indexOfAmp - after == 0) {
184 log(ast, MSG_WS_NOT_PRECEDED, "&");
185 }
186 else if (indexOfAmp - after != 1) {
187 log(ast, MSG_WS_FOLLOWED, CLOSE_ANGLE_BRACKET);
188 }
189 }
190 else if (line[after] == ' ') {
191 log(ast, MSG_WS_FOLLOWED, CLOSE_ANGLE_BRACKET);
192 }
193 }
194
195
196
197
198
199
200
201
202 private void processSingleGeneric(DetailAST ast, int[] line, int after) {
203 final char charAfter = Character.toChars(line[after])[0];
204 if (isGenericBeforeMethod(ast)
205 || isGenericBeforeCtorInvocation(ast)
206 || isGenericBeforeRecordHeader(ast)) {
207 if (Character.isWhitespace(charAfter)) {
208 log(ast, MSG_WS_FOLLOWED, CLOSE_ANGLE_BRACKET);
209 }
210 }
211 else if (!isCharacterValidAfterGenericEnd(charAfter)) {
212 log(ast, MSG_WS_ILLEGAL_FOLLOW, CLOSE_ANGLE_BRACKET);
213 }
214 }
215
216
217
218
219
220
221
222
223
224
225
226 private static boolean isGenericBeforeRecordHeader(DetailAST ast) {
227 final DetailAST grandParent = ast.getParent().getParent();
228 return grandParent.getType() == TokenTypes.RECORD_DEF
229 || grandParent.getParent().getType() == TokenTypes.RECORD_PATTERN_DEF;
230 }
231
232
233
234
235
236
237
238
239
240
241
242 private static boolean isGenericBeforeCtorInvocation(DetailAST ast) {
243 final DetailAST grandParent = ast.getParent().getParent();
244 return grandParent.getType() == TokenTypes.LITERAL_NEW
245 || grandParent.getParent().getType() == TokenTypes.LITERAL_NEW;
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259 private static boolean isGenericAfterNew(DetailAST ast) {
260 final DetailAST parent = ast.getParent();
261 return parent.getParent().getType() == TokenTypes.LITERAL_NEW
262 && (parent.getNextSibling().getType() == TokenTypes.IDENT
263 || parent.getNextSibling().getType() == TokenTypes.DOT
264 || parent.getNextSibling().getType() == TokenTypes.ANNOTATIONS);
265 }
266
267
268
269
270
271
272
273 private static boolean isGenericBeforeMethod(DetailAST ast) {
274 return ast.getParent().getParent().getParent().getType() == TokenTypes.METHOD_CALL
275 || isAfterMethodReference(ast);
276 }
277
278
279
280
281
282
283
284
285 private static boolean isAfterMethodReference(DetailAST genericEnd) {
286 return genericEnd.getParent().getParent().getType() == TokenTypes.METHOD_REF;
287 }
288
289
290
291
292
293
294 private void processStart(DetailAST ast) {
295 final int[] line = getLineCodePoints(ast.getLineNo() - 1);
296 final int before = ast.getColumnNo() - 1;
297 final int after = ast.getColumnNo() + 1;
298
299
300
301
302
303
304
305
306
307
308
309 if (before >= 0) {
310 final DetailAST parent = ast.getParent();
311 final DetailAST grandparent = parent.getParent();
312
313 if (grandparent.getType() == TokenTypes.CTOR_DEF
314 || grandparent.getType() == TokenTypes.METHOD_DEF
315 || isGenericAfterNew(ast)) {
316
317 if (!CommonUtil.isCodePointWhitespace(line, before)) {
318 log(ast, MSG_WS_NOT_PRECEDED, OPEN_ANGLE_BRACKET);
319 }
320 }
321
322 else if (CommonUtil.isCodePointWhitespace(line, before)
323 && !containsWhitespaceBefore(before, line)) {
324 log(ast, MSG_WS_PRECEDED, OPEN_ANGLE_BRACKET);
325 }
326 }
327
328 if (after < line.length
329 && CommonUtil.isCodePointWhitespace(line, after)) {
330 log(ast, MSG_WS_FOLLOWED, OPEN_ANGLE_BRACKET);
331 }
332 }
333
334
335
336
337
338
339
340
341
342
343 private static boolean containsWhitespaceBetween(int fromIndex, int toIndex, int... line) {
344 boolean result = true;
345 for (int i = fromIndex; i < toIndex; i++) {
346 if (!CommonUtil.isCodePointWhitespace(line, i)) {
347 result = false;
348 break;
349 }
350 }
351 return result;
352 }
353
354
355
356
357
358
359
360
361
362 private static boolean containsWhitespaceBefore(int before, int... line) {
363 return before != 0 && CodePointUtil.hasWhitespaceBefore(before, line);
364 }
365
366
367
368
369
370
371
372 private static boolean isCharacterValidAfterGenericEnd(char charAfter) {
373 return charAfter == ')' || charAfter == ','
374 || charAfter == '[' || charAfter == '.'
375 || charAfter == ':' || charAfter == ';'
376 || Character.isWhitespace(charAfter);
377 }
378
379 }