-
Notifications
You must be signed in to change notification settings - Fork 2
Nullability
pawel_labaj edited this page Aug 2, 2023
·
12 revisions
AutoRecord generates nullability checks for record components by default. This means that all record components will be considered non-null by default, and an appropriate compact constructor will be generated.
If you want to allow a recordComponent to be nullable, you can annotate the corresponding method in the interface with javax.annotation.Nullable
annotation.
Here's an example interface:
@AutoRecord
interface Person {
String name();
int age();
@Nullable String email();
}
Here's the corresponding generated record that demonstrates nullability checking:
@Generated("pl.com.labaj.autorecord.AutoRecord")
@GeneratedWithAutoRecord
record PersonRecord(String name, int age, @Nullable String email) implements Person {
PersonRecord {
requireNonNull(name, "name must not be null");
}
}
A compact constructor with nullability checking is generated. The email()
method is annotated with Nullable
annotation, hence email
recordComponent could be null
.
The age()
method returns a primitve type, so nullability check is not applied for age
recordComponent