Skip to content

Commit 27072cc

Browse files
committed
Improve wording
1 parent 26d413c commit 27072cc

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

framework-docs/modules/ROOT/pages/core/null-safety.adoc

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ https://github.com/uber/NullAway[NullAway] to enforce null-safety at the applica
4242
The purpose of this section is to share some proposed guidelines for explicitly specifying the nullability of
4343
Spring-related libraries or applications.
4444

45-
4645
[[null-safety-guidelines-jspecify]]
4746
=== JSpecify
4847

4948
==== Defaults to non-null
5049

5150
A key point to understand is that the nullness of types is unknown by default in Java and that non-null type
52-
usages are by far more frequent than nullable usages. In order to keep codebases readable, we typically want to define
51+
usage is by far more frequent than nullable usage. In order to keep codebases readable, we typically want to define
5352
by default that type usage is non-null unless marked as nullable for a specific scope. This is exactly the purpose of
5453
https://jspecify.dev/docs/api/org/jspecify/annotations/NullMarked.html[`@NullMarked`] which is typically set in Spring
5554
projects at the package level via a `package-info.java` file, for example:
@@ -64,15 +63,15 @@ import org.jspecify.annotations.NullMarked;
6463

6564
==== Explicit nullability
6665

67-
In `@NullMarked` code, nullable type usages are defined explicitly with
66+
In `@NullMarked` code, nullable type usage is defined explicitly with
6867
https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html[`@Nullable`].
6968

70-
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
7372
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.
7675

7776
For example, for a field:
7877

@@ -109,26 +108,27 @@ the array itself. Pay attention to the syntax
109108
https://docs.oracle.com/javase/specs/jls/se17/html/jls-9.html#jls-9.7.4[defined by the Java specification] which may be
110109
initially surprising. For example, in `@NullMarked` code:
111110

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`.
115114

116115
==== Generics
117116

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:
119118

120119
- `List<String>` means a list of non-null elements (equivalent of `List<@NonNull String>`)
121120
- `List<@Nullable String>` means a list of nullable elements
122121

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
124123
https://jspecify.dev/docs/user-guide/#generics[JSpecify generics documentation] for more details.
125124

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].
127127

128128
==== Nested and fully qualified types
129129

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:
132132

133133
- `Cache.@Nullable ValueWrapper`
134134
- `jakarta.validation.@Nullable Validator`
@@ -153,15 +153,15 @@ parameter cannot be null after a successful invocation of `Assert.notNull()`.
153153

154154
Optionally, it is possible to set `NullAway:JSpecifyMode=true` to enable
155155
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
157157
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
159159
expected baseline). It is recommended to enable the JSpecify mode only as a second step, after making sure the codebase
160160
generates no warning with the recommended configuration mentioned previously in this section.
161161

162162
==== Warnings suppression
163163

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
165165
to suppress related warnings and to document the reason:
166166

167167
- `@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.
176176
- `@SuppressWarnings("NullAway") // Well-known map keys` can be used when `Map#get` invocations are performed with keys that are known
177177
to be present and when non-null related values have been inserted previously.
178178
- `@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).
180180

181181

182182
[[null-safety-migrating]]
@@ -194,7 +194,7 @@ and the capability to specify nullability more precisely for more use cases.
194194

195195
A key difference is that Spring's deprecated null-safety annotations, which follow JSR 305 semantics, apply to fields,
196196
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
198198
nullness of arrays/varargs as well as to define the nullness of generic types.
199199

200200
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.
209209
`public @Nullable String method()` with JSpecify annotations.
210210

211211
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).
214214

0 commit comments

Comments
 (0)