WhitespaceAround
Since Checkstyle 3.0
Description
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.
Properties
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 '}'
int y = 0;
int a = 4;
void example() {
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) { }
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
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 {
public Example3(){} // violation ''}' is not preceded with whitespace'
int y = 0;
int a = 4;
void example() {
Runnable noop = () ->{}; // violation ''}' is not preceded with whitespace'
try { }
catch (Exception e){} // violation ''}' is not preceded with whitespace'
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char item: vowels) { }
for (int i = 100;i > 10; i--){}
// violation above ''}' is not preceded with whitespace'
do {} while (y == 1); // violation ''}' is not preceded with whitespace'
int i = 0;
switch (i) {
case 1: {} // violation ''}' is not preceded with whitespace'
}
int a=4;
}
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 Example4(){} // 3 violations
// no space after ')' and '{', no space before '}'
int y = 0;
int a = 4;
void example() {
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) { }
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {}
void myFunction2() { }
}
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(){} // violation, no space after ')'
int y = 0;
int a = 4;
void example() {
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) { }
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
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 {
public Example6(){} // 3 violations
// no space after ')' and '{', no space before '}'
int y = 0;
int a = 4;
void example() {
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) { }
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
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 {
public Example7(){} // 3 violations
// no space after ')' and '{', no space before '}'
int y = 0;
int a = 4;
void example() {
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) { }
for (int i = 100;i > 10; i--){}
do {} while (y == 1);
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
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 {
public Example8(){} // 3 violations
// no space after ')' and '{', no space before '}'
int y = 0;
int a = 4;
void example() {
Runnable noop = () ->{}; // violation, no space after '->'
try { }
catch (Exception e){} // 3 violations
// no space after ')' and '{', no space before '}'
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char item: vowels) { }
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
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 {
public Example9(){} // 3 violations
// no space after ')' and '{', no space before '}'
int y = 0;
int a = 4;
void example() {
Runnable noop = () ->{}; // 4 violations
// no space after '->' and '{', no space before '{' and '}'
try { }
catch (Exception e){}
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char item: vowels) { }
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
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 {
public Example10(){} // 3 violations
// no space after ')' and '{', no space before '}'
int y = 0;
int a = 4;
void example() {
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) { } // violation '':' is not preceded with whitespace'
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {} // 2 violations
// no space after '{', no space before '}'
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
To configure the check to allow empty switch block statements:
<module name="Checker">
<module name="TreeWalker">
<module name="WhitespaceAround">
<property name="allowEmptySwitchBlockStatements" value="true"/>
</module>
</module>
</module>
Example:
class Example11 {
public Example11(){} // 3 violations
// no space after ')' and '{', no space before '}'
int y = 0;
int a = 4;
void example() {
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) { }
for (int i = 100;i > 10; i--){} // 3 violations
// no space before '{', no space after '{', no space before '}'
do {} while (y == 1); // 2 violations
// no space after '{', no space before '}'
int i = 0;
switch (i) {
case 1: {}
}
int a=4; // 2 violations
// no space before '=', no space after '='
}
void myFunction() {} // 2 violations
// no space after '{', no space before '}'
void myFunction2() { }
}
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;
}
}
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.
Fully Qualified Name
com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck
Use this fully qualified class name in configuration when an exact class reference is required.






