WhitespaceAround

Since Checkstyle 3.0

Description

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 and allowEmptyCatches 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.

Properties

name description type default value since
allowEmptyCatches Allow empty catch bodies. boolean false 7.6
allowEmptyConstructors Allow empty constructor bodies. boolean false 4.0
allowEmptyLambdas Allow empty lambda bodies. boolean false 6.14
allowEmptyLoops Allow empty loop bodies. boolean false 5.8
allowEmptyMethods Allow empty method bodies. boolean false 4.0
allowEmptyTypes Allow empty class, interface and enum bodies. boolean false 5.8
ignoreEnhancedForColon Ignore whitespace around colon in enhanced for loop. boolean true 5.5
tokens tokens to check subset of tokens ASSIGN , ARRAY_INIT , BAND , BAND_ASSIGN , BOR , BOR_ASSIGN , BSR , BSR_ASSIGN , BXOR , BXOR_ASSIGN , COLON , DIV , DIV_ASSIGN , DO_WHILE , EQUAL , GE , GT , LAMBDA , 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 , WILDCARD_TYPE , GENERIC_START , GENERIC_END , ELLIPSIS , LITERAL_WHEN . ASSIGN , BAND , BAND_ASSIGN , BOR , BOR_ASSIGN , BSR , BSR_ASSIGN , BXOR , BXOR_ASSIGN , COLON , DIV , DIV_ASSIGN , DO_WHILE , EQUAL , GE , GT , LAMBDA , 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 , LITERAL_WHEN . 3.0

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround"/>
  </module>
</module>
        

Example:

class Example1 {
  public Example1(){} // 3 violations
  // no space after ')' and '{', no space before '}'
  public static void main(String[] args) {
    if (true) { }
    else{ // 2 violations
         // no space after 'else', no space before '}'
    }

    for (int i = 1; i > 1; i++) {} // 2 violations
    // no space after '{', no space before '}'

    Runnable noop = () ->{}; // 4 violations
    // no space after '->' and '{', no space before '{' and '}'
    try { }
    catch (Exception e){} // 3 violations
    // no space after ')' and '{', no space before '}'
    char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    for (char item: vowels) { // OK, ignoreEnhancedForColon is true by default

    }
  }
}
        

To configure the check for whitespace only around assignment operators:

<module name="Checker">
  <module name="TreeWalker">
    <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>
  </module>
</module>
        

Example:

class Example2 {
  void example() {
    int b=10; // 2 violations
    // no space before and after'='
    int c = 10;
    b+=10; // 2 violations
    // no space before and after'+='
    b += 10;
    c*=10; // 2 violations
    // no space before and after'*='
    c *= 10;
    c-=5; // 2 violations
    // no space before and after'-='
    c -= 5;
    c/=2; // 2 violations
    // no space before and after'/='
    c /= 2;
    c%=1; // 2 violations
    // no space before and after'%='
    c %= 1;
    c>>=1; // 2 violations
    // no space before and after'>>='
    c >>= 1;
    c>>>=1; // 2 violations
    // no space before and after'>>>='
    c >>>= 1;
  }
}
        

To configure the check for whitespace only around curly braces:

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

Example:

class Example3 {
  void myFunction() {} // violation ''}' is not preceded with whitespace'
  void myFunction2() { }
}
        

To configure the check to allow empty method bodies:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround">
      <property name="allowEmptyMethods" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example4 {
  public void muFunction() {}
  int a=4; // 2 violations
  // no space before and after '='
}
        

To configure the check to allow empty constructor bodies:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround">
      <property name="allowEmptyConstructors" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example5 {
  public Example5() {}
  public void myFunction() {} // 2 violations
  // no space after '{', no space before '}'
}
        

To configure the check to allow empty type bodies:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround">
      <property name="allowEmptyTypes" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example6 {
  class Test {}
  interface testInterface{}
  class anotherTest {
    int a=4; // 2 violations
    // no space before and after '='
  }
}
        

To configure the check to allow empty loop bodies:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround">
      <property name="allowEmptyLoops" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example7 {
  int y = 0;
  void example() {
    for (int i = 100;i > 10; i--){}
    do {} while (y == 1);
    int a=4; // 2 violations
    // no space before and after '='
  }
}
        

To configure the check to allow empty lambda bodies:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround">
      <property name="allowEmptyLambdas" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example8 {
  void example() {
    Runnable noop = () -> {};
    int a=4; // 2 violations
    // no space before and after '='
  }
}
        

To configure the check to allow empty catch bodies:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround">
      <property name="allowEmptyCatches" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example9 {
  void example() {
    int a=4; // 2 violations
    // no space before and after '='
    try {
    } catch (Exception e){}
  }
}
        

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:

<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAround">
      <property name="ignoreEnhancedForColon" value="false"/>
    </module>
  </module>
</module>
        

Example:

class Example10 {
  void example() {
    int a=4; // 2 violations
    // no space before and after '='
    char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    for (char item: vowels) { // violation '':' is not preceded with whitespace'
    }
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker