EmptyForInitializerPad

Description

Since Checkstyle 3.4

Checks the padding of an empty for initializer; that is whether white space is required at an empty for initializer, or such white space is forbidden. No check occurs if there is a line wrap at the initializer, as in

for (
      ; i < j; i++, j--)
        

Properties

name description type default value since
option policy on how to pad an empty for iterator Pad Policy nospace 3.4

Examples

To configure the check:

<module name="EmptyForInitializerPad"/>
        

To configure the check to require white space at an empty for iterator:

<module name="EmptyForInitializerPad">
    <property name="option" value="space"/>
</module>
        

Example of Usage

Error 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

EmptyForIteratorPad

Description

Since Checkstyle 3.0

Checks the padding of an empty for iterator; that is whether white space is required at an empty for iterator, or such white space is forbidden. No check occurs if there is a line wrap at the iterator, as in

for (Iterator foo = very.long.line.iterator();
      foo.hasNext();
     )
        

Properties

name description type default value since
option policy on how to pad an empty for iterator Pad Policy nospace 3.0

Examples

To configure the check:

<module name="EmptyForIteratorPad"/>
        

To configure the check to require white space at an empty for iterator:

<module name="EmptyForIteratorPad">
    <property name="option" value="space"/>
</module>
        

Example of Usage

Error 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

EmptyLineSeparator

Description

Since Checkstyle 5.8

Checks for empty line separators after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.

ATTENTION: empty line separator is required between AST siblings, not after line where token is found.

Properties

name description type default value since
allowNoEmptyLineBetweenFields Allow no empty line between fields Boolean false 5.8
allowMultipleEmptyLines Allow multiple empty lines between class members Boolean true 6.3
allowMultipleEmptyLinesInsideClassMembers Allow multiple empty lines inside class members Boolean true 6.18
tokens tokens to check subset of tokens PACKAGE_DEF, IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF. PACKAGE_DEF, IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF. 5.8

Examples

Example of declarations without empty line separator:

///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.whitespace;
import java.io.Serializable;
class Foo
{
    public static final int FOO_CONST = 1;
    public void foo() {} //should be separated from previous statement.
}
      

An example of how to configure the check with default parameters is:

<module name="EmptyLineSeparator"/>
      

Example of declarations with empty line separator that is expected by the Check by default:

///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.whitespace;

import java.io.Serializable;

class Foo
{
    public static final int FOO_CONST = 1;

    public void foo() {} //should be separated from previous statement.
}
      

An example how to check empty line after VARIABLE_DEF and METHOD_DEF:

<module name="EmptyLineSeparator">
    <property name="tokens" value="VARIABLE_DEF, METHOD_DEF"/>
</module>
      

An example how to allow no empty line between fields:

<module name="EmptyLineSeparator">
    <property name="allowNoEmptyLineBetweenFields" value="true"/>
</module>
      

An example how to disallow multiple empty lines inside constructor, initialization block and method:

          <module name="EmptyLineSeparator">
          <property name="allowMultipleEmptyLinesInsideClassMembers" value="false"/>
          </module>
      

The check is valid only for statements that have body: CLASS_DEF, INTERFACE_DEF, ENUM_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF,

Example of declarations with multiple empty lines inside method:

          ///////////////////////////////////////////////////
          //HEADER
          ///////////////////////////////////////////////////

          package com.puppycrawl.tools.checkstyle.whitespace;

          class Foo
          {

              public void foo() {
              

                  System.out.println(1); // violation since method has 2 empty lines subsequently
              }
          }
      

Error 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

FileTabCharacter

Description

Since Checkstyle 5.0

Checks that there are no tab characters ('\t') in the source code.

Rationale:

  • Developers should not need to configure the tab width of their text editors in order to be able to read source code.
  • From the Apache jakarta coding standards: In a distributed development environment, when the commit messages get sent to a mailing list, they are almost impossible to read if you use tabs.

Properties

name description type default value since
eachLine whether to report on each line containing a tab, or just the first instance Boolean false 5.0
fileExtensions file type extension of files to process String Set all files 5.0

Examples

To configure the check to report on the first instance in each file:

<module name="FileTabCharacter"/>
        

To configure the check to report on each line in each file:

<module name="FileTabCharacter">
    <property name="eachLine" value="true"/>
</module>
        

Error 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

