001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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 * &lt;module name="WhitespaceAround"/&gt;
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 * &lt;module name="WhitespaceAround"&gt;
094 *     &lt;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"/&gt;
098 * &lt;/module&gt;
099 * </pre>
100 *
101 * <p>An example of how to configure the check for whitespace only around
102 * curly braces is:
103 * <pre>
104 * &lt;module name="WhitespaceAround"&gt;
105 *     &lt;property name="tokens"
106 *               value="LCURLY,RCURLY"/&gt;
107 * &lt;/module&gt;
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 &gt; 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>   &lt;property name="allowEmptyMethods" value="true" /&gt;</pre>
138 *
139 * <p>To configure the check to allow empty constructor blocks use
140 *
141 * <pre>   &lt;property name="allowEmptyConstructors" value="true" /&gt;</pre>
142 *
143 * <p>To configure the check to allow empty type blocks use
144 *
145 * <pre>   &lt;property name="allowEmptyTypes" value="true" /&gt;</pre>
146 *
147 * <p>To configure the check to allow empty loop blocks use
148 *
149 * <pre>   &lt;property name="allowEmptyLoops" value="true" /&gt;</pre>
150 *
151 * <p>To configure the check to allow empty lambdas blocks use
152 *
153 * <pre>   &lt;property name="allowEmptyLambdas" value="true" /&gt;</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>   &lt;property name="ignoreEnhancedForColon" value="true" /&gt;</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.ARRAY_INIT,
255            TokenTypes.BAND,
256            TokenTypes.BAND_ASSIGN,
257            TokenTypes.BOR,
258            TokenTypes.BOR_ASSIGN,
259            TokenTypes.BSR,
260            TokenTypes.BSR_ASSIGN,
261            TokenTypes.BXOR,
262            TokenTypes.BXOR_ASSIGN,
263            TokenTypes.COLON,
264            TokenTypes.DIV,
265            TokenTypes.DIV_ASSIGN,
266            TokenTypes.DO_WHILE,
267            TokenTypes.EQUAL,
268            TokenTypes.GE,
269            TokenTypes.GT,
270            TokenTypes.LAMBDA,
271            TokenTypes.LAND,
272            TokenTypes.LCURLY,
273            TokenTypes.LE,
274            TokenTypes.LITERAL_CATCH,
275            TokenTypes.LITERAL_DO,
276            TokenTypes.LITERAL_ELSE,
277            TokenTypes.LITERAL_FINALLY,
278            TokenTypes.LITERAL_FOR,
279            TokenTypes.LITERAL_IF,
280            TokenTypes.LITERAL_RETURN,
281            TokenTypes.LITERAL_SWITCH,
282            TokenTypes.LITERAL_SYNCHRONIZED,
283            TokenTypes.LITERAL_TRY,
284            TokenTypes.LITERAL_WHILE,
285            TokenTypes.LOR,
286            TokenTypes.LT,
287            TokenTypes.MINUS,
288            TokenTypes.MINUS_ASSIGN,
289            TokenTypes.MOD,
290            TokenTypes.MOD_ASSIGN,
291            TokenTypes.NOT_EQUAL,
292            TokenTypes.PLUS,
293            TokenTypes.PLUS_ASSIGN,
294            TokenTypes.QUESTION,
295            TokenTypes.RCURLY,
296            TokenTypes.SL,
297            TokenTypes.SLIST,
298            TokenTypes.SL_ASSIGN,
299            TokenTypes.SR,
300            TokenTypes.SR_ASSIGN,
301            TokenTypes.STAR,
302            TokenTypes.STAR_ASSIGN,
303            TokenTypes.LITERAL_ASSERT,
304            TokenTypes.TYPE_EXTENSION_AND,
305            TokenTypes.WILDCARD_TYPE,
306            TokenTypes.GENERIC_START,
307            TokenTypes.GENERIC_END,
308        };
309    }
310
311    @Override
312    public int[] getRequiredTokens() {
313        return CommonUtils.EMPTY_INT_ARRAY;
314    }
315
316    /**
317     * Sets whether or not empty method bodies are allowed.
318     * @param allow {@code true} to allow empty method bodies.
319     */
320    public void setAllowEmptyMethods(boolean allow) {
321        allowEmptyMethods = allow;
322    }
323
324    /**
325     * Sets whether or not empty constructor bodies are allowed.
326     * @param allow {@code true} to allow empty constructor bodies.
327     */
328    public void setAllowEmptyConstructors(boolean allow) {
329        allowEmptyConstructors = allow;
330    }
331
332    /**
333     * Sets whether or not to ignore the whitespace around the
334     * colon in an enhanced for loop.
335     * @param ignore {@code true} to ignore enhanced for colon.
336     */
337    public void setIgnoreEnhancedForColon(boolean ignore) {
338        ignoreEnhancedForColon = ignore;
339    }
340
341    /**
342     * Sets whether or not empty type bodies are allowed.
343     * @param allow {@code true} to allow empty type bodies.
344     */
345    public void setAllowEmptyTypes(boolean allow) {
346        allowEmptyTypes = allow;
347    }
348
349    /**
350     * Sets whether or not empty loop bodies are allowed.
351     * @param allow {@code true} to allow empty loops bodies.
352     */
353    public void setAllowEmptyLoops(boolean allow) {
354        allowEmptyLoops = allow;
355    }
356
357    /**
358     * Sets whether or not empty lambdas bodies are allowed.
359     * @param allow {@code true} to allow empty lambda expressions.
360     */
361    public void setAllowEmptyLambdas(boolean allow) {
362        allowEmptyLambdas = allow;
363    }
364
365    @Override
366    public void visitToken(DetailAST ast) {
367        final int currentType = ast.getType();
368        if (!isNotRelevantSituation(ast, currentType)) {
369            final String line = getLine(ast.getLineNo() - 1);
370            final int before = ast.getColumnNo() - 1;
371            final int after = ast.getColumnNo() + ast.getText().length();
372
373            if (before >= 0) {
374                final char prevChar = line.charAt(before);
375                if (shouldCheckSeparationFromPreviousToken(ast)
376                        && !Character.isWhitespace(prevChar)) {
377                    log(ast.getLineNo(), ast.getColumnNo(),
378                            MSG_WS_NOT_PRECEDED, ast.getText());
379                }
380            }
381
382            if (after < line.length()) {
383                final char nextChar = line.charAt(after);
384                if (shouldCheckSeparationFromNextToken(ast, nextChar)
385                        && !Character.isWhitespace(nextChar)) {
386                    log(ast.getLineNo(), ast.getColumnNo() + ast.getText().length(),
387                            MSG_WS_NOT_FOLLOWED, ast.getText());
388                }
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                && ast.getType() != TokenTypes.ARRAY_INIT
453                && !isAnonymousInnerClassEnd(ast.getType(), nextChar)
454                && !isPartOfDoubleBraceInitializerForNextToken(ast);
455    }
456
457    /**
458     * Check for "})" or "};" or "},". Happens with anon-inners
459     * @param currentType token
460     * @param nextChar next symbol
461     * @return true is that is end of anon inner class
462     */
463    private static boolean isAnonymousInnerClassEnd(int currentType, char nextChar) {
464        return currentType == TokenTypes.RCURLY
465                && (nextChar == ')'
466                        || nextChar == ';'
467                        || nextChar == ','
468                        || nextChar == '.');
469    }
470
471    /**
472     * Is empty block.
473     * @param ast ast
474     * @param parentType parent
475     * @return true is block is empty
476     */
477    private boolean isEmptyBlock(DetailAST ast, int parentType) {
478        return isEmptyMethodBlock(ast, parentType)
479                || isEmptyCtorBlock(ast, parentType)
480                || isEmptyLoop(ast, parentType)
481                || isEmptyLambda(ast, parentType);
482    }
483
484    /**
485     * Tests if a given {@code DetailAST} is part of an empty block.
486     * An example empty block might look like the following
487     * <p>
488     * <pre>   public void myMethod(int val) {}</pre>
489     * </p>
490     * In the above, the method body is an empty block ("{}").
491     *
492     * @param ast the {@code DetailAST} to test.
493     * @param parentType the token type of {@code ast}'s parent.
494     * @param match the parent token type we're looking to match.
495     * @return {@code true} if {@code ast} makes up part of an
496     *         empty block contained under a {@code match} token type
497     *         node.
498     */
499    private static boolean isEmptyBlock(DetailAST ast, int parentType, int match) {
500        final int type = ast.getType();
501        if (type == TokenTypes.RCURLY) {
502            final DetailAST parent = ast.getParent();
503            final DetailAST grandParent = ast.getParent().getParent();
504            return parentType == TokenTypes.SLIST
505                    && parent.getFirstChild().getType() == TokenTypes.RCURLY
506                    && grandParent.getType() == match;
507        }
508
509        return type == TokenTypes.SLIST
510                && parentType == match
511                && ast.getFirstChild().getType() == TokenTypes.RCURLY;
512    }
513
514    /**
515     * Whether colon belongs to cases or defaults.
516     * @param currentType current
517     * @param parentType parent
518     * @return true if current token in colon of case or default tokens
519     */
520    private static boolean isColonOfCaseOrDefault(int currentType, int parentType) {
521        return currentType == TokenTypes.COLON
522                && (parentType == TokenTypes.LITERAL_DEFAULT
523                        || parentType == TokenTypes.LITERAL_CASE);
524    }
525
526    /**
527     * Whether colon belongs to for-each.
528     * @param currentType current
529     * @param parentType parent
530     * @return true if current token in colon of for-each token
531     */
532    private boolean isColonOfForEach(int currentType, int parentType) {
533        return currentType == TokenTypes.COLON
534                && parentType == TokenTypes.FOR_EACH_CLAUSE
535                && ignoreEnhancedForColon;
536    }
537
538    /**
539     * Is array initialization.
540     * @param currentType current token
541     * @param parentType parent token
542     * @return true is current token inside array initialization
543     */
544    private static boolean isArrayInitialization(int currentType, int parentType) {
545        return (currentType == TokenTypes.RCURLY || currentType == TokenTypes.LCURLY)
546                && (parentType == TokenTypes.ARRAY_INIT
547                        || parentType == TokenTypes.ANNOTATION_ARRAY_INIT);
548    }
549
550    /**
551     * Test if the given {@code DetailAST} is part of an allowed empty
552     * method block.
553     * @param ast the {@code DetailAST} to test.
554     * @param parentType the token type of {@code ast}'s parent.
555     * @return {@code true} if {@code ast} makes up part of an
556     *         allowed empty method block.
557     */
558    private boolean isEmptyMethodBlock(DetailAST ast, int parentType) {
559        return allowEmptyMethods
560                && isEmptyBlock(ast, parentType, TokenTypes.METHOD_DEF);
561    }
562
563    /**
564     * Test if the given {@code DetailAST} is part of an allowed empty
565     * constructor (ctor) block.
566     * @param ast the {@code DetailAST} to test.
567     * @param parentType the token type of {@code ast}'s parent.
568     * @return {@code true} if {@code ast} makes up part of an
569     *         allowed empty constructor block.
570     */
571    private boolean isEmptyCtorBlock(DetailAST ast, int parentType) {
572        return allowEmptyConstructors
573                && isEmptyBlock(ast, parentType, TokenTypes.CTOR_DEF);
574    }
575
576    /**
577     *
578     * @param ast ast the {@code DetailAST} to test.
579     * @param parentType the token type of {@code ast}'s parent.
580     * @return {@code true} if {@code ast} makes up part of an
581     *         allowed empty loop block.
582     */
583    private boolean isEmptyLoop(DetailAST ast, int parentType) {
584        return allowEmptyLoops
585                && (isEmptyBlock(ast, parentType, TokenTypes.LITERAL_FOR)
586                        || isEmptyBlock(ast, parentType, TokenTypes.LITERAL_WHILE)
587                        || isEmptyBlock(ast, parentType, TokenTypes.LITERAL_DO));
588    }
589
590    /**
591     * Test if the given {@code DetailAST} is part of an allowed empty
592     * lambda block.
593     * @param ast the {@code DetailAST} to test.
594     * @param parentType the token type of {@code ast}'s parent.
595     * @return {@code true} if {@code ast} makes up part of an
596     *         allowed empty lambda block.
597     */
598    private boolean isEmptyLambda(DetailAST ast, int parentType) {
599        return allowEmptyLambdas && isEmptyBlock(ast, parentType, TokenTypes.LAMBDA);
600    }
601
602    /**
603     * Test if the given {@code DetailAST} is part of an empty block.
604     * An example empty block might look like the following
605     * <p>
606     * <pre>   class Foo {}</pre>
607     * </p>
608     *
609     * @param ast ast the {@code DetailAST} to test.
610     * @return {@code true} if {@code ast} makes up part of an
611     *         empty block contained under a {@code match} token type
612     *         node.
613     */
614    private static boolean isEmptyType(DetailAST ast) {
615        final int type = ast.getType();
616        final DetailAST nextSibling = ast.getNextSibling();
617        final DetailAST previousSibling = ast.getPreviousSibling();
618        return type == TokenTypes.LCURLY
619                    && nextSibling.getType() == TokenTypes.RCURLY
620                || type == TokenTypes.RCURLY
621                    && previousSibling != null
622                    && previousSibling.getType() == TokenTypes.LCURLY;
623    }
624
625    /**
626     * Check if given ast is part of double brace initializer and if it
627     * should omit checking if previous token is separated by whitespace.
628     * @param ast ast to check
629     * @return true if it should omit checking for previous token, false otherwise
630     */
631    private static boolean isPartOfDoubleBraceInitializerForPreviousToken(DetailAST ast) {
632        final boolean initializerBeginsAfterClassBegins = ast.getType() == TokenTypes.SLIST
633                && ast.getParent().getType() == TokenTypes.INSTANCE_INIT;
634        final boolean classEndsAfterInitializerEnds = ast.getType() == TokenTypes.RCURLY
635                && ast.getPreviousSibling() != null
636                && ast.getPreviousSibling().getType() == TokenTypes.INSTANCE_INIT;
637        return initializerBeginsAfterClassBegins || classEndsAfterInitializerEnds;
638    }
639
640    /**
641     * Check if given ast is part of double brace initializer and if it
642     * should omit checking if next token is separated by whitespace.
643     * See <a href="https://github.com/checkstyle/checkstyle/pull/2845">
644     * PR#2845</a> for more information why this function was needed.
645     * @param ast ast to check
646     * @return true if it should omit checking for next token, false otherwise
647     */
648    private static boolean isPartOfDoubleBraceInitializerForNextToken(DetailAST ast) {
649        final boolean classBeginBeforeInitializerBegin = ast.getType() == TokenTypes.LCURLY
650            && ast.getNextSibling().getType() == TokenTypes.INSTANCE_INIT;
651        final boolean initalizerEndsBeforeClassEnds = ast.getType() == TokenTypes.RCURLY
652            && ast.getParent().getType() == TokenTypes.SLIST
653            && ast.getParent().getParent().getType() == TokenTypes.INSTANCE_INIT
654            && ast.getParent().getParent().getNextSibling().getType() == TokenTypes.RCURLY;
655        return classBeginBeforeInitializerBegin || initalizerEndsBeforeClassEnds;
656    }
657}