GenericWhitespace

Since Checkstyle 5.0

Description

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

Whitespace is defined by implementation of java.lang.Character.isWhitespace(char)

Left angle bracket ("<"):

  • should be preceded with whitespace only in generic methods definitions.
  • should not be preceded with whitespace when it is preceded method name or constructor.
  • should not be preceded with whitespace when 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 a method name, constructor, or record header.

Examples

To configure the check:


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

Examples with correct spacing:


class Example1 {
  List<String> l;
  public <T> void foo() {}
  List a = new ArrayList<>();
  Map<Integer, String> m;
  HashSet<Integer> set;
  record License<T>() {}
}

Examples with incorrect spacing:


class Example2 {
  List <String> l; // violation ''<' is preceded with whitespace.'
  public<T> void foo() {} // violation ''<' is not preceded with whitespace.'
  List a = new ArrayList<> (); // violation ''>' is followed by whitespace.'
  Map<Integer, String>m; // violation ''>' is followed by an illegal character.'
  HashSet<Integer > set; // violation ''>' is preceded with whitespace.'
  record License<T> () {} // violation ''>' is followed by 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.GenericWhitespaceCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker