RecordComponentNumber

Since Checkstyle 8.36

Description

Checks the number of record components in the header of a record definition.

Properties

name description type default value since
accessModifiers Access modifiers of record definitions where the number of record components should be checked. AccessModifierOption[] public, protected, package, private 8.36
max Specify the maximum number of components allowed in the header of a record definition. int 8 8.36

Examples

To configure the check:

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

Java code example:

public record MyRecord1(int x, int y) { // ok, 2 components
   ...
}

record MyRecord2(int x, int y, String str,
                         Node node, Order order, Data data
                         String location, Date date, Image image) { // violation, 9 components
   ...
}
        

To configure the check to allow 5 record components at all access modifier levels for record definitions:

<module name="Checker">
  <module name="TreeWalker">
    <module name="RecordComponentNumber">
      <property name="max" value="5"/>
    </module>
  </module>
</module>
        

Java code example:

public record MyRecord1(int x, int y, String str) { // ok, 3 components
   ...
}

public record MyRecord2(int x, int y, String str,
                         Node node, Order order, Data data) { // violation, 6 components
   ...
}
        

To configure the check to allow 10 record components for a public record definition, but 3 for private record definitions:

<module name="Checker">
  <module name="TreeWalker">
    <module name="RecordComponentNumber">
      <property name="max" value="3"/>
      <property name="accessModifiers" value="private"/>
    </module>
    <module name="RecordComponentNumber">
      <property name="max" value="10"/>
      <property name="accessModifiers" value="public"/>
    </module>
  </module>
</module>
        

Java code example:

public record MyRecord1(int x, int y, String str) { // ok, public record definition allowed 10
   ...
}

private record MyRecord2(int x, int y, String str, Node node) { // violation
   ...                                // private record definition allowed 3 components
}
        

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.sizes

Parent Module

TreeWalker