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.blocks;
21
22 import java.util.Arrays;
23 import java.util.Locale;
24 import java.util.Optional;
25
26 import com.puppycrawl.tools.checkstyle.StatelessCheck;
27 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
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 @StatelessCheck
93 public class RightCurlyCheck extends AbstractCheck {
94
95
96
97
98
99 public static final String MSG_KEY_LINE_BREAK_BEFORE = "line.break.before";
100
101
102
103
104
105 public static final String MSG_KEY_LINE_ALONE = "line.alone";
106
107
108
109
110
111 public static final String MSG_KEY_LINE_SAME = "line.same";
112
113
114
115
116 private RightCurlyOption option = RightCurlyOption.SAME;
117
118
119
120
121
122
123
124
125 public void setOption(String optionStr) {
126 option = RightCurlyOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
127 }
128
129 @Override
130 public int[] getDefaultTokens() {
131 return new int[] {
132 TokenTypes.LITERAL_TRY,
133 TokenTypes.LITERAL_CATCH,
134 TokenTypes.LITERAL_FINALLY,
135 TokenTypes.LITERAL_IF,
136 TokenTypes.LITERAL_ELSE,
137 };
138 }
139
140 @Override
141 public int[] getAcceptableTokens() {
142 return new int[] {
143 TokenTypes.LITERAL_TRY,
144 TokenTypes.LITERAL_CATCH,
145 TokenTypes.LITERAL_FINALLY,
146 TokenTypes.LITERAL_IF,
147 TokenTypes.LITERAL_ELSE,
148 TokenTypes.CLASS_DEF,
149 TokenTypes.METHOD_DEF,
150 TokenTypes.CTOR_DEF,
151 TokenTypes.LITERAL_FOR,
152 TokenTypes.LITERAL_WHILE,
153 TokenTypes.LITERAL_DO,
154 TokenTypes.STATIC_INIT,
155 TokenTypes.INSTANCE_INIT,
156 TokenTypes.ANNOTATION_DEF,
157 TokenTypes.ENUM_DEF,
158 TokenTypes.INTERFACE_DEF,
159 TokenTypes.RECORD_DEF,
160 TokenTypes.COMPACT_CTOR_DEF,
161 TokenTypes.LITERAL_SWITCH,
162 TokenTypes.LITERAL_CASE,
163 };
164 }
165
166 @Override
167 public int[] getRequiredTokens() {
168 return CommonUtil.EMPTY_INT_ARRAY;
169 }
170
171 @Override
172 public void visitToken(DetailAST ast) {
173 final Details details = Details.getDetails(ast);
174 final DetailAST rcurly = details.rcurly;
175
176 if (rcurly != null) {
177 final String violation = validate(details);
178 if (!violation.isEmpty()) {
179 log(rcurly, violation, "}", rcurly.getColumnNo() + 1);
180 }
181 }
182 }
183
184
185
186
187
188
189
190
191 private String validate(Details details) {
192 String violation = "";
193 if (shouldHaveLineBreakBefore(option, details)) {
194 violation = MSG_KEY_LINE_BREAK_BEFORE;
195 }
196 else if (shouldBeOnSameLine(option, details)) {
197 violation = MSG_KEY_LINE_SAME;
198 }
199 else if (shouldBeAloneOnLine(option, details, getLine(details.rcurly.getLineNo() - 1))) {
200 violation = MSG_KEY_LINE_ALONE;
201 }
202 return violation;
203 }
204
205
206
207
208
209
210
211
212 private static boolean shouldHaveLineBreakBefore(RightCurlyOption bracePolicy,
213 Details details) {
214 return bracePolicy == RightCurlyOption.SAME
215 && !hasLineBreakBefore(details.rcurly)
216 && !TokenUtil.areOnSameLine(details.lcurly, details.rcurly);
217 }
218
219
220
221
222
223
224
225
226 private static boolean shouldBeOnSameLine(RightCurlyOption bracePolicy, Details details) {
227 return bracePolicy == RightCurlyOption.SAME
228 && !details.shouldCheckLastRcurly
229 && !TokenUtil.areOnSameLine(details.rcurly, details.nextToken);
230 }
231
232
233
234
235
236
237
238
239
240 private static boolean shouldBeAloneOnLine(RightCurlyOption bracePolicy,
241 Details details,
242 String targetSrcLine) {
243 return bracePolicy == RightCurlyOption.ALONE
244 && shouldBeAloneOnLineWithAloneOption(details, targetSrcLine)
245 || (bracePolicy == RightCurlyOption.ALONE_OR_SINGLELINE
246 || details.shouldCheckLastRcurly)
247 && shouldBeAloneOnLineWithNotAloneOption(details, targetSrcLine);
248 }
249
250
251
252
253
254
255
256
257 private static boolean shouldBeAloneOnLineWithAloneOption(Details details,
258 String targetSrcLine) {
259 return !isAloneOnLine(details, targetSrcLine);
260 }
261
262
263
264
265
266
267
268
269
270 private static boolean shouldBeAloneOnLineWithNotAloneOption(Details details,
271 String targetSrcLine) {
272 return shouldBeAloneOnLineWithAloneOption(details, targetSrcLine)
273 && !isBlockAloneOnSingleLine(details);
274 }
275
276
277
278
279
280
281
282
283 private static boolean isAloneOnLine(Details details, String targetSrcLine) {
284 final DetailAST rcurly = details.rcurly;
285 final DetailAST nextToken = details.nextToken;
286 return (nextToken == null || !TokenUtil.areOnSameLine(rcurly, nextToken)
287 || skipDoubleBraceInstInit(details))
288 && CommonUtil.hasWhitespaceBefore(details.rcurly.getColumnNo(),
289 targetSrcLine);
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 private static boolean skipDoubleBraceInstInit(Details details) {
311 boolean skipDoubleBraceInstInit = false;
312 final DetailAST tokenAfterNextToken = Details.getNextToken(details.nextToken);
313 if (tokenAfterNextToken != null) {
314 final DetailAST rcurly = details.rcurly;
315 skipDoubleBraceInstInit = rcurly.getParent().getParent()
316 .getType() == TokenTypes.INSTANCE_INIT
317 && details.nextToken.getType() == TokenTypes.RCURLY
318 && !TokenUtil.areOnSameLine(rcurly, Details.getNextToken(tokenAfterNextToken));
319 }
320 return skipDoubleBraceInstInit;
321 }
322
323
324
325
326
327
328
329 private static boolean isBlockAloneOnSingleLine(Details details) {
330 DetailAST nextToken = details.nextToken;
331
332 while (nextToken != null && nextToken.getType() == TokenTypes.LITERAL_ELSE) {
333 nextToken = Details.getNextToken(nextToken);
334 }
335
336
337 final int[] tokensWithBlockSibling = {
338 TokenTypes.DO_WHILE,
339 TokenTypes.LITERAL_FINALLY,
340 TokenTypes.LITERAL_CATCH,
341 };
342
343 if (TokenUtil.isOfType(nextToken, tokensWithBlockSibling)) {
344 final DetailAST parent = nextToken.getParent();
345 nextToken = Details.getNextToken(parent);
346 }
347
348 return TokenUtil.areOnSameLine(details.lcurly, details.rcurly)
349 && (nextToken == null || !TokenUtil.areOnSameLine(details.rcurly, nextToken)
350 || isRightcurlyFollowedBySemicolon(details));
351 }
352
353
354
355
356
357
358
359 private static boolean isRightcurlyFollowedBySemicolon(Details details) {
360 return details.nextToken.getType() == TokenTypes.SEMI;
361 }
362
363
364
365
366
367
368
369 private static boolean hasLineBreakBefore(DetailAST rightCurly) {
370 DetailAST previousToken = rightCurly.getPreviousSibling();
371 if (previousToken == null) {
372 previousToken = rightCurly.getParent();
373 }
374 return !TokenUtil.areOnSameLine(rightCurly, previousToken);
375 }
376
377
378
379
380 private static final class Details {
381
382
383
384
385 private static final int[] TOKENS_WITH_NO_CHILD_SLIST = {
386 TokenTypes.CLASS_DEF,
387 TokenTypes.ENUM_DEF,
388 TokenTypes.ANNOTATION_DEF,
389 TokenTypes.INTERFACE_DEF,
390 TokenTypes.RECORD_DEF,
391 };
392
393
394 private final DetailAST rcurly;
395
396 private final DetailAST lcurly;
397
398 private final DetailAST nextToken;
399
400 private final boolean shouldCheckLastRcurly;
401
402
403
404
405
406
407
408
409
410 private Details(DetailAST lcurly, DetailAST rcurly,
411 DetailAST nextToken, boolean shouldCheckLastRcurly) {
412 this.lcurly = lcurly;
413 this.rcurly = rcurly;
414 this.nextToken = nextToken;
415 this.shouldCheckLastRcurly = shouldCheckLastRcurly;
416 }
417
418
419
420
421
422
423
424 private static Details getDetails(DetailAST ast) {
425 return switch (ast.getType()) {
426 case TokenTypes.LITERAL_TRY, TokenTypes.LITERAL_CATCH -> getDetailsForTryCatch(ast);
427 case TokenTypes.LITERAL_IF -> getDetailsForIf(ast);
428 case TokenTypes.LITERAL_DO -> getDetailsForDoLoops(ast);
429 case TokenTypes.LITERAL_SWITCH -> getDetailsForSwitch(ast);
430 case TokenTypes.LITERAL_CASE -> getDetailsForCase(ast);
431 default -> getDetailsForOthers(ast);
432 };
433 }
434
435
436
437
438
439
440
441 private static Details getDetailsForSwitch(DetailAST switchNode) {
442 final DetailAST lcurly = switchNode.findFirstToken(TokenTypes.LCURLY);
443 final DetailAST rcurly;
444 DetailAST nextToken = null;
445
446 if (isSwitchExpression(switchNode)) {
447 rcurly = null;
448 }
449 else {
450 rcurly = switchNode.getLastChild();
451 nextToken = getNextToken(switchNode);
452 }
453 return new Details(lcurly, rcurly, nextToken, true);
454 }
455
456
457
458
459
460
461
462 private static Details getDetailsForCase(DetailAST caseNode) {
463 final DetailAST caseParent = caseNode.getParent();
464 final int parentType = caseParent.getType();
465 final Optional<DetailAST> lcurly;
466 final DetailAST statementList;
467
468 if (parentType == TokenTypes.SWITCH_RULE) {
469 statementList = caseParent.findFirstToken(TokenTypes.SLIST);
470 lcurly = Optional.ofNullable(statementList);
471 }
472 else {
473 statementList = caseNode.getNextSibling();
474 lcurly = Optional.ofNullable(statementList)
475 .map(DetailAST::getFirstChild)
476 .filter(node -> node.getType() == TokenTypes.SLIST);
477 }
478 final DetailAST rcurly = lcurly.map(DetailAST::getLastChild)
479 .filter(child -> !isSwitchExpression(caseParent))
480 .orElse(null);
481 final Optional<DetailAST> nextToken =
482 Optional.ofNullable(lcurly.map(DetailAST::getNextSibling)
483 .orElseGet(() -> getNextToken(caseParent)));
484
485 return new Details(lcurly.orElse(null), rcurly, nextToken.orElse(null), true);
486 }
487
488
489
490
491
492
493
494 private static boolean isSwitchExpression(DetailAST switchNode) {
495 DetailAST currentNode = switchNode;
496 boolean ans = false;
497
498 while (currentNode != null) {
499 if (currentNode.getType() == TokenTypes.EXPR) {
500 ans = true;
501 }
502 currentNode = currentNode.getParent();
503 }
504 return ans;
505 }
506
507
508
509
510
511
512
513 private static Details getDetailsForTryCatch(DetailAST ast) {
514 final DetailAST lcurly;
515 DetailAST nextToken;
516 final int tokenType = ast.getType();
517 if (tokenType == TokenTypes.LITERAL_TRY) {
518 if (ast.getFirstChild().getType() == TokenTypes.RESOURCE_SPECIFICATION) {
519 lcurly = ast.getFirstChild().getNextSibling();
520 }
521 else {
522 lcurly = ast.getFirstChild();
523 }
524 nextToken = lcurly.getNextSibling();
525 }
526 else {
527 nextToken = ast.getNextSibling();
528 lcurly = ast.getLastChild();
529 }
530
531 final boolean shouldCheckLastRcurly;
532 if (nextToken == null) {
533 shouldCheckLastRcurly = true;
534 nextToken = getNextToken(ast);
535 }
536 else {
537 shouldCheckLastRcurly = false;
538 }
539
540 final DetailAST rcurly = lcurly.getLastChild();
541 return new Details(lcurly, rcurly, nextToken, shouldCheckLastRcurly);
542 }
543
544
545
546
547
548
549
550 private static Details getDetailsForIf(DetailAST ast) {
551 final boolean shouldCheckLastRcurly;
552 final DetailAST lcurly;
553 DetailAST nextToken = ast.findFirstToken(TokenTypes.LITERAL_ELSE);
554
555 if (nextToken == null) {
556 shouldCheckLastRcurly = true;
557 nextToken = getNextToken(ast);
558 lcurly = ast.getLastChild();
559 }
560 else {
561 shouldCheckLastRcurly = false;
562 lcurly = nextToken.getPreviousSibling();
563 }
564
565 DetailAST rcurly = null;
566 if (lcurly.getType() == TokenTypes.SLIST) {
567 rcurly = lcurly.getLastChild();
568 }
569 return new Details(lcurly, rcurly, nextToken, shouldCheckLastRcurly);
570 }
571
572
573
574
575
576
577
578
579 private static Details getDetailsForOthers(DetailAST ast) {
580 DetailAST rcurly = null;
581 final DetailAST lcurly;
582 final int tokenType = ast.getType();
583 if (isTokenWithNoChildSlist(tokenType)) {
584 final DetailAST child = ast.getLastChild();
585 lcurly = child;
586 rcurly = child.getLastChild();
587 }
588 else {
589 lcurly = ast.findFirstToken(TokenTypes.SLIST);
590 if (lcurly != null) {
591
592 rcurly = lcurly.getLastChild();
593 }
594 }
595 return new Details(lcurly, rcurly, getNextToken(ast), true);
596 }
597
598
599
600
601
602
603
604
605 private static boolean isTokenWithNoChildSlist(int tokenType) {
606 return Arrays.stream(TOKENS_WITH_NO_CHILD_SLIST).anyMatch(token -> token == tokenType);
607 }
608
609
610
611
612
613
614
615 private static Details getDetailsForDoLoops(DetailAST ast) {
616 final DetailAST lcurly = ast.findFirstToken(TokenTypes.SLIST);
617 final DetailAST nextToken = ast.findFirstToken(TokenTypes.DO_WHILE);
618 DetailAST rcurly = null;
619 if (lcurly != null) {
620 rcurly = lcurly.getLastChild();
621 }
622 return new Details(lcurly, rcurly, nextToken, false);
623 }
624
625
626
627
628
629
630
631 private static DetailAST getNextToken(DetailAST ast) {
632 DetailAST next = null;
633 DetailAST parent = ast;
634 while (next == null && parent != null) {
635 next = parent.getNextSibling();
636 parent = parent.getParent();
637 }
638 return next;
639 }
640 }
641 }