You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A key difference between JSpecify `@Nullable` / `@NonNull` annotations and most other variants is that they are
71
-
meta-annotated with `@Target(ElementType.TYPE_USE)`, so they apply only to type usages. This impacts where such
72
-
annotations should be placed, either to comply with
69
+
A key difference between JSpecify `@Nullable` / `@NonNull` annotations and most other variants is that the JSpecify
70
+
annotations are meta-annotated with `@Target(ElementType.TYPE_USE)`, so they apply only to type usage. This impacts
71
+
where such annotations should be placed, either to comply with
73
72
https://docs.oracle.com/javase/specs/jls/se17/html/jls-9.html#jls-9.7.4[related Java specifications] or to follow code
74
-
style best practices. From a style perspective, it is recommended to embrace the type-use nature of those annotations by placing them on the
75
-
same line than the annotated type.
73
+
style best practices. From a style perspective, it is recommended to embrace the type-use nature of those annotations
74
+
by placing them on the same line as and immediately preceding the annotated type.
76
75
77
76
For example, for a field:
78
77
@@ -109,26 +108,27 @@ the array itself. Pay attention to the syntax
109
108
https://docs.oracle.com/javase/specs/jls/se17/html/jls-9.html#jls-9.7.4[defined by the Java specification] which may be
110
109
initially surprising. For example, in `@NullMarked` code:
111
110
112
-
- `@Nullable Object[] array` means individual elements can be null but the array itself cannot.
113
-
- `Object @Nullable [] array` means individual elements cannot be null but the array itself can.
114
-
- `@Nullable Object @Nullable [] array` means both individual elements and the array can be null.
111
+
- `@Nullable Object[] array` means individual elements can be `null` but the array itself cannot.
112
+
- `Object @Nullable [] array` means individual elements cannot be `null` but the array itself can.
113
+
- `@Nullable Object @Nullable [] array` means both individual elements and the array can be `null`.
115
114
116
115
==== Generics
117
116
118
-
JSpecify annotations applies to generics as well. For example, in `@NullMarked` code:
117
+
JSpecify annotations apply to generics as well. For example, in `@NullMarked` code:
119
118
120
119
- `List<String>` means a list of non-null elements (equivalent of `List<@NonNull String>`)
121
120
- `List<@Nullable String>` means a list of nullable elements
122
121
123
-
Things are a bit more complicated when you are declaring generic types or generic methods, see related
122
+
Things are a bit more complicated when you are declaring generic types or generic methods. See the related
124
123
https://jspecify.dev/docs/user-guide/#generics[JSpecify generics documentation] for more details.
125
124
126
-
WARNING: Generic types and generic methods nullability https://github.com/uber/NullAway/issues?q=is%3Aissue+is%3Aopen+label%3Ajspecify[is not yet fully supported by NullAway].
125
+
WARNING: The nullability of generic types and generic methods
126
+
https://github.com/uber/NullAway/issues?q=is%3Aissue+is%3Aopen+label%3Ajspecify[is not yet fully supported by NullAway].
127
127
128
128
==== Nested and fully qualified types
129
129
130
-
The Java specification also enforces that annotations defined with `@Target(ElementType.TYPE_USE)` like JSpecify
131
-
`@Nullable` should be specified after the last `.` with inner or fully qualified types:
130
+
The Java specification also enforces that annotations defined with `@Target(ElementType.TYPE_USE)` – like JSpecify's
131
+
`@Nullable` annotation – must be declared after the last dot (`.`) within inner or fully qualified type names:
132
132
133
133
- `Cache.@Nullable ValueWrapper`
134
134
- `jakarta.validation.@Nullable Validator`
@@ -153,15 +153,15 @@ parameter cannot be null after a successful invocation of `Assert.notNull()`.
153
153
154
154
Optionally, it is possible to set `NullAway:JSpecifyMode=true` to enable
155
155
https://github.com/uber/NullAway/wiki/JSpecify-Support[checks on the full JSpecify semantics], including annotations on
156
-
arrays, varargs and generics. Be aware that this mode is
156
+
arrays, varargs, and generics. Be aware that this mode is
157
157
https://github.com/uber/NullAway/issues?q=is%3Aissue+is%3Aopen+label%3Ajspecify[still under development] and requires
158
-
using JDK 22 or later (typically combined with the `--release` Java compiler flag to configure the
158
+
JDK 22 or later (typically combined with the `--release` Java compiler flag to configure the
159
159
expected baseline). It is recommended to enable the JSpecify mode only as a second step, after making sure the codebase
160
160
generates no warning with the recommended configuration mentioned previously in this section.
161
161
162
162
==== Warnings suppression
163
163
164
-
There are a few valid use cases where NullAway will incorrectly detect nullability problems. In such case, it is recommended
164
+
There are a few valid use cases where NullAway will incorrectly detect nullability problems. In such cases, it is recommended
165
165
to suppress related warnings and to document the reason:
166
166
167
167
- `@SuppressWarnings("NullAway.Init")` at field, constructor, or class level can be used to avoid unnecessary warnings
@@ -176,7 +176,7 @@ non-null values even if that cannot be expressed by the API.
176
176
- `@SuppressWarnings("NullAway") // Well-known map keys` can be used when `Map#get` invocations are performed with keys that are known
177
177
to be present and when non-null related values have been inserted previously.
178
178
- `@SuppressWarnings("NullAway") // Overridden method does not define nullability` can be used when the superclass does
179
-
not define nullability (typically when the superclass comes from a dependency).
179
+
not define nullability (typically when the superclass comes from an external dependency).
180
180
181
181
182
182
[[null-safety-migrating]]
@@ -194,7 +194,7 @@ and the capability to specify nullability more precisely for more use cases.
194
194
195
195
A key difference is that Spring's deprecated null-safety annotations, which follow JSR 305 semantics, apply to fields,
196
196
parameters, and return values; while JSpecify annotations apply to type usage. This subtle difference
197
-
is in practice pretty significant, as it allows developers to differentiate between the nullness of elements and the
197
+
is pretty significant in practice, since it allows developers to differentiate between the nullness of elements and the
198
198
nullness of arrays/varargs as well as to define the nullness of generic types.
199
199
200
200
That means array and varargs null-safety declarations have to be updated to keep the same semantics. For example
@@ -209,6 +209,6 @@ with JSpecify annotations.
209
209
`public @Nullable String method()` with JSpecify annotations.
210
210
211
211
Also, with JSpecify, you do not need to specify `@NonNull` when overriding a type usage annotated with `@Nullable` in the
212
-
super method to "undo" the nullable declaration in null-marked code. Just declare it unannotated and the null-marked
213
-
defaults (a type usage is considered non-null unless explicitly annotated as nullable) will apply.
212
+
super method to "undo" the nullable declaration in null-marked code. Just declare it unannotated, and the null-marked
213
+
defaults will apply (type usage is considered non-null unless explicitly annotated as nullable).
0 commit comments