Skip to content

Commit ea6fbbb

Browse files
committed
Improve documentation.
1 parent c63b236 commit ea6fbbb

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/main/java/org/springframework/data/util/Nullability.java

+44
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.lang.reflect.Parameter;
2020

2121
import org.springframework.core.MethodParameter;
22+
import org.springframework.lang.NonNullApi;
23+
import org.springframework.lang.Nullable;
2224

2325
/**
2426
* Provides access to nullability declarations of methods and parameters, usually obtained from a source such as a
@@ -33,6 +35,46 @@
3335
* contains methods, an enclosing class contains inner classes, a package contains classes) to inherit nullability rules
3436
* if the particular method or parameter does not declare nullability rules.
3537
* <p>
38+
* By default (no annotation use), a package and its types are considered allowing {@literal null} values in return
39+
* values and method parameters. Nullability rules are expressed by annotating a package with annotations such as
40+
* Spring's {@link NonNullApi}. All types of the package inherit the package rule. Subpackages do not inherit
41+
* nullability rules and must be annotated themselves.
42+
*
43+
* <pre class="code">
44+
* &#64;org.springframework.lang.NonNullApi
45+
* package com.example;
46+
* </pre>
47+
*
48+
* {@link Nullable} selectively permits {@literal null} values for method return values or method parameters by
49+
* annotating the method respectively the parameters:
50+
*
51+
* <pre class="code">
52+
* public class ExampleClass {
53+
*
54+
* String shouldNotReturnNull(@Nullable String acceptsNull, String doesNotAcceptNull) {
55+
* // …
56+
* }
57+
*
58+
* &#64;Nullable
59+
* String nullableReturn(String parameter) {
60+
* // …
61+
* }
62+
* }
63+
* </pre>
64+
* <p>
65+
* {@code javax.annotation.Nonnull} is suitable for composition of meta-annotations and expresses via
66+
* {@code javax.annotation.Nonnull#when()} in which cases non-nullability is applicable. Nullability introspection
67+
* considers the following mechanisms:
68+
* <ul>
69+
* <li>Spring's {@link NonNullApi}, {@link Nullable}, and {@link org.springframework.lang.NonNull}.</li>
70+
* <li>JSR-305 {@code javax.annotation.Nonnull} and meta-annotations.</li>
71+
* <li><a href="https://projects.eclipse.org/projects/ee4j.ca">Jakarta Annotations API</a> via
72+
* {@code jakarta.annotation.Nonnull} which is a simplified variant of JSR-305 {@code javax.annotation.Nonnull} without
73+
* {@code when} and {@code @TypeQualifierDefault}.</li>
74+
* <li><a href="https://https://jspecify.dev/">JSpecify</a>, a newly designed specification to opt-in for non-null by
75+
* default through {@code org.jspecify.annotations.NullMarked} and {@code org.jspecify.annotations.Nullable}.</li>
76+
* </ul>
77+
* <p>
3678
* A component might be interested on whether nullability is declared and if so, whether the particular element is
3779
* nullable or non-null.
3880
* <p>
@@ -54,6 +96,8 @@
5496
* declarations, for example to validate input or output.</b>
5597
*
5698
* @author Mark Paluch
99+
* @see NonNullApi
100+
* @see Nullable
57101
*/
58102
public interface Nullability {
59103

0 commit comments

Comments
 (0)