Checker

GenericWhitespace

Description

Since Checkstyle 5.0

Checks that the whitespace around the Generic tokens (angle brackets) "<" and ">" are correct to the typical convention. The convention is not configurable.

Left angle bracket ("<"):

  • should be preceded with whitespace only in generic methods definitions.
  • should not be preceded with whitespace when it is precede method name or following type name.
  • should not be followed with whitespace in all cases.

Right angle bracket (">"):

  • should not be preceded with whitespace in all cases.
  • should be followed with whitespace in almost all cases, except diamond operators and when preceding method name.

Examples with correct spacing:

public void <K, V extends Number> boolean foo(K, V) {} // Generic methods definitions
class name<T1, T2, ..., Tn> {}                         // Generic type definition
OrderedPair<String, Box<Integer>> p;                   // Generic type reference
boolean same = Util.<Integer, String>compare(p1, p2);  // Generic preceded method name
Pair<Integer, String> p1 = new Pair<>(1, "apple");     // Diamond operator
List<T> list = ImmutableList.Builder<T>::new;          // Method reference
sort(list, Comparable::<String>compareTo);             // Method reference
        

Examples

To configure the check:

<module name="GenericWhitespace"/>
        

Error 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

MethodParamPad

Description

Since Checkstyle 3.4

Checks the padding between the identifier of a method definition, constructor definition, method call, or constructor invocation; and the left parenthesis of the parameter list. That is, if the identifier and left parenthesis are on the same line, checks whether a space is required immediately after the identifier or such a space is forbidden. If they are not on the same line, reports an error, unless configured to allow line breaks. To allow linebreaks after the identifier, set property allowLineBreaks to true.

Properties

name description type default value since
allowLineBreaks whether a line break between the identifier and left parenthesis is allowed Boolean false 3.4
option policy on how to pad method parameter Pad Policy nospace 3.4
tokens tokens to check subset of tokens CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL, ENUM_CONSTANT_DEF. CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL, ENUM_CONSTANT_DEF. 3.4

Examples

To configure the check:

<module name="MethodParamPad"/>
        

To configure the check to require a space after the identifier of a method definition, except if the left parenthesis occurs on a new line:

<module name="MethodParamPad">
    <property name="tokens" value="METHOD_DEF"/>
    <property name="option" value="space"/>
    <property name="allowLineBreaks" value="true"/>
</module>
        

Error 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

NoLineWrap

Description

Since Checkstyle 5.8

Checks that chosen statements are not line-wrapped. By default this Check restricts wrapping import and package statements, but it's possible to check any statement.

Properties

name description type default value since
tokens tokens to check subset of tokens IMPORT, STATIC_IMPORT, PACKAGE_DEF, CLASS_DEF, METHOD_DEF, CTOR_DEF, ENUM_DEF, INTERFACE_DEF. PACKAGE_DEF, IMPORT, STATIC_IMPORT. 5.8

Examples

Examples of line-wrapped statements (bad case):

package com.puppycrawl. //violation
    tools.checkstyle.checks;

import com.puppycrawl.tools. //violation
    checkstyle.api.AbstractCheck;

import static java.math. //violation
    BigInteger.ZERO;
      

To configure the check to force no line-wrapping in package and import statements (default values):

<module name="NoLineWrap"/>
      

To configure the check to force no line-wrapping only in import statements:

<module name="NoLineWrap">
    <property name="tokens" value="IMPORT"/>
</module>
      

Examples of not line-wrapped statements (good case):

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import static java.math.BigInteger.ZERO;
      

Error 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

NoWhitespaceAfter

Description

Since Checkstyle 3.0

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 this tokens, not after them. Space after the ANNOTATIONS before ARRAY_DECLARATOR and INDEX_OP will be ignored.

Properties

name description type default value since
allowLineBreaks 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="NoWhitespaceAfter"/>
        

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

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

If the annotation is between the type and the array, the check will skip validation for spaces:

public void foo(final char @NotNull [] param) {} // No violation
        

Example of Usage

Error 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

NoWhitespaceBefore

Description

Since Checkstyle 3.0

Checks that there is no whitespace before a token. More specifically, it checks that it is not preceded with whitespace, or (if linebreaks are allowed) all characters on the line before are whitespace. To allow linebreaks before a token, set property allowLineBreaks to true. No check occurs before semi-colons in empty for loop initializers or conditions.

