GenericWhitespace

Description

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

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
        

Properties

None.

Examples

To configure the check:

<module name="GenericWhitespace"/>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

EmptyForInitializerPad

Description

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
option policy on how to pad an empty for iterator pad policy nospace

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

EmptyForIteratorPad

Description

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
option policy on how to pad an empty for iterator pad policy nospace

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

MethodParamPad

Description

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
allowLineBreaks whether a line break between the identifier and left parenthesis is allowed boolean false
option policy on how to pad method parameter pad policy nospace
tokens tokens to check subset of tokens CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

NoWhitespaceAfter

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.

Properties

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

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

NoWhitespaceBefore

Description

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.

Properties

name description type default value
allowLineBreaks whether whitespace is allowed if the token is at a linebreak boolean false
tokens tokens to check subset of tokens SEMI, DOT, POST_DEC, POST_INC SEMI, POST_DEC, POST_INC

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

OperatorWrap

Description

Checks the policy on how to wrap lines on operators.

Properties

name description type default value
option policy on how to wrap lines wrap operator policy nl
tokens tokens to check subset of tokens ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, SL, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN BAND, BOR, BSR, BXOR, COLON, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR (all tokens except assignment operators)

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

ParenPad

Description

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, with the exception that it does not check for padding of the right parenthesis at an empty for iterator and empty for initializer. 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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

TypecastParenPad

Description

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
option policy on how to pad parentheses pad policy nospace
tokens tokens to check Must have tokens TYPECAST, RPAREN TYPECAST, RPAREN

Examples

To configure the check:

<module name="TypecastParenPad"/>
        

To configure the check to require spaces:

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

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

FileTabCharacter

Description

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
eachLine whether to report on each line containing a tab, or just the first instance boolean false
fileExtensions file type extension of files to process String Set {}

Example

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

Checker

WhitespaceAfter

Description

Checks that a token is followed by whitespace.

Properties

name description type default value
tokens tokens to check subset of tokens COMMA, SEMI, TYPECAST COMMA, SEMI, TYPECAST

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

WhitespaceAround

Description

Checks that a token is surrounded by whitespace. Empty constructor, method, class, enum, interface, loop bodies (blocks) 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
public @interface Beta {} // empty annotation type
        

may optionally be exempted from the policy using the allowEmptyMethods, allowEmptyConstructors , allowEmptyTypes and allowEmptyLoops properties.

Properties

name description type default value
tokens tokens to check subset of tokens ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, DO_WHILE, EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_ASSERT, 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, TYPE_EXTENSION_AND all tokens
allowEmptyConstructors allow empty constructor bodies boolean false
allowEmptyMethods allow empty method bodies boolean false
allowEmptyTypes allow empty class, interface and enum bodies boolean false
allowEmptyLoops allow empty loop bodies boolean false
ignoreEnhancedForColon ignore whitespace around colon in for-each loops boolean true

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

NoLineWrap

Description

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
tokens assignments to check subset of tokens PACKAGE_DEF, IMPORT, CLASS_DEF, ENUM_DEF INTERFACE_DEF, CTOR_DEF, METHOD_DEF, PACKAGE_DEF, IMPORT

Examples

Examples of line-wrapped statements (bad case):

package com.puppycrawl.
    tools.checkstyle.checks;

import com.puppycrawl.tools.
    checkstyle.api.Check;
      

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.Check;
      

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

EmptyLineSeparator

Description

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

Properties

name description type default value
tokens assignments to check subset of tokens PACKAGE_DEF, IMPORT, CLASS_DEF, ENUM_DEF INTERFACE_DEF, CTOR_DEF, METHOD_DEF, STATIC_INIT, INSTANCE_INIT, VARIABLE_DEF All subset of tokens
allowNoEmptyLineBetweenFields Allow no empty line between fields boolean false

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>
      

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker

SeparatorWrap

Description

Checks line wrapping with separators.

Properties

name description type default value
option policy on how to wrap lines wrap operator policy eol
tokens tokens to check subset of tokens DOT, COMMA, SEMI, ELLIPSIS, AT, LPAREN, RPAREN, ARRAY_DECLARATOR, RBRACK DOT, COMMA

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);
        

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>
        

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker