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:
class Example1{
public record MyRecord1(int x, int y, String str) {}
public record MyRecord2(int x, int y, double d,
String str, char c, float f) {}
record MyRecord3(int x, int y, int z, double d, // violation, 9 components
String str1, String str2, char c, float f, String location) {}
private record MyRecord4(int x, int y,
String str, double d) {}
}
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:
class Example2{
public record MyRecord1(int x, int y, String str) {}
public record MyRecord2(int x, int y, double d, // violation, 6 components
String str, char c, float f) {}
record MyRecord3(int x, int y, int z, double d, // violation, 9 components
String str1, String str2, char c, float f, String location) {}
private record MyRecord4(int x, int y,
String str, double d) {}
}
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="5"/>
<property name="accessModifiers" value="public"/>
</module>
</module>
</module>
Java code example:
class Example3 {
public record MyRecord1(int x, int y, String str) {}
public record MyRecord2(int x, int y, double d, // violation, 6 components
String str, char c, float f) {}
record MyRecord3(int x, int y, int z, double d,
String str1, String str2, char c, float f, String location) {}
private record MyRecord4(int x, int y, // violation, 4 components
String str, double d) {}
}
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