When I looked among the standard constraints in Bean Validation API (JSR-303), I found the NotNull.List
annotation. Its description is:
Defines several @NotNull annotations on the same element
This is valid syntax:
@NotNull.List({@NotNull, @NotNull})
private Object myObject;
But it makes no sense. Either the object is null or it is not. When would you use this annotation?
There are several other similar annotations like AssertFalse.List
and AssertTrue.List
.
The @NotNull Annotation is, actually, an explicit contract declaring the following: A method should not return null. A variable (like fields, local variables, and parameters) cannot should not hold null value.
@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.
@NotNull validates that the annotated property value is not null. @AssertTrue validates that the annotated property value is true.
@NotNull : The CharSequence, Collection, Map or Array object is not null, but can be empty. @NotEmpty : The CharSequence, Collection, Map or Array object is not null and size > 0. @NotBlank : The string is not null and the trimmed length is greater than zero.
Hi, when using the @NotNull annotation, IntelliJ will generate bytecode to throw an IllegalArgumentException when a null is passed/returned.
The @NonNull/@Nullable annotation can put in front of functions as well to indicate the return value nullability. return null; // warn: 'null' is returned by the method ... Easy, right? Keep your Billion Dollar in your pocket and enjoy using this @Nullable and @NonNull.
You can have multiple @NotNull annotations that are mutually exclusive based on the group attribute.
@NotNull.List({@NotNull(groups=Foo.class,message="Some message!"),
@NotNull(groups=bar.class, message="Some other message!"})
private Object myObject;
I do agree it's a little silly for this example since only the payload and message can be affected, but it's probably there to remain consistent with the other annotations.
See here 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