Class WhitespaceAroundCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class WhitespaceAroundCheck
    extends AbstractCheck
    Checks that a token is surrounded by whitespace. Empty constructor, method, class, enum, interface, loop bodies (blocks), lambdas of the form
     public MyClass() {}      // empty constructor
     public void func() {}    // empty method
     public interface Foo {} // empty interface
     public class Foo {} // empty class
     public enum Foo {} // empty enum
     MyClass c = new MyClass() {}; // empty anonymous class
     while (i = 1) {} // empty while loop
     for (int i = 1; i > 1; i++) {} // empty for loop
     do {} while (i = 1); // empty do-while loop
     Runnable noop = () -> {}; // empty lambda
     public @interface Beta {} // empty annotation type
     

    may optionally be exempted from the policy using the allowEmptyMethods, allowEmptyConstructors, allowEmptyTypes, allowEmptyLoops, allowEmptyLambdas, allowEmptyCatches and allowEmptySwitchBlockStatements properties.

    This check does not flag as violation double brace initialization like:

     new Properties() {{
         setProperty("key", "value");
     }};
     

    Parameter allowEmptyCatches allows to suppress violations when token list contains SLIST to check if beginning of block is surrounded by whitespace and catch block is empty, for example:

     try {
         k = 5 / i;
     } catch (ArithmeticException ex) {}
     

    With this property turned off, this raises violation because the beginning of the catch block (left curly bracket) is not separated from the end of the catch block (right curly bracket).

    Note: Switch expressions are ignored by this check.

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • ws.notFollowed
    • ws.notPreceded
    Since:
    3.0
    • Method Detail

      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Description copied from class: AbstractCheck
        The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
        Specified by:
        getAcceptableTokens in class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • setAllowEmptyMethods

        public void setAllowEmptyMethods​(boolean allow)
        Setter to allow empty method bodies.
        Parameters:
        allow - true to allow empty method bodies.
        Since:
        4.0
      • setAllowEmptyConstructors

        public void setAllowEmptyConstructors​(boolean allow)
        Setter to allow empty constructor bodies.
        Parameters:
        allow - true to allow empty constructor bodies.
        Since:
        4.0
      • setIgnoreEnhancedForColon

        public void setIgnoreEnhancedForColon​(boolean ignore)
        Setter to ignore whitespace around colon in enhanced for loop.
        Parameters:
        ignore - true to ignore enhanced for colon.
        Since:
        5.5
      • setAllowEmptyTypes

        public void setAllowEmptyTypes​(boolean allow)
        Setter to allow empty class, interface and enum bodies.
        Parameters:
        allow - true to allow empty type bodies.
        Since:
        5.8
      • setAllowEmptyLoops

        public void setAllowEmptyLoops​(boolean allow)
        Setter to allow empty loop bodies.
        Parameters:
        allow - true to allow empty loops bodies.
        Since:
        5.8
      • setAllowEmptyLambdas

        public void setAllowEmptyLambdas​(boolean allow)
        Setter to allow empty lambda bodies.
        Parameters:
        allow - true to allow empty lambda expressions.
        Since:
        6.14
      • setAllowEmptyCatches

        public void setAllowEmptyCatches​(boolean allow)
        Setter to allow empty catch bodies.
        Parameters:
        allow - true to allow empty catch blocks.
        Since:
        7.6
      • setAllowEmptySwitchBlockStatements

        public void setAllowEmptySwitchBlockStatements​(boolean allow)
        Setter to allow empty switch blocks and block statements.
        Parameters:
        allow - true to allow empty switch case and default blocks.
        Since:
        10.19.0
      • isNotRelevantSituation

        private boolean isNotRelevantSituation​(DetailAST ast,
                                               int currentType)
        Is ast not a target of Check.
        Parameters:
        ast - ast
        currentType - type of ast
        Returns:
        true is ok to skip validation
      • shouldCheckSeparationFromPreviousToken

        private static boolean shouldCheckSeparationFromPreviousToken​(DetailAST ast)
        Check if it should be checked if previous token is separated from current by whitespace. This function is needed to recognise double brace initialization as valid, unfortunately it's not possible to implement this functionality in isNotRelevantSituation method, because in this method when we return true(is not relevant) ast is later doesn't check at all. For example: new Properties() {{setProperty("double curly braces", "are not a style violation"); }}; For second left curly brace in first line when we would return true from isNotRelevantSituation it wouldn't later check that the next token(setProperty) is not separated from previous token.
        Parameters:
        ast - current AST.
        Returns:
        true if it should be checked if previous token is separated by whitespace, false otherwise.
      • shouldCheckSeparationFromNextToken

        private boolean shouldCheckSeparationFromNextToken​(DetailAST ast,
                                                           char nextChar)
        Check if it should be checked if next token is separated from current by whitespace. Explanation why this method is needed is identical to one included in shouldCheckSeparationFromPreviousToken method.
        Parameters:
        ast - current AST.
        nextChar - next character.
        Returns:
        true if it should be checked if next token is separated by whitespace, false otherwise.
      • isAnonymousInnerClassEnd

        private static boolean isAnonymousInnerClassEnd​(int currentType,
                                                        char nextChar)
        Check for "})" or "};" or "},". Happens with anon-inners
        Parameters:
        currentType - token
        nextChar - next symbol
        Returns:
        true is that is end of anon inner class
      • isEmptyBlock

        private boolean isEmptyBlock​(DetailAST ast,
                                     int parentType)
        Is empty block.
        Parameters:
        ast - ast
        parentType - parent
        Returns:
        true is block is empty
      • isEmptyBlock

        private static boolean isEmptyBlock​(DetailAST ast,
                                            int parentType,
                                            int match)
        Tests if a given DetailAST is part of an empty block. An example empty block might look like the following
           public void myMethod(int val) {}
        In the above, the method body is an empty block ("{}").
        Parameters:
        ast - the DetailAST to test.
        parentType - the token type of ast's parent.
        match - the parent token type we're looking to match.
        Returns:
        true if ast makes up part of an empty block contained under a match token type node.
      • isEmptyMethodBlock

        private boolean isEmptyMethodBlock​(DetailAST ast,
                                           int parentType)
        Test if the given DetailAST is part of an allowed empty method block.
        Parameters:
        ast - the DetailAST to test.
        parentType - the token type of ast's parent.
        Returns:
        true if ast makes up part of an allowed empty method block.
      • isEmptyCtorBlockCheckedFromRcurly

        private boolean isEmptyCtorBlockCheckedFromRcurly​(DetailAST ast)
        Test if the given DetailAST is part of an allowed empty constructor (ctor) block checked from RCURLY.
        Parameters:
        ast - the DetailAST to test.
        Returns:
        true if ast makes up part of an allowed empty constructor block.
      • isEmptyCtorBlockCheckedFromSlist

        private boolean isEmptyCtorBlockCheckedFromSlist​(DetailAST ast)
        Test if the given DetailAST is a part of an allowed empty constructor checked from SLIST token.
        Parameters:
        ast - the DetailAST to test.
        Returns:
        true if ast makes up part of an empty constructor block.
      • isEmptyLoop

        private boolean isEmptyLoop​(DetailAST ast,
                                    int parentType)
        Checks if loop is empty.
        Parameters:
        ast - ast the DetailAST to test.
        parentType - the token type of ast's parent.
        Returns:
        true if ast makes up part of an allowed empty loop block.
      • isEmptyLambda

        private boolean isEmptyLambda​(DetailAST ast,
                                      int parentType)
        Test if the given DetailAST is part of an allowed empty lambda block.
        Parameters:
        ast - the DetailAST to test.
        parentType - the token type of ast's parent.
        Returns:
        true if ast makes up part of an allowed empty lambda block.
      • isEmptyCatch

        private boolean isEmptyCatch​(DetailAST ast,
                                     int parentType)
        Tests if the given DetailAst is part of an allowed empty catch block.
        Parameters:
        ast - the DetailAst to test.
        parentType - the token type of ast's parent
        Returns:
        true if ast makes up part of an allowed empty catch block.
      • isEmptySwitchBlockStatement

        private boolean isEmptySwitchBlockStatement​(DetailAST ast)
        Tests if the given DetailAst is part of an allowed empty switch case or default block.
        Parameters:
        ast - the DetailAst to test.
        Returns:
        true if ast makes up part of an allowed empty switch case or default block.
      • isEmptyType

        private static boolean isEmptyType​(DetailAST ast)
        Test if the given DetailAST is part of an empty block. An example empty block might look like the following
           class Foo {}
        Parameters:
        ast - ast the DetailAST to test.
        Returns:
        true if ast makes up part of an empty block contained under a match token type node.
      • isPartOfDoubleBraceInitializerForPreviousToken

        private static boolean isPartOfDoubleBraceInitializerForPreviousToken​(DetailAST ast)
        Check if given ast is part of double brace initializer and if it should omit checking if previous token is separated by whitespace.
        Parameters:
        ast - ast to check
        Returns:
        true if it should omit checking for previous token, false otherwise
      • isPartOfDoubleBraceInitializerForNextToken

        private static boolean isPartOfDoubleBraceInitializerForNextToken​(DetailAST ast)
        Check if given ast is part of double brace initializer and if it should omit checking if next token is separated by whitespace. See PR#2845 for more information why this function was needed.
        Parameters:
        ast - ast to check
        Returns:
        true if it should omit checking for next token, false otherwise