001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.whitespace; 021 022import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 026 027/** 028 * Checks that a token is surrounded by whitespace. 029 * 030 * <p>By default the check will check the following operators: 031 * {@link TokenTypes#LITERAL_ASSERT ASSERT}, 032 * {@link TokenTypes#ASSIGN ASSIGN}, 033 * {@link TokenTypes#BAND BAND}, 034 * {@link TokenTypes#BAND_ASSIGN BAND_ASSIGN}, 035 * {@link TokenTypes#BOR BOR}, 036 * {@link TokenTypes#BOR_ASSIGN BOR_ASSIGN}, 037 * {@link TokenTypes#BSR BSR}, 038 * {@link TokenTypes#BSR_ASSIGN BSR_ASSIGN}, 039 * {@link TokenTypes#BXOR BXOR}, 040 * {@link TokenTypes#BXOR_ASSIGN BXOR_ASSIGN}, 041 * {@link TokenTypes#COLON COLON}, 042 * {@link TokenTypes#DIV DIV}, 043 * {@link TokenTypes#DIV_ASSIGN DIV_ASSIGN}, 044 * {@link TokenTypes#DO_WHILE DO_WHILE}, 045 * {@link TokenTypes#EQUAL EQUAL}, 046 * {@link TokenTypes#GE GE}, 047 * {@link TokenTypes#GT GT}, 048 * {@link TokenTypes#LAND LAND}, 049 * {@link TokenTypes#LCURLY LCURLY}, 050 * {@link TokenTypes#LE LE}, 051 * {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH}, 052 * {@link TokenTypes#LITERAL_DO LITERAL_DO}, 053 * {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE}, 054 * {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY}, 055 * {@link TokenTypes#LITERAL_FOR LITERAL_FOR}, 056 * {@link TokenTypes#LITERAL_IF LITERAL_IF}, 057 * {@link TokenTypes#LITERAL_RETURN LITERAL_RETURN}, 058 * {@link TokenTypes#LITERAL_SWITCH LITERAL_SWITCH}, 059 * {@link TokenTypes#LITERAL_SYNCHRONIZED LITERAL_SYNCHRONIZED}, 060 * {@link TokenTypes#LITERAL_TRY LITERAL_TRY}, 061 * {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE}, 062 * {@link TokenTypes#LOR LOR}, 063 * {@link TokenTypes#LT LT}, 064 * {@link TokenTypes#MINUS MINUS}, 065 * {@link TokenTypes#MINUS_ASSIGN MINUS_ASSIGN}, 066 * {@link TokenTypes#MOD MOD}, 067 * {@link TokenTypes#MOD_ASSIGN MOD_ASSIGN}, 068 * {@link TokenTypes#NOT_EQUAL NOT_EQUAL}, 069 * {@link TokenTypes#PLUS PLUS}, 070 * {@link TokenTypes#PLUS_ASSIGN PLUS_ASSIGN}, 071 * {@link TokenTypes#QUESTION QUESTION}, 072 * {@link TokenTypes#RCURLY RCURLY}, 073 * {@link TokenTypes#SL SL}, 074 * {@link TokenTypes#SLIST SLIST}, 075 * {@link TokenTypes#SL_ASSIGN SL_ASSIGN}, 076 * {@link TokenTypes#SR SR}, 077 * {@link TokenTypes#SR_ASSIGN SR_ASSIGN}, 078 * {@link TokenTypes#STAR STAR}, 079 * {@link TokenTypes#STAR_ASSIGN STAR_ASSIGN}, 080 * {@link TokenTypes#LITERAL_ASSERT LITERAL_ASSERT}, 081 * {@link TokenTypes#TYPE_EXTENSION_AND TYPE_EXTENSION_AND}. 082 * 083 * <p>An example of how to configure the check is: 084 * 085 * <pre> 086 * <module name="WhitespaceAround"/> 087 * </pre> 088 * 089 * <p>An example of how to configure the check for whitespace only around 090 * assignment operators is: 091 * 092 * <pre> 093 * <module name="WhitespaceAround"> 094 * <property name="tokens" 095 * value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN, 096 * MOD_ASSIGN,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BXOR_ASSIGN, 097 * BOR_ASSIGN,BAND_ASSIGN"/> 098 * </module> 099 * </pre> 100 * 101 * <p>An example of how to configure the check for whitespace only around 102 * curly braces is: 103 * <pre> 104 * <module name="WhitespaceAround"> 105 * <property name="tokens" 106 * value="LCURLY,RCURLY"/> 107 * </module> 108 * </pre> 109 * 110 * <p>In addition, this check can be configured to allow empty methods, types, 111 * for, while, do-while loops, lambdas and constructor bodies. 112 * For example: 113 * 114 * <pre>{@code 115 * public MyClass() {} // empty constructor 116 * public void func() {} // empty method 117 * public interface Foo {} // empty interface 118 * public class Foo {} // empty class 119 * public enum Foo {} // empty enum 120 * MyClass c = new MyClass() {}; // empty anonymous class 121 * while (i = 1) {} // empty while loop 122 * for (int i = 1; i > 1; i++) {} // empty for loop 123 * do {} while (i = 1); // empty do-while loop 124 * Runnable noop = () -> {}; // empty lambda 125 * public @interface Beta {} // empty annotation type 126 * }</pre> 127 * 128 * <p>This check does not flag as violation double brace initialization like:</p> 129 * <pre> 130 * new Properties() {{ 131 * setProperty("key", "value"); 132 * }}; 133 * </pre> 134 * 135 * <p>To configure the check to allow empty method blocks use 136 * 137 * <pre> <property name="allowEmptyMethods" value="true" /></pre> 138 * 139 * <p>To configure the check to allow empty constructor blocks use 140 * 141 * <pre> <property name="allowEmptyConstructors" value="true" /></pre> 142 * 143 * <p>To configure the check to allow empty type blocks use 144 * 145 * <pre> <property name="allowEmptyTypes" value="true" /></pre> 146 * 147 * <p>To configure the check to allow empty loop blocks use 148 * 149 * <pre> <property name="allowEmptyLoops" value="true" /></pre> 150 * 151 * <p>To configure the check to allow empty lambdas blocks use 152 * 153 * <pre> <property name="allowEmptyLambdas" value="true" /></pre> 154 * 155 * <p>Also, this check can be configured to ignore the colon in an enhanced for 156 * loop. The colon in an enhanced for loop is ignored by default 157 * 158 * <p>To configure the check to ignore the colon 159 * 160 * <pre> <property name="ignoreEnhancedForColon" value="true" /></pre> 161 * 162 * @author Oliver Burn 163 * @author maxvetrenko 164 * @author Andrei Selkin 165 */ 166public class WhitespaceAroundCheck extends AbstractCheck { 167 168 /** 169 * A key is pointing to the warning message text in "messages.properties" 170 * file. 171 */ 172 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; 173 174 /** 175 * A key is pointing to the warning message text in "messages.properties" 176 * file. 177 */ 178 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 179 180 /** Whether or not empty constructor bodies are allowed. */ 181 private boolean allowEmptyConstructors; 182 /** Whether or not empty method bodies are allowed. */ 183 private boolean allowEmptyMethods; 184 /** Whether or not empty classes, enums and interfaces are allowed. */ 185 private boolean allowEmptyTypes; 186 /** Whether or not empty loops are allowed. */ 187 private boolean allowEmptyLoops; 188 /** Whether or not empty lambda blocks are allowed. */ 189 private boolean allowEmptyLambdas; 190 /** Whether or not to ignore a colon in a enhanced for loop. */ 191 private boolean ignoreEnhancedForColon = true; 192 193 @Override 194 public int[] getDefaultTokens() { 195 return new int[] { 196 TokenTypes.ASSIGN, 197 TokenTypes.BAND, 198 TokenTypes.BAND_ASSIGN, 199 TokenTypes.BOR, 200 TokenTypes.BOR_ASSIGN, 201 TokenTypes.BSR, 202 TokenTypes.BSR_ASSIGN, 203 TokenTypes.BXOR, 204 TokenTypes.BXOR_ASSIGN, 205 TokenTypes.COLON, 206 TokenTypes.DIV, 207 TokenTypes.DIV_ASSIGN, 208 TokenTypes.DO_WHILE, 209 TokenTypes.EQUAL, 210 TokenTypes.GE, 211 TokenTypes.GT, 212 TokenTypes.LAMBDA, 213 TokenTypes.LAND, 214 TokenTypes.LCURLY, 215 TokenTypes.LE, 216 TokenTypes.LITERAL_CATCH, 217 TokenTypes.LITERAL_DO, 218 TokenTypes.LITERAL_ELSE, 219 TokenTypes.LITERAL_FINALLY, 220 TokenTypes.LITERAL_FOR, 221 TokenTypes.LITERAL_IF, 222 TokenTypes.LITERAL_RETURN, 223 TokenTypes.LITERAL_SWITCH, 224 TokenTypes.LITERAL_SYNCHRONIZED, 225 TokenTypes.LITERAL_TRY, 226 TokenTypes.LITERAL_WHILE, 227 TokenTypes.LOR, 228 TokenTypes.LT, 229 TokenTypes.MINUS, 230 TokenTypes.MINUS_ASSIGN, 231 TokenTypes.MOD, 232 TokenTypes.MOD_ASSIGN, 233 TokenTypes.NOT_EQUAL, 234 TokenTypes.PLUS, 235 TokenTypes.PLUS_ASSIGN, 236 TokenTypes.QUESTION, 237 TokenTypes.RCURLY, 238 TokenTypes.SL, 239 TokenTypes.SLIST, 240 TokenTypes.SL_ASSIGN, 241 TokenTypes.SR, 242 TokenTypes.SR_ASSIGN, 243 TokenTypes.STAR, 244 TokenTypes.STAR_ASSIGN, 245 TokenTypes.LITERAL_ASSERT, 246 TokenTypes.TYPE_EXTENSION_AND, 247 }; 248 } 249 250 @Override 251 public int[] getAcceptableTokens() { 252 return new int[] { 253 TokenTypes.ASSIGN, 254 TokenTypes.BAND, 255 TokenTypes.BAND_ASSIGN, 256 TokenTypes.BOR, 257 TokenTypes.BOR_ASSIGN, 258 TokenTypes.BSR, 259 TokenTypes.BSR_ASSIGN, 260 TokenTypes.BXOR, 261 TokenTypes.BXOR_ASSIGN, 262 TokenTypes.COLON, 263 TokenTypes.DIV, 264 TokenTypes.DIV_ASSIGN, 265 TokenTypes.DO_WHILE, 266 TokenTypes.EQUAL, 267 TokenTypes.GE, 268 TokenTypes.GT, 269 TokenTypes.LAMBDA, 270 TokenTypes.LAND, 271 TokenTypes.LCURLY, 272 TokenTypes.LE, 273 TokenTypes.LITERAL_CATCH, 274 TokenTypes.LITERAL_DO, 275 TokenTypes.LITERAL_ELSE, 276 TokenTypes.LITERAL_FINALLY, 277 TokenTypes.LITERAL_FOR, 278 TokenTypes.LITERAL_IF, 279 TokenTypes.LITERAL_RETURN, 280 TokenTypes.LITERAL_SWITCH, 281 TokenTypes.LITERAL_SYNCHRONIZED, 282 TokenTypes.LITERAL_TRY, 283 TokenTypes.LITERAL_WHILE, 284 TokenTypes.LOR, 285 TokenTypes.LT, 286 TokenTypes.MINUS, 287 TokenTypes.MINUS_ASSIGN, 288 TokenTypes.MOD, 289 TokenTypes.MOD_ASSIGN, 290 TokenTypes.NOT_EQUAL, 291 TokenTypes.PLUS, 292 TokenTypes.PLUS_ASSIGN, 293 TokenTypes.QUESTION, 294 TokenTypes.RCURLY, 295 TokenTypes.SL, 296 TokenTypes.SLIST, 297 TokenTypes.SL_ASSIGN, 298 TokenTypes.SR, 299 TokenTypes.SR_ASSIGN, 300 TokenTypes.STAR, 301 TokenTypes.STAR_ASSIGN, 302 TokenTypes.LITERAL_ASSERT, 303 TokenTypes.TYPE_EXTENSION_AND, 304 TokenTypes.WILDCARD_TYPE, 305 TokenTypes.GENERIC_START, 306 TokenTypes.GENERIC_END, 307 }; 308 } 309 310 @Override 311 public int[] getRequiredTokens() { 312 return CommonUtils.EMPTY_INT_ARRAY; 313 } 314 315 /** 316 * Sets whether or not empty method bodies are allowed. 317 * @param allow {@code true} to allow empty method bodies. 318 */ 319 public void setAllowEmptyMethods(boolean allow) { 320 allowEmptyMethods = allow; 321 } 322 323 /** 324 * Sets whether or not empty constructor bodies are allowed. 325 * @param allow {@code true} to allow empty constructor bodies. 326 */ 327 public void setAllowEmptyConstructors(boolean allow) { 328 allowEmptyConstructors = allow; 329 } 330 331 /** 332 * Sets whether or not to ignore the whitespace around the 333 * colon in an enhanced for loop. 334 * @param ignore {@code true} to ignore enhanced for colon. 335 */ 336 public void setIgnoreEnhancedForColon(boolean ignore) { 337 ignoreEnhancedForColon = ignore; 338 } 339 340 /** 341 * Sets whether or not empty type bodies are allowed. 342 * @param allow {@code true} to allow empty type bodies. 343 */ 344 public void setAllowEmptyTypes(boolean allow) { 345 allowEmptyTypes = allow; 346 } 347 348 /** 349 * Sets whether or not empty loop bodies are allowed. 350 * @param allow {@code true} to allow empty loops bodies. 351 */ 352 public void setAllowEmptyLoops(boolean allow) { 353 allowEmptyLoops = allow; 354 } 355 356 /** 357 * Sets whether or not empty lambdas bodies are allowed. 358 * @param allow {@code true} to allow empty lambda expressions. 359 */ 360 public void setAllowEmptyLambdas(boolean allow) { 361 allowEmptyLambdas = allow; 362 } 363 364 @Override 365 public void visitToken(DetailAST ast) { 366 final int currentType = ast.getType(); 367 if (!isNotRelevantSituation(ast, currentType)) { 368 final String line = getLine(ast.getLineNo() - 1); 369 final int before = ast.getColumnNo() - 1; 370 final int after = ast.getColumnNo() + ast.getText().length(); 371 372 if (before >= 0) { 373 final char prevChar = line.charAt(before); 374 if (shouldCheckSeparationFromPreviousToken(ast) 375 && !Character.isWhitespace(prevChar)) { 376 log(ast.getLineNo(), ast.getColumnNo(), 377 MSG_WS_NOT_PRECEDED, ast.getText()); 378 } 379 } 380 381 if (after < line.length()) { 382 final char nextChar = line.charAt(after); 383 if (shouldCheckSeparationFromNextToken(ast, nextChar) 384 && !Character.isWhitespace(nextChar)) { 385 log(ast.getLineNo(), ast.getColumnNo() + ast.getText().length(), 386 MSG_WS_NOT_FOLLOWED, ast.getText()); 387 } 388 } 389 } 390 } 391 392 /** 393 * Is ast not a target of Check. 394 * @param ast ast 395 * @param currentType type of ast 396 * @return true is ok to skip validation 397 */ 398 private boolean isNotRelevantSituation(DetailAST ast, int currentType) { 399 final int parentType = ast.getParent().getType(); 400 final boolean starImport = currentType == TokenTypes.STAR 401 && parentType == TokenTypes.DOT; 402 final boolean slistInsideCaseGroup = currentType == TokenTypes.SLIST 403 && parentType == TokenTypes.CASE_GROUP; 404 405 final boolean starImportOrSlistInsideCaseGroup = starImport || slistInsideCaseGroup; 406 final boolean colonOfCaseOrDefaultOrForEach = 407 isColonOfCaseOrDefault(currentType, parentType) 408 || isColonOfForEach(currentType, parentType); 409 final boolean emptyBlockOrType = 410 isEmptyBlock(ast, parentType) 411 || allowEmptyTypes && isEmptyType(ast); 412 413 return starImportOrSlistInsideCaseGroup 414 || colonOfCaseOrDefaultOrForEach 415 || emptyBlockOrType 416 || isArrayInitialization(currentType, parentType); 417 } 418 419 /** 420 * Check if it should be checked if previous token is separated from current by 421 * whitespace. 422 * This function is needed to recognise double brace initialization as valid, 423 * unfortunately its not possible to implement this functionality 424 * in isNotRelevantSituation method, because in this method when we return 425 * true(is not relevant) ast is later doesnt check at all. For example: 426 * new Properties() {{setProperty("double curly braces", "are not a style error"); 427 * }}; 428 * For second left curly brace in first line when we would return true from 429 * isNotRelevantSituation it wouldn't later check that the next token(setProperty) 430 * is not separated from previous token. 431 * @param ast current AST. 432 * @return true if it should be checked if previous token is separated by whitespace, 433 * false otherwise. 434 */ 435 private static boolean shouldCheckSeparationFromPreviousToken(DetailAST ast) { 436 return !isPartOfDoubleBraceInitializerForPreviousToken(ast); 437 } 438 439 /** 440 * Check if it should be checked if next token is separated from current by 441 * whitespace. Explanation why this method is needed is identical to one 442 * included in shouldCheckSeparationFromPreviousToken method. 443 * @param ast current AST. 444 * @param nextChar next character. 445 * @return true if it should be checked if next token is separated by whitespace, 446 * false otherwise. 447 */ 448 private static boolean shouldCheckSeparationFromNextToken(DetailAST ast, char nextChar) { 449 return !(ast.getType() == TokenTypes.LITERAL_RETURN 450 && ast.getFirstChild().getType() == TokenTypes.SEMI) 451 && !isAnonymousInnerClassEnd(ast.getType(), nextChar) 452 && !isPartOfDoubleBraceInitializerForNextToken(ast); 453 } 454 455 /** 456 * Check for "})" or "};" or "},". Happens with anon-inners 457 * @param currentType token 458 * @param nextChar next symbol 459 * @return true is that is end of anon inner class 460 */ 461 private static boolean isAnonymousInnerClassEnd(int currentType, char nextChar) { 462 return currentType == TokenTypes.RCURLY 463 && (nextChar == ')' 464 || nextChar == ';' 465 || nextChar == ',' 466 || nextChar == '.'); 467 } 468 469 /** 470 * Is empty block. 471 * @param ast ast 472 * @param parentType parent 473 * @return true is block is empty 474 */ 475 private boolean isEmptyBlock(DetailAST ast, int parentType) { 476 return isEmptyMethodBlock(ast, parentType) 477 || isEmptyCtorBlock(ast, parentType) 478 || isEmptyLoop(ast, parentType) 479 || isEmptyLambda(ast, parentType); 480 } 481 482 /** 483 * Tests if a given {@code DetailAST} is part of an empty block. 484 * An example empty block might look like the following 485 * <p> 486 * <pre> public void myMethod(int val) {}</pre> 487 * </p> 488 * In the above, the method body is an empty block ("{}"). 489 * 490 * @param ast the {@code DetailAST} to test. 491 * @param parentType the token type of {@code ast}'s parent. 492 * @param match the parent token type we're looking to match. 493 * @return {@code true} if {@code ast} makes up part of an 494 * empty block contained under a {@code match} token type 495 * node. 496 */ 497 private static boolean isEmptyBlock(DetailAST ast, int parentType, int match) { 498 final int type = ast.getType(); 499 if (type == TokenTypes.RCURLY) { 500 final DetailAST parent = ast.getParent(); 501 final DetailAST grandParent = ast.getParent().getParent(); 502 return parentType == TokenTypes.SLIST 503 && parent.getFirstChild().getType() == TokenTypes.RCURLY 504 && grandParent.getType() == match; 505 } 506 507 return type == TokenTypes.SLIST 508 && parentType == match 509 && ast.getFirstChild().getType() == TokenTypes.RCURLY; 510 } 511 512 /** 513 * Whether colon belongs to cases or defaults. 514 * @param currentType current 515 * @param parentType parent 516 * @return true if current token in colon of case or default tokens 517 */ 518 private static boolean isColonOfCaseOrDefault(int currentType, int parentType) { 519 return currentType == TokenTypes.COLON 520 && (parentType == TokenTypes.LITERAL_DEFAULT 521 || parentType == TokenTypes.LITERAL_CASE); 522 } 523 524 /** 525 * Whether colon belongs to for-each. 526 * @param currentType current 527 * @param parentType parent 528 * @return true if current token in colon of for-each token 529 */ 530 private boolean isColonOfForEach(int currentType, int parentType) { 531 return currentType == TokenTypes.COLON 532 && parentType == TokenTypes.FOR_EACH_CLAUSE 533 && ignoreEnhancedForColon; 534 } 535 536 /** 537 * Is array initialization. 538 * @param currentType current token 539 * @param parentType parent token 540 * @return true is current token inside array initialization 541 */ 542 private static boolean isArrayInitialization(int currentType, int parentType) { 543 return (currentType == TokenTypes.RCURLY || currentType == TokenTypes.LCURLY) 544 && (parentType == TokenTypes.ARRAY_INIT 545 || parentType == TokenTypes.ANNOTATION_ARRAY_INIT); 546 } 547 548 /** 549 * Test if the given {@code DetailAST} is part of an allowed empty 550 * method block. 551 * @param ast the {@code DetailAST} to test. 552 * @param parentType the token type of {@code ast}'s parent. 553 * @return {@code true} if {@code ast} makes up part of an 554 * allowed empty method block. 555 */ 556 private boolean isEmptyMethodBlock(DetailAST ast, int parentType) { 557 return allowEmptyMethods 558 && isEmptyBlock(ast, parentType, TokenTypes.METHOD_DEF); 559 } 560 561 /** 562 * Test if the given {@code DetailAST} is part of an allowed empty 563 * constructor (ctor) block. 564 * @param ast the {@code DetailAST} to test. 565 * @param parentType the token type of {@code ast}'s parent. 566 * @return {@code true} if {@code ast} makes up part of an 567 * allowed empty constructor block. 568 */ 569 private boolean isEmptyCtorBlock(DetailAST ast, int parentType) { 570 return allowEmptyConstructors 571 && isEmptyBlock(ast, parentType, TokenTypes.CTOR_DEF); 572 } 573 574 /** 575 * 576 * @param ast ast the {@code DetailAST} to test. 577 * @param parentType the token type of {@code ast}'s parent. 578 * @return {@code true} if {@code ast} makes up part of an 579 * allowed empty loop block. 580 */ 581 private boolean isEmptyLoop(DetailAST ast, int parentType) { 582 return allowEmptyLoops 583 && (isEmptyBlock(ast, parentType, TokenTypes.LITERAL_FOR) 584 || isEmptyBlock(ast, parentType, TokenTypes.LITERAL_WHILE) 585 || isEmptyBlock(ast, parentType, TokenTypes.LITERAL_DO)); 586 } 587 588 /** 589 * Test if the given {@code DetailAST} is part of an allowed empty 590 * lambda block. 591 * @param ast the {@code DetailAST} to test. 592 * @param parentType the token type of {@code ast}'s parent. 593 * @return {@code true} if {@code ast} makes up part of an 594 * allowed empty lambda block. 595 */ 596 private boolean isEmptyLambda(DetailAST ast, int parentType) { 597 return allowEmptyLambdas && isEmptyBlock(ast, parentType, TokenTypes.LAMBDA); 598 } 599 600 /** 601 * Test if the given {@code DetailAST} is part of an empty block. 602 * An example empty block might look like the following 603 * <p> 604 * <pre> class Foo {}</pre> 605 * </p> 606 * 607 * @param ast ast the {@code DetailAST} to test. 608 * @return {@code true} if {@code ast} makes up part of an 609 * empty block contained under a {@code match} token type 610 * node. 611 */ 612 private static boolean isEmptyType(DetailAST ast) { 613 final int type = ast.getType(); 614 final DetailAST nextSibling = ast.getNextSibling(); 615 final DetailAST previousSibling = ast.getPreviousSibling(); 616 return type == TokenTypes.LCURLY 617 && nextSibling.getType() == TokenTypes.RCURLY 618 || type == TokenTypes.RCURLY 619 && previousSibling != null 620 && previousSibling.getType() == TokenTypes.LCURLY; 621 } 622 623 /** 624 * Check if given ast is part of double brace initializer and if it 625 * should omit checking if previous token is separated by whitespace. 626 * @param ast ast to check 627 * @return true if it should omit checking for previous token, false otherwise 628 */ 629 private static boolean isPartOfDoubleBraceInitializerForPreviousToken(DetailAST ast) { 630 final boolean initializerBeginsAfterClassBegins = ast.getType() == TokenTypes.SLIST 631 && ast.getParent().getType() == TokenTypes.INSTANCE_INIT; 632 final boolean classEndsAfterInitializerEnds = ast.getType() == TokenTypes.RCURLY 633 && ast.getPreviousSibling() != null 634 && ast.getPreviousSibling().getType() == TokenTypes.INSTANCE_INIT; 635 return initializerBeginsAfterClassBegins || classEndsAfterInitializerEnds; 636 } 637 638 /** 639 * Check if given ast is part of double brace initializer and if it 640 * should omit checking if next token is separated by whitespace. 641 * See <a href="https://github.com/checkstyle/checkstyle/pull/2845"> 642 * PR#2845</a> for more information why this function was needed. 643 * @param ast ast to check 644 * @return true if it should omit checking for next token, false otherwise 645 */ 646 private static boolean isPartOfDoubleBraceInitializerForNextToken(DetailAST ast) { 647 final boolean classBeginBeforeInitializerBegin = ast.getType() == TokenTypes.LCURLY 648 && ast.getNextSibling().getType() == TokenTypes.INSTANCE_INIT; 649 final boolean initalizerEndsBeforeClassEnds = ast.getType() == TokenTypes.RCURLY 650 && ast.getParent().getType() == TokenTypes.SLIST 651 && ast.getParent().getParent().getType() == TokenTypes.INSTANCE_INIT 652 && ast.getParent().getParent().getNextSibling().getType() == TokenTypes.RCURLY; 653 return classBeginBeforeInitializerBegin || initalizerEndsBeforeClassEnds; 654 } 655}