WhitespaceAfter
Since Checkstyle 3.0
Description
Checks that a token is followed by whitespace, with the exception that it does not check for whitespace after the semicolon of an empty for iterator. Use Check EmptyForIteratorPad to validate empty for iterators.
Properties
| name | description | type | default value | since |
|---|---|---|---|---|
| tokens | tokens to check | subset of tokens COMMA , SEMI , TYPECAST , LITERAL_IF , LITERAL_ELSE , LITERAL_WHILE , LITERAL_DO , LITERAL_FOR , LITERAL_FINALLY , LITERAL_RETURN , LITERAL_YIELD , LITERAL_CATCH , DO_WHILE , ELLIPSIS , LITERAL_SWITCH , LITERAL_SYNCHRONIZED , LITERAL_TRY , LITERAL_CASE , LAMBDA , LITERAL_WHEN , ANNOTATIONS . | COMMA , SEMI , TYPECAST , LITERAL_IF , LITERAL_ELSE , LITERAL_WHILE , LITERAL_DO , LITERAL_FOR , LITERAL_FINALLY , LITERAL_RETURN , LITERAL_YIELD , LITERAL_CATCH , DO_WHILE , ELLIPSIS , LITERAL_SWITCH , LITERAL_SYNCHRONIZED , LITERAL_TRY , LITERAL_CASE , LAMBDA , LITERAL_WHEN . | 3.0 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="WhitespaceAfter"/>
</module>
</module>
Example:
class Example1 {
int a = 0; int b = 1;
int c = 2;int d = 3; // violation 'not followed by whitespace'
void example() throws Exception {
if (true) {} else if(false) {} // violation 'not followed by whitespace'
test("a", "b");
test("a","b"); // violation 'not followed by whitespace'
for (int i = 0; i < 1; i++) {}
for(int i = 0; i < 1; i++) {} // violation 'not followed by whitespace'
try (InputStream ignored = System.in) {}
try(InputStream ignored = System.in) {} // violation 'not followed by whitespace'
try {} catch (Exception e) {}
try {} catch(Exception e) {} // violation 'not followed by whitespace'
yieldExample();
}
String test(String a, String b) {
return(a + b); // violation 'not followed by whitespace'
}
void yieldExample() {
int x = switch ("a") {
case "a" -> { yield(1); } // violation ''yield' is not followed by whitespace'
default -> { yield 2; }
};
}
}
To configure the check for whitespace only after COMMA and SEMI tokens:
<module name="Checker">
<module name="TreeWalker">
<module name="WhitespaceAfter">
<property name="tokens" value="COMMA, SEMI"/>
</module>
</module>
</module>
Example:
class Example2 {
int a = 0; int b = 1;
int c = 2;int d = 3; // violation 'not followed by whitespace'
void example() throws Exception {
if (true) {} else if(false) {}
test("a", "b");
test("a","b"); // violation 'not followed by whitespace'
for (int i = 0; i < 1; i++) {}
for(int i = 0; i < 1; i++) {}
try (InputStream ignored = System.in) {}
try(InputStream ignored = System.in) {}
try {} catch (Exception e) {}
try {} catch(Exception e) {}
yieldExample();
}
String test(String a, String b) {
return(a + b);
}
void yieldExample() {
int x = switch ("a") {
case "a" -> { yield(1); }
default -> { yield 2; }
};
}
}
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.WhitespaceAfterCheck
Use this fully qualified class name in configuration when an exact class reference is required.






