Since Checkstyle 3.0
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.
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' } } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.whitespace