WhitespaceBeforeEmptyBody
Since Checkstyle 13.9.0
Description
This check applies to the following tokens: Methods and constructors, Classes, interfaces, enums, and records, Annotation definitions, Loops: while, for, do-while, Lambdas and anonymous classes, Static initializer blocks, Try-catch-finally statements, Synchronized blocks, Switch statements.
A body containing only comments is considered empty by this check.
Properties
| name | description | type | default value | since |
|---|---|---|---|---|
| tokens | tokens to check | subset of tokens METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF , CLASS_DEF , INTERFACE_DEF , RECORD_DEF , ENUM_DEF , ANNOTATION_DEF , LITERAL_NEW , LITERAL_DO , LITERAL_WHILE , LITERAL_FOR , LITERAL_IF , LITERAL_ELSE , STATIC_INIT , LITERAL_TRY , LITERAL_CATCH , LITERAL_FINALLY , LITERAL_SYNCHRONIZED , LITERAL_SWITCH , LAMBDA . | METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF , CLASS_DEF , INTERFACE_DEF , RECORD_DEF , ENUM_DEF , ANNOTATION_DEF , LITERAL_NEW , LITERAL_DO , LITERAL_WHILE , LITERAL_FOR , LITERAL_IF , LITERAL_ELSE , STATIC_INIT , LITERAL_TRY , LITERAL_CATCH , LITERAL_FINALLY , LITERAL_SYNCHRONIZED , LITERAL_SWITCH , LAMBDA . | 13.9.0 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="WhitespaceBeforeEmptyBody"/>
</module>
</module>
Example:
class Example1 {
void method(){} // violation ''{' is not preceded with whitespace'
void method2(){ // violation ''{' is not preceded with whitespace'
// comment
}
void method3() {
for (int i = 0; i < 1; i++){}
// violation above ''{' is not preceded with whitespace'
}
class Internal{} // violation ''{' is not preceded with whitespace'
}
To configure the check for only methods and classes:
<module name="Checker">
<module name="TreeWalker">
<module name="WhitespaceBeforeEmptyBody">
<property name="tokens"
value="METHOD_DEF, CLASS_DEF"/>
</module>
</module>
</module>
Example:
class Example2 {
void method(){} // violation ''{' is not preceded with whitespace'
void method2(){ // violation ''{' is not preceded with whitespace'
// comment
}
void method3() {
for (int i = 0; i < 1; i++){}
}
class Internal{} // 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.
Fully Qualified Name
com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceBeforeEmptyBodyCheck
Use this fully qualified class name in configuration when an exact class reference is required.






