Skip to content

Commit fd13a44

Browse files
committed
Improved documentation for nullity annotations
1 parent fd51131 commit fd13a44

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

src/jvmMain/java/org/jetbrains/annotations/NotNull.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,22 @@
2121
/**
2222
* An element annotated with NotNull claims {@code null} value is <em>forbidden</em>
2323
* to return (for methods), pass to (parameters) and hold (local variables and fields).
24+
* <p>
2425
* Apart from documentation purposes this annotation is intended to be used by static analysis tools
2526
* to validate against probable runtime errors and element contract violations.
27+
* <p>
28+
* If a field is annotated as {@code @NotNull} it's expected to be initialized during object construction.
29+
* Tools may issue a warning if it's not the case.
30+
* <p>
31+
* If a method overrides a superclass method, and the superclass method specifies the nullability on parameter
32+
* or return type, then the subclass method should specify the same nullability, either directly or indirectly
33+
* via {@link @NotNullByDefault}. The only exception is the covariant return type nullability: if the superclass
34+
* method is declared to return nullable value, then subclass may declare to return a not-null value.
35+
* <p>
36+
* The tools may issue a warning if the nullability for a subclass method contradicts from the specified nullability
37+
* of a superclass method.
38+
*
39+
* @see Nullable
2640
*/
2741
@Documented
2842
@Retention(RetentionPolicy.CLASS)
@@ -36,7 +50,7 @@
3650
/**
3751
* @return Custom exception type that should be thrown when not-nullity contract is violated.
3852
* The exception class should have a constructor with one String argument (message).
39-
*
53+
* <p>
4054
* By default, {@link IllegalArgumentException} is thrown on null method arguments and
4155
* {@link IllegalStateException} &mdash; on null return value.
4256
*/

src/jvmMain/java/org/jetbrains/annotations/NotNullByDefault.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
* Recursively not-null means that along with types themselves, the components of array types, the type arguments
1414
* of generic types and the upper bounds of wildcard types are also not-null.
1515
* <p>
16-
* The annotation has no effect on overridden method parameters and return values. In this case, the annotations
17-
* declared on the original method in the superclass or interface take place.
16+
* If a method overrides a superclass method, and the superclass method specifies the nullability on parameter
17+
* or return type, then the subclass method should specify the same nullability, either directly or indirectly
18+
* via {@code @NotNullByDefault}. The only exception is the covariant return type nullability: if the superclass
19+
* method is declared to return nullable value, then subclass may declare to return a not-null value.
20+
* <p>
21+
* The tools may issue a warning if the nullability for a subclass method contradicts from the specified nullability
22+
* of a superclass method.
1823
* <p>
1924
* The annotation has no effect on newly declared type parameters and their bounds. Only instantiations of
2025
* type parameters are constrained.
2126
* <p>
2227
* The annotation has no effect on local variables.
2328
*
2429
* @see NotNull
25-
* @since 25.0.0
30+
* @since 26.0.0
2631
*/
2732
@Documented
2833
@Retention(RetentionPolicy.CLASS)

src/jvmMain/java/org/jetbrains/annotations/Nullable.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,31 @@
1919
import java.lang.annotation.*;
2020

2121
/**
22-
* An element annotated with {@link Nullable} claims {@code null} value is perfectly <em>valid</em>
22+
* An element annotated with {@code Nullable} claims {@code null} value is perfectly <em>valid</em>
2323
* to return (for methods), pass to (parameters) or hold in (local variables and fields).
2424
* Apart from documentation purposes this annotation is intended to be used by static analysis tools
2525
* to validate against probable runtime errors or element contract violations.
2626
* <p>
2727
* By convention, this annotation applied only when the value should <em>always</em> be checked against {@code null}
2828
* because the developer could do nothing to prevent null from happening.
29-
* Otherwise, too eager {@link Nullable} usage could lead to too many false positives from static analysis tools.
29+
* Otherwise, too eager {@code Nullable} usage could lead to too many false positives from static analysis tools.
3030
* <p>
31-
* For example, {@link java.util.Map#get(Object key)} should <em>not</em> be annotated {@link Nullable} because
31+
* For example, {@link java.util.Map#get(Object key)} should <em>not</em> be annotated {@code Nullable} because
3232
* someone may have put not-null value in the map by this key and is expecting to find this value there ever since.
33+
* It could be annotated as {@link UnknownNullability} or left unannotated.
3334
* <p>
34-
* On the other hand, the {@link java.lang.ref.Reference#get()} should be annotated {@link Nullable} because
35+
* On the other hand, the {@link java.lang.ref.Reference#get()} should be annotated {@code Nullable} because
3536
* it returns {@code null} if object got collected which can happen at any time completely unexpectedly.
37+
* <p>
38+
* If a method overrides a superclass method, and the superclass method specifies the nullability on parameter
39+
* or return type, then the subclass method should specify the same nullability, either directly or indirectly
40+
* via {@link @NotNullByDefault}. The only exception is the covariant return type nullability: if the superclass
41+
* method is declared to return nullable value, then subclass may declare to return a not-null value.
42+
* <p>
43+
* The tools may issue a warning if the nullability for a subclass method contradicts from the specified nullability
44+
* of a superclass method.
45+
*
46+
* @see NotNull
3647
*/
3748
@Documented
3849
@Retention(RetentionPolicy.CLASS)

src/jvmMain/java/org/jetbrains/annotations/UnknownNullability.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
/**
26-
* An element annotated with {@link UnknownNullability} claims that no specific nullability
26+
* An element annotated with {@code UnknownNullability} claims that no specific nullability
2727
* should be assumed by static analyzer. The unconditional dereference of the annotated value
2828
* should not trigger a static analysis warning by default (though static analysis tool may have
2929
* an option to perform stricter analysis and issue warnings for {@code @UnknownNullability} as well).
@@ -34,7 +34,9 @@
3434
* The {@code UnknownNullability} is the default nullability for unannotated methods, so it's
3535
* rarely necessary. An explicit annotation may serve to document the method behavior and also
3636
* to override automatic annotation inference result that could be implemented in some static
37-
* analysis tools.
37+
* analysis tools. Finally, it can override the effect of {@link NotNullByDefault}.
38+
*
39+
* @since 21.0.0
3840
*/
3941
@Documented
4042
@Retention(RetentionPolicy.CLASS)

0 commit comments

Comments
 (0)