I'm using the @AllArgsConstructor
annotation in my class and want to be sure that arguments will be not null.
Is there a way to modify this annotation to make this possible, or I should implement constractor?
Thanks
@Getter
@Setter
@ToString
@AllArgsConstructor
public class Contact {
private String name;
private String phoneNumber;
}
...
<lombok.version>1.16.18</lombok.version>
But if you add a constructor with parameters (or @AllArgsConstructor ), then you'll need to add a no args constructor (or @NoArgsConstructor ) as well, for JPA/Hibernate to work.
An all-args constructor requires one argument for every field in the class. Complete documentation is found at the project lombok features page for @Constructor. Even though it is not listed, this annotation also has the onConstructor parameter.
The @AllArgsConstructor annotation generates a constructor with one parameter for every field in the class. Fields that are annotated with @NonNull result in null checks with the corresponding parameters in the constructor. The annotation won't generate a parameter for the static and initialized final fields.
The constructor will throw a NullPointerException if any of the parameters intended for the fields marked with @NonNull contain null . The order of the parameters match the order in which the fields appear in your class. @AllArgsConstructor generates a constructor with 1 parameter for each field in your class.
Yes, you have to use @ NonNull
on such fields, it will result in a null check in the generated constructor.
@RequiredArgsConstructor
Generates a constructor with required arguments. Required arguments are final fields and fields with constraints such as @NonNull. Complete documentation is found at the project lombok features page for @Constructor . Even though it is not listed, this annotation also has the onConstructor parameter. See the full documentation for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With