NoWhitespaceAfter

Since Checkstyle 3.0

Description

Checks that there is no whitespace after a token. More specifically, it checks that it is not followed by whitespace, or (if linebreaks are allowed) all characters on the line after are whitespace. To forbid linebreaks after a token, set property allowLineBreaks to false.

The check processes ARRAY_DECLARATOR and INDEX_OP tokens specially from other tokens. Actually it is checked that there is no whitespace before these tokens, not after them. Space after the ANNOTATIONS before ARRAY_DECLARATOR and INDEX_OP will be ignored.

If the annotation is between the type and the array, like char @NotNull [] param, the check will skip validation for spaces.

Note: This check processes the LITERAL_SYNCHRONIZED token only when it appears as a part of a synchronized statement, i.e. synchronized(this) {}.

Properties

name description type default value since
allowLineBreaks Control whether whitespace is allowed if the token is at a linebreak. boolean true 3.0
tokens tokens to check subset of tokens ARRAY_INIT , AT , INC , DEC , UNARY_MINUS , UNARY_PLUS , BNOT , LNOT , DOT , TYPECAST , ARRAY_DECLARATOR , INDEX_OP , LITERAL_SYNCHRONIZED , METHOD_REF . ARRAY_INIT , AT , INC , DEC , UNARY_MINUS , UNARY_PLUS , BNOT , LNOT , DOT , ARRAY_DECLARATOR , INDEX_OP . 3.0

Examples

To configure the check:

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

Example:

class Example1 {
  public void lineBreak(String x) {
    Integer.
        parseInt(x);
    Integer.parseInt(x);
  }

  public void dotOperator(String s) {
    Integer.parseInt(s);
    Integer. parseInt(s); // violation ''.' is followed by whitespace'
  }

  public void arrayDec() {
    int[] arr;
    int [] array; // violation ''int' is followed by whitespace'
  }

  public void bitwiseNot(int a) {
    a = ~ a; // violation ''~' is followed by whitespace'
    a = ~a;
  }
}
        

To configure the check to forbid linebreaks after a DOT token:

<module name="Checker">
  <module name="TreeWalker">
    <module name="NoWhitespaceAfter">
      <property name="allowLineBreaks" value="false"/>
      <property name="tokens" value="DOT"/>
    </module>
  </module>
</module>
        

Example:

class Example2 {
  public void lineBreak(String x) {
    Integer.
        parseInt(x); // violation above ''.' is followed by whitespace'
    Integer.parseInt(x);
  }

  public void dotOperator(String s) {
    Integer.parseInt(s);
    Integer. parseInt(s); // violation ''.' is followed by whitespace'
  }

  public void arrayDec() {
    int[] arr;
    int [] array;
  }

  public void bitwiseNot(int a) {
    a = ~ a;
    a = ~a;
  }
}
        

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