Properties

name description type default value since
allowLineBreaks whether whitespace is allowed if the token is at a linebreak Boolean false 3.0
tokens tokens to check subset of tokens COMMA, SEMI, POST_INC, POST_DEC, DOT, GENERIC_START, GENERIC_END, ELLIPSIS, METHOD_REF. COMMA, SEMI, POST_INC, POST_DEC, ELLIPSIS. 3.0

Examples

To configure the check:

<module name="NoWhitespaceBefore"/>
        

To configure the check to allow linebreaks before a DOT token:

<module name="NoWhitespaceBefore">
    <property name="tokens" value="DOT"/>
    <property name="allowLineBreaks" value="true"/>
</module>
        

Error 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

OperatorWrap

Description

Since Checkstyle 3.0

Checks the policy on how to wrap lines on operators.

Properties

name description type default value since
option policy on how to wrap lines Wrap Operator Policy nl 3.0
tokens tokens to check subset of tokens QUESTION, COLON, EQUAL, NOT_EQUAL, DIV, PLUS, MINUS, STAR, MOD, SR, BSR, GE, GT, SL, LE, LT, BXOR, BOR, LOR, BAND, LAND, LITERAL_INSTANCEOF, TYPE_EXTENSION_AND, ASSIGN, DIV_ASSIGN, PLUS_ASSIGN, MINUS_ASSIGN, STAR_ASSIGN, MOD_ASSIGN, SR_ASSIGN, BSR_ASSIGN, SL_ASSIGN, BXOR_ASSIGN, BOR_ASSIGN, BAND_ASSIGN, METHOD_REF. QUESTION, COLON, EQUAL, NOT_EQUAL, DIV, PLUS, MINUS, STAR, MOD, SR, BSR, GE, GT, SL, LE, LT, BXOR, BOR, LOR, BAND, LAND, TYPE_EXTENSION_AND, LITERAL_INSTANCEOF. 3.0

Examples

To configure the check:

<module name="OperatorWrap"/>
        

To configure the check for the assignment operator, =, at the end of a line:

<module name="OperatorWrap">
    <property name="tokens" value="ASSIGN"/>
    <property name="option" value="eol"/>
</module>
        

Error 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

ParenPad

Description

Since Checkstyle 3.0

Checks the policy on the padding of parentheses; i.e. whether a space is required after a left parenthesis and before a right parenthesis, or such spaces are forbidden. No check occurs at the right parenthesis after an empty for iterator, at the left parenthesis before an empty for initialization, or at the right parenthesis of a try-with-resources resource specification where the last resource variable has a trailing semi-colon. Use Check EmptyForIteratorPad to validate empty for iterators and EmptyForInitializerPad to validate empty for initializers. Typecasts are also not checked, as there is TypecastParenPad to validate them.

Examples

To configure the check:

<module name="ParenPad"/>
        

To configure the check to require spaces for the parentheses of constructor, method, and super constructor calls:

<module name="ParenPad">
    <property name="tokens" value="CTOR_CALL, METHOD_CALL,
    SUPER_CTOR_CALL"/>
    <property name="option" value="space"/>
</module>
        

The following cases not checked:

for ( ; i < j; i++, j--) // no check after left parenthesis
for (Iterator it = xs.iterator(); it.hasNext(); ) // no check before right parenthesis
try (Closeable resource = acquire(); ) // no check before right parenthesis
        

Error 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

SeparatorWrap

Description

Since Checkstyle 5.8

Checks line wrapping with separators.

Properties

name description type default value since
option policy on how to wrap lines Wrap Operator Policy eol 5.8
tokens tokens to check subset of tokens DOT, COMMA, SEMI, ELLIPSIS, AT, LPAREN, RPAREN, ARRAY_DECLARATOR, RBRACK, METHOD_REF. DOT, COMMA. 5.8

Examples

Code example for comma and dot at the new line:

s
    .isEmpty();
foo(i
    ,s);
        

An example of how to configure the check is:

<module name="SeparatorWrap"/>
        

Code example for comma and dot at the previous line:

s.
    isEmpty();
foo(i,
    s);
        

Example for checking method reference at new line (good case and bad case):

Arrays.sort(stringArray, String:: // violation
    compareToIgnoreCase);
