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