|
19 | 19 | import java.lang.reflect.Parameter;
|
20 | 20 |
|
21 | 21 | import org.springframework.core.MethodParameter;
|
| 22 | +import org.springframework.lang.NonNullApi; |
| 23 | +import org.springframework.lang.Nullable; |
22 | 24 |
|
23 | 25 | /**
|
24 | 26 | * Provides access to nullability declarations of methods and parameters, usually obtained from a source such as a
|
|
33 | 35 | * contains methods, an enclosing class contains inner classes, a package contains classes) to inherit nullability rules
|
34 | 36 | * if the particular method or parameter does not declare nullability rules.
|
35 | 37 | * <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 | + * @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 | + * @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> |
36 | 78 | * A component might be interested on whether nullability is declared and if so, whether the particular element is
|
37 | 79 | * nullable or non-null.
|
38 | 80 | * <p>
|
|
54 | 96 | * declarations, for example to validate input or output.</b>
|
55 | 97 | *
|
56 | 98 | * @author Mark Paluch
|
| 99 | + * @see NonNullApi |
| 100 | + * @see Nullable |
57 | 101 | */
|
58 | 102 | public interface Nullability {
|
59 | 103 |
|
|
0 commit comments