forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NoWhitespaceAfterCheck.java
591 lines (561 loc) · 21.3 KB
/
NoWhitespaceAfterCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.whitespace;
import java.util.Optional;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
/**
* <p>
* Checks that there is no whitespace after a token.
* More specifically, it checks that it is not followed by whitespace,
* or (if linebreaks are allowed) all characters on the line after are
* whitespace. To forbid linebreaks after a token, set property
* {@code allowLineBreaks} to {@code false}.
* </p>
* <p>
* The check processes
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ARRAY_DECLARATOR">
* ARRAY_DECLARATOR</a> and
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INDEX_OP">
* INDEX_OP</a> tokens specially from other tokens. Actually it is checked that
* there is no whitespace before these tokens, not after them. Space after the
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATIONS">
* ANNOTATIONS</a> before
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ARRAY_DECLARATOR">
* ARRAY_DECLARATOR</a> and
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INDEX_OP">
* INDEX_OP</a> will be ignored.
* </p>
* <p>
* If the annotation is between the type and the array, the check will skip validation for spaces
* </p>
* <pre>
* public void foo(final char @NotNull [] param) {} // No violation
* </pre>
* <ul>
* <li>
* Property {@code allowLineBreaks} - Control whether whitespace is allowed
* if the token is at a linebreak.
* Type is {@code boolean}.
* Default value is {@code true}.
* </li>
* <li>
* Property {@code tokens} - tokens to check
* Type is {@code java.lang.String[]}.
* Validation type is {@code tokenSet}.
* Default value is:
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ARRAY_INIT">
* ARRAY_INIT</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#AT">
* AT</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INC">
* INC</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DEC">
* DEC</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#UNARY_MINUS">
* UNARY_MINUS</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#UNARY_PLUS">
* UNARY_PLUS</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BNOT">
* BNOT</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LNOT">
* LNOT</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DOT">
* DOT</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ARRAY_DECLARATOR">
* ARRAY_DECLARATOR</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INDEX_OP">
* INDEX_OP</a>.
* </li>
* </ul>
* <p>
* To configure the check:
* </p>
* <pre>
* <module name="NoWhitespaceAfter"/>
* </pre>
* <p>Example:</p>
* <pre>
* class Test {
*
* public void lineBreak(String x) {
* Integer.
* parseInt(x); // Ok
* Integer.parseInt(x); // Ok
* }
*
* public void dotOperator(String s) {
* Integer.parseInt(s); // Ok
* Integer. parseInt(s); // violation, '.' is followed by whitespace
* }
*
* public void arrayDec() {
* int[] arr = arr; // Ok
* int [] arr = arr; // violation, int is followed by whitespace
* }
*
* public void bitwiseNot(int a) {
* a = ~ a; // violation '~' is followed by whitespace
* a = ~a; // Ok
* }
* }
* </pre>
* <p>To configure the check to forbid linebreaks after a DOT token:
* </p>
* <pre>
* <module name="NoWhitespaceAfter">
* <property name="tokens" value="DOT"/>
* <property name="allowLineBreaks" value="false"/>
* </module>
* </pre>
* <p>Example:</p>
* <pre>
* class Test {
*
* public void lineBreak(String x) {
* Integer.
* parseInt(x); // violation, '.' is followed by whitespace
* Integer.parseInt(x); // Ok
* }
*
* public void dotOperator(String s) {
* Integer.parseInt(s); // Ok
* Integer. parseInt(s); // violation, '.' is followed by whitespace
* }
*
* public void arrayDec() {
* int[] arr = arr; // Ok
* int [] arr = arr; // Ok
* }
*
* public void bitwiseNot(int a) {
* a = ~ a; // Ok
* a = ~a; // Ok
* }
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
* Violation Message Keys:
* </p>
* <ul>
* <li>
* {@code ws.followed}
* </li>
* </ul>
*
* @since 3.0
*/
@StatelessCheck
public class NoWhitespaceAfterCheck extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "ws.followed";
/** Control whether whitespace is allowed if the token is at a linebreak. */
private boolean allowLineBreaks = true;
@Override
public int[] getDefaultTokens() {
return new int[] {
TokenTypes.ARRAY_INIT,
TokenTypes.AT,
TokenTypes.INC,
TokenTypes.DEC,
TokenTypes.UNARY_MINUS,
TokenTypes.UNARY_PLUS,
TokenTypes.BNOT,
TokenTypes.LNOT,
TokenTypes.DOT,
TokenTypes.ARRAY_DECLARATOR,
TokenTypes.INDEX_OP,
};
}
@Override
public int[] getAcceptableTokens() {
return new int[] {
TokenTypes.ARRAY_INIT,
TokenTypes.AT,
TokenTypes.INC,
TokenTypes.DEC,
TokenTypes.UNARY_MINUS,
TokenTypes.UNARY_PLUS,
TokenTypes.BNOT,
TokenTypes.LNOT,
TokenTypes.DOT,
TokenTypes.TYPECAST,
TokenTypes.ARRAY_DECLARATOR,
TokenTypes.INDEX_OP,
TokenTypes.LITERAL_SYNCHRONIZED,
TokenTypes.METHOD_REF,
};
}
@Override
public int[] getRequiredTokens() {
return CommonUtil.EMPTY_INT_ARRAY;
}
/**
* Setter to control whether whitespace is allowed if the token is at a linebreak.
*
* @param allowLineBreaks whether whitespace should be
* flagged at linebreaks.
*/
public void setAllowLineBreaks(boolean allowLineBreaks) {
this.allowLineBreaks = allowLineBreaks;
}
@Override
public void visitToken(DetailAST ast) {
if (shouldCheckWhitespaceAfter(ast)) {
final DetailAST whitespaceFollowedAst = getWhitespaceFollowedNode(ast);
final int whitespaceColumnNo = getPositionAfter(whitespaceFollowedAst);
final int whitespaceLineNo = whitespaceFollowedAst.getLineNo();
if (hasTrailingWhitespace(ast, whitespaceColumnNo, whitespaceLineNo)) {
log(ast, MSG_KEY, whitespaceFollowedAst.getText());
}
}
}
/**
* For a visited ast node returns node that should be checked
* for not being followed by whitespace.
*
* @param ast
* , visited node.
* @return node before ast.
*/
private static DetailAST getWhitespaceFollowedNode(DetailAST ast) {
final DetailAST whitespaceFollowedAst;
switch (ast.getType()) {
case TokenTypes.TYPECAST:
whitespaceFollowedAst = ast.findFirstToken(TokenTypes.RPAREN);
break;
case TokenTypes.ARRAY_DECLARATOR:
whitespaceFollowedAst = getArrayDeclaratorPreviousElement(ast);
break;
case TokenTypes.INDEX_OP:
whitespaceFollowedAst = getIndexOpPreviousElement(ast);
break;
default:
whitespaceFollowedAst = ast;
}
return whitespaceFollowedAst;
}
/**
* Returns whether whitespace after a visited node should be checked. For example, whitespace
* is not allowed between a type and an array declarator (returns true), except when there is
* an annotation in between the type and array declarator (returns false).
*
* @param ast the visited node
* @return true if whitespace after ast should be checked
*/
private static boolean shouldCheckWhitespaceAfter(DetailAST ast) {
boolean checkWhitespace = true;
final DetailAST previousSibling = ast.getPreviousSibling();
if (previousSibling != null && previousSibling.getType() == TokenTypes.ANNOTATIONS) {
checkWhitespace = false;
}
return checkWhitespace;
}
/**
* Gets position after token (place of possible redundant whitespace).
*
* @param ast Node representing token.
* @return position after token.
*/
private static int getPositionAfter(DetailAST ast) {
final int after;
// If target of possible redundant whitespace is in method definition.
if (ast.getType() == TokenTypes.IDENT
&& ast.getNextSibling() != null
&& ast.getNextSibling().getType() == TokenTypes.LPAREN) {
final DetailAST methodDef = ast.getParent();
final DetailAST endOfParams = methodDef.findFirstToken(TokenTypes.RPAREN);
after = endOfParams.getColumnNo() + 1;
}
else {
after = ast.getColumnNo() + ast.getText().length();
}
return after;
}
/**
* Checks if there is unwanted whitespace after the visited node.
*
* @param ast
* , visited node.
* @param whitespaceColumnNo
* , column number of a possible whitespace.
* @param whitespaceLineNo
* , line number of a possible whitespace.
* @return true if whitespace found.
*/
private boolean hasTrailingWhitespace(DetailAST ast,
int whitespaceColumnNo, int whitespaceLineNo) {
final boolean result;
final int astLineNo = ast.getLineNo();
final int[] line = getLineCodePoints(astLineNo - 1);
if (astLineNo == whitespaceLineNo && whitespaceColumnNo < line.length) {
result = CommonUtil.isCodePointWhitespace(line, whitespaceColumnNo);
}
else {
result = !allowLineBreaks;
}
return result;
}
/**
* Returns proper argument for getPositionAfter method, it is a token after
* {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR}, in can be {@link TokenTypes#RBRACK
* RBRACK}, {@link TokenTypes#IDENT IDENT} or an array type definition (literal).
*
* @param ast
* , {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR} node.
* @return previous node by text order.
* @throws IllegalStateException if an unexpected token type is encountered.
*/
private static DetailAST getArrayDeclaratorPreviousElement(DetailAST ast) {
final DetailAST previousElement;
if (ast.getPreviousSibling() != null
&& ast.getPreviousSibling().getType() == TokenTypes.ARRAY_DECLARATOR) {
// Covers higher dimension array declarations and initializations
previousElement = getPreviousElementOfMultiDimArray(ast);
}
else {
// first array index, is preceded with identifier or type
final DetailAST parent = ast.getParent();
switch (parent.getType()) {
// generics
case TokenTypes.TYPE_UPPER_BOUNDS:
case TokenTypes.TYPE_LOWER_BOUNDS:
previousElement = ast.getPreviousSibling();
break;
case TokenTypes.LITERAL_NEW:
case TokenTypes.TYPE_ARGUMENT:
case TokenTypes.DOT:
previousElement = getTypeLastNode(ast);
break;
// mundane array declaration, can be either java style or C style
case TokenTypes.TYPE:
previousElement = getPreviousNodeWithParentOfTypeAst(ast, parent);
break;
// java 8 method reference
case TokenTypes.METHOD_REF:
final DetailAST ident = getIdentLastToken(ast);
if (ident == null) {
// i.e. int[]::new
previousElement = ast.getParent().getFirstChild();
}
else {
previousElement = ident;
}
break;
default:
throw new IllegalStateException("unexpected ast syntax " + parent);
}
}
return previousElement;
}
/**
* Gets the previous element of a second or higher dimension of an
* array declaration or initialization.
*
* @param leftBracket the token to get previous element of
* @return the previous element
*/
private static DetailAST getPreviousElementOfMultiDimArray(DetailAST leftBracket) {
final DetailAST previousRightBracket = leftBracket.getPreviousSibling().getLastChild();
DetailAST ident = null;
// This will get us past the type ident, to the actual identifier
DetailAST parent = leftBracket.getParent().getParent();
while (ident == null) {
ident = parent.findFirstToken(TokenTypes.IDENT);
parent = parent.getParent();
}
final DetailAST previousElement;
if (ident.getColumnNo() > previousRightBracket.getColumnNo()
&& ident.getColumnNo() < leftBracket.getColumnNo()) {
// C style and Java style ' int[] arr []' in same construct
previousElement = ident;
}
else {
// 'int[][] arr' or 'int arr[][]'
previousElement = previousRightBracket;
}
return previousElement;
}
/**
* Gets previous node for {@link TokenTypes#INDEX_OP INDEX_OP} token
* for usage in getPositionAfter method, it is a simplified copy of
* getArrayDeclaratorPreviousElement method.
*
* @param ast
* , {@link TokenTypes#INDEX_OP INDEX_OP} node.
* @return previous node by text order.
*/
private static DetailAST getIndexOpPreviousElement(DetailAST ast) {
final DetailAST result;
final DetailAST firstChild = ast.getFirstChild();
if (firstChild.getType() == TokenTypes.INDEX_OP) {
// second or higher array index
result = firstChild.findFirstToken(TokenTypes.RBRACK);
}
else if (firstChild.getType() == TokenTypes.IDENT) {
result = firstChild;
}
else {
final DetailAST ident = getIdentLastToken(ast);
if (ident == null) {
final DetailAST rparen = ast.findFirstToken(TokenTypes.RPAREN);
// construction like new int[]{1}[0]
if (rparen == null) {
final DetailAST lastChild = firstChild.getLastChild();
result = lastChild.findFirstToken(TokenTypes.RCURLY);
}
// construction like ((byte[]) pixels)[0]
else {
result = rparen;
}
}
else {
result = ident;
}
}
return result;
}
/**
* Searches parameter node for a type node.
* Returns it or its last node if it has an extended structure.
*
* @param ast
* , subject node.
* @return type node.
*/
private static DetailAST getTypeLastNode(DetailAST ast) {
final DetailAST typeLastNode;
final DetailAST parent = ast.getParent();
final boolean isPrecededByTypeArgs =
parent.findFirstToken(TokenTypes.TYPE_ARGUMENTS) != null;
final Optional<DetailAST> objectArrayType = Optional.ofNullable(getIdentLastToken(ast));
if (isPrecededByTypeArgs) {
typeLastNode = parent.findFirstToken(TokenTypes.TYPE_ARGUMENTS)
.findFirstToken(TokenTypes.GENERIC_END);
}
else if (objectArrayType.isPresent()) {
typeLastNode = objectArrayType.get();
}
else {
typeLastNode = parent.getFirstChild();
}
return typeLastNode;
}
/**
* Finds previous node by text order for an array declarator,
* which parent type is {@link TokenTypes#TYPE TYPE}.
*
* @param ast
* , array declarator node.
* @param parent
* , its parent node.
* @return previous node by text order.
*/
private static DetailAST getPreviousNodeWithParentOfTypeAst(DetailAST ast, DetailAST parent) {
final DetailAST previousElement;
final DetailAST ident = getIdentLastToken(parent.getParent());
final DetailAST lastTypeNode = getTypeLastNode(ast);
// sometimes there are ident-less sentences
// i.e. "(Object[]) null", but in casual case should be
// checked whether ident or lastTypeNode has preceding position
// determining if it is java style or C style
if (ident == null || ident.getLineNo() > ast.getLineNo()) {
previousElement = lastTypeNode;
}
else if (ident.getLineNo() < ast.getLineNo()) {
previousElement = ident;
}
// ident and lastTypeNode lay on one line
else {
final int instanceOfSize = 13;
// +2 because ast has `[]` after the ident
if (ident.getColumnNo() >= ast.getColumnNo() + 2
// +13 because ident (at most 1 character) is followed by
// ' instanceof ' (12 characters)
|| lastTypeNode.getColumnNo() >= ident.getColumnNo() + instanceOfSize) {
previousElement = lastTypeNode;
}
else {
previousElement = ident;
}
}
return previousElement;
}
/**
* Gets leftmost token of identifier.
*
* @param ast
* , token possibly possessing an identifier.
* @return leftmost token of identifier.
*/
private static DetailAST getIdentLastToken(DetailAST ast) {
final DetailAST result;
final Optional<DetailAST> dot = getPrecedingDot(ast);
// method call case
if (dot.isEmpty() || ast.getFirstChild().getType() == TokenTypes.METHOD_CALL) {
final DetailAST methodCall = ast.findFirstToken(TokenTypes.METHOD_CALL);
if (methodCall == null) {
result = ast.findFirstToken(TokenTypes.IDENT);
}
else {
result = methodCall.findFirstToken(TokenTypes.RPAREN);
}
}
// qualified name case
else {
result = dot.get().getFirstChild().getNextSibling();
}
return result;
}
/**
* Gets the dot preceding a class member array index operation or class
* reference.
*
* @param leftBracket the ast we are checking
* @return dot preceding the left bracket
*/
private static Optional<DetailAST> getPrecedingDot(DetailAST leftBracket) {
final DetailAST referencedMemberDot = leftBracket.findFirstToken(TokenTypes.DOT);
final Optional<DetailAST> result = Optional.ofNullable(referencedMemberDot);
return result.or(() -> getReferencedClassDot(leftBracket));
}
/**
* Gets the dot preceding a class reference.
*
* @param leftBracket the ast we are checking
* @return dot preceding the left bracket
*/
private static Optional<DetailAST> getReferencedClassDot(DetailAST leftBracket) {
final DetailAST parent = leftBracket.getParent();
Optional<DetailAST> classDot = Optional.empty();
if (parent.getType() != TokenTypes.ASSIGN) {
classDot = Optional.ofNullable(parent.findFirstToken(TokenTypes.DOT));
}
return classDot;
}
}