Arrays.sort(stringArray, String
    ::compareToIgnoreCase); // good
        

An example of how to configure the check for METHOD_REF at new line:

<module name="SeparatorWrap">
    <property name="tokens" value="METHOD_REF"/>
    <property name="option" value="nl"/>
</module>
        

An example of how to configure the check for comma at the new line is:

<module name="SeparatorWrap">
    <property name="tokens" value="COMMA"/>
    <property name="option" value="nl"/>
</module>
        

Error 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

SingleSpaceSeparator

Description

Since Checkstyle 6.19

Checks that non-whitespace characters are separated by no more than one whitespace. Separating characters by tabs or multiple spaces will be reported. Currently the check doesn't permit horizontal alignment. To inspect whitespaces before and after comments, set the property validateComments to true.

Setting validateComments to false will ignore cases like:

int i;  // Multiple whitespaces before comment tokens will be ignored.
private void foo(int  /* whitespaces before and after block-comments will be
ignored */  i) {
        

Sometimes, users like to space similar items on different lines to the same column position for easier reading. This feature isn't supported by this check, so both braces in the following case will be reported as violations.

public long toNanos(long d)  { return d;             } // 2 violations
public long toMicros(long d) { return d / (C1 / C0); }
        

Properties

name description type default value since
validateComments If set to true, whitespaces surrounding comments will be validated. Boolean false 6.19

Examples

To configure the check:

<module name="SingleSpaceSeparator"/>
        

To configure the check so that it validates comments:

<module name="SingleSpaceSeparator">
  <property name="validateComments" value="true"/>
</module>
        

Example of Usage

Error 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

TypecastParenPad

Description

Since Checkstyle 3.2

Checks the policy on the padding of parentheses for typecasts. That is, whether a space is required after a left parenthesis and before a right parenthesis, or such spaces are forbidden.

Properties

name description type default value since
option policy on how to pad parentheses Pad Policy nospace 3.2

Examples

To configure the check:

<module name="TypecastParenPad"/>
        

To configure the check to require spaces:

<module name="TypecastParenPad">
    <property name="option" value="space"/>
</module>
        

Example of Usage

Error 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

WhitespaceAfter

Description

Since Checkstyle 3.0

Checks that a token is followed by whitespace.

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, DO_WHILE. COMMA, SEMI, TYPECAST, LITERAL_IF, LITERAL_ELSE, LITERAL_WHILE, LITERAL_DO, LITERAL_FOR, DO_WHILE. 3.0

Examples

To configure the check:

<module name="WhitespaceAfter"/>
        

To configure the check for whitespace only after COMMA and SEMI tokens:

<module name="WhitespaceAfter">
    <property name="tokens" value="COMMA, SEMI"/>
</module>
        

Example of Usage

Error 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

WhitespaceAround

Description

Since Checkstyle 3.0

Checks that a token is surrounded by whitespace. Empty constructor, method, class, enum, interface, loop bodies (blocks), lambdas of the form

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 and allowEmptyCatches 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).

Properties

name description type default value since
allowEmptyConstructors allow empty constructor bodies Boolean false 4.0
allowEmptyMethods allow empty method bodies Boolean false 4.0
allowEmptyTypes allow empty class, interface and enum bodies Boolean false 5.8
allowEmptyLoops allow empty loop bodies Boolean false 5.8
allowEmptyLambdas allow empty lambda bodies Boolean false 6.14
allowEmptyCatches allow empty catch bodies Boolean false 7.6
ignoreEnhancedForColon ignore whitespace around colon in for-each loops Boolean true 5.5
tokens tokens to check subset of tokens ASSIGN, ARRAY_INIT, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, DO_WHILE, EQUAL, GE, GT, LAMBDA, LAND, LCURLY, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN, LITERAL_ASSERT, TYPE_EXTENSION_AND, WILDCARD_TYPE, GENERIC_START, GENERIC_END, ELLIPSIS. ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, DO_WHILE, EQUAL, GE, GT, LAMBDA, LAND, LCURLY, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN, LITERAL_ASSERT, TYPE_EXTENSION_AND. 3.0

Examples

To configure the check:

<module name="WhitespaceAround"/>
        

To configure the check for whitespace around the assignment operator, =:

<module name="WhitespaceAround">
    <property name="tokens" value="ASSIGN"/>
</module>
        

Error 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