Class WhitespaceAroundCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class WhitespaceAroundCheck
    extends AbstractCheck
    Checks that a token is surrounded by whitespace.

    By default the check will check the following operators: ASSERT, ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, DO_WHILE, EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN, LITERAL_ASSERT, TYPE_EXTENSION_AND.

    An example of how to configure the check is:

     <module name="WhitespaceAround"/>
     

    An example of how to configure the check for whitespace only around assignment operators is:

     <module name="WhitespaceAround">
         <property name="tokens"
                   value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,
                          MOD_ASSIGN,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BXOR_ASSIGN,
                          BOR_ASSIGN,BAND_ASSIGN"/>
     </module>
     

    An example of how to configure the check for whitespace only around curly braces is:

     <module name="WhitespaceAround">
         <property name="tokens"
                   value="LCURLY,RCURLY"/>
     </module>
     

    In addition, this check can be configured to allow empty methods, types, for, while, do-while loops, lambdas and constructor bodies. For example:

    
     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 &gt; 1; i++) {} // empty for loop
     do {} while (i = 1); // empty do-while loop
     Runnable noop = () -> {}; // empty lambda
     public @interface Beta {} // empty annotation type
     

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

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

    To configure the check to allow empty method blocks use

       <property name="allowEmptyMethods" value="true" />

    To configure the check to allow empty constructor blocks use

       <property name="allowEmptyConstructors" value="true" />

    To configure the check to allow empty type blocks use

       <property name="allowEmptyTypes" value="true" />

    To configure the check to allow empty loop blocks use

       <property name="allowEmptyLoops" value="true" />

    To configure the check to allow empty lambdas blocks use

       <property name="allowEmptyLambdas" value="true" />

    Also, this check can be configured to ignore the colon in an enhanced for loop. The colon in an enhanced for loop is ignored by default

    To configure the check to ignore the colon

       <property name="ignoreEnhancedForColon" value="true" />
    Author:
    Oliver Burn, maxvetrenko, Andrei Selkin
    • 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)
        Sets whether or not empty method bodies are allowed.
        Parameters:
        allow - true to allow empty method bodies.
      • setAllowEmptyConstructors

        public void setAllowEmptyConstructors​(boolean allow)
        Sets whether or not empty constructor bodies are allowed.
        Parameters:
        allow - true to allow empty constructor bodies.
      • setIgnoreEnhancedForColon

        public void setIgnoreEnhancedForColon​(boolean ignore)
        Sets whether or not to ignore the whitespace around the colon in an enhanced for loop.
        Parameters:
        ignore - true to ignore enhanced for colon.
      • setAllowEmptyTypes

        public void setAllowEmptyTypes​(boolean allow)
        Sets whether or not empty type bodies are allowed.
        Parameters:
        allow - true to allow empty type bodies.
      • setAllowEmptyLoops

        public void setAllowEmptyLoops​(boolean allow)
        Sets whether or not empty loop bodies are allowed.
        Parameters:
        allow - true to allow empty loops bodies.
      • setAllowEmptyLambdas

        public void setAllowEmptyLambdas​(boolean allow)
        Sets whether or not empty lambdas bodies are allowed.
        Parameters:
        allow - true to allow empty lambda expressions.
      • setAllowEmptyCatches

        public void setAllowEmptyCatches​(boolean allow)
        Sets whether or not empty catch blocks are allowed.
        Parameters:
        allow - true to allow empty catch blocks.