Skip to content

Commit 22a2332

Browse files
committed
Polishing
1 parent e9c402c commit 22a2332

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

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

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,34 @@ annotated with https://jspecify.dev/docs/start-here/[JSpecify] annotations to de
66
fields, and related type usages. Reading the https://jspecify.dev/docs/user-guide/[JSpecify user guide] is highly
77
recommended in order to get familiar with those annotations and semantics.
88

9-
The primary goal of this null-safety arrangement is to prevent a `NullPointerException` from being thrown at runtime via build
10-
time checks and to use explicit nullability as a way to express the possible absence of value. It is useful in both
11-
Java by leveraging some tooling (https://github.com/uber/NullAway[NullAway] or IDEs supporting JSpecify annotations
12-
such as IntelliJ IDEA) and Kotlin where JSpecify annotations are automatically translated to
9+
The primary goal of this null-safety arrangement is to prevent a `NullPointerException` from being thrown at
10+
runtime via build time checks and to use explicit nullability as a way to express the possible absence of value.
11+
It is useful in both Java by leveraging some tooling (https://github.com/uber/NullAway[NullAway] or IDEs supporting
12+
JSpecify annotations such as IntelliJ IDEA) and Kotlin where JSpecify annotations are automatically translated to
1313
{kotlin-docs}/null-safety.html[Kotlin's null safety].
1414

15-
The {spring-framework-api}/core/Nullness.html[`Nullness` Spring API] can be used at runtime to detect the nullness of a
16-
type usage, a field, a method return type, or a parameter. It provides full support for JSpecify annotations,
17-
Kotlin null safety, and Java primitive types, as well as a pragmatic check on any `@Nullable` annotation (regardless of the
18-
package).
15+
The {spring-framework-api}/core/Nullness.html[`Nullness` Spring API] can be used at runtime to detect the
16+
nullness of a type usage, a field, a method return type, or a parameter. It provides full support for
17+
JSpecify annotations, Kotlin null safety, and Java primitive types, as well as a pragmatic check on any
18+
`@Nullable` annotation (regardless of the package).
19+
1920

2021
[[null-safety-libraries]]
2122
== Annotating libraries with JSpecify annotations
2223

23-
As of Spring Framework 7, the Spring Framework codebase leverages JSpecify annotations to expose null-safe APIs and
24-
to check the consistency of those nullability declarations with https://github.com/uber/NullAway[NullAway] as part of
25-
its build. It is recommended for each library depending on Spring Framework and Spring portfolio projects, as
26-
well as other libraries related to the Spring ecosystem (Reactor, Micrometer, and Spring community projects), to do the
27-
same.
24+
As of Spring Framework 7, the Spring Framework codebase leverages JSpecify annotations to expose null-safe APIs
25+
and to check the consistency of those nullability declarations with https://github.com/uber/NullAway[NullAway]
26+
as part of its build. It is recommended for each library depending on Spring Framework and Spring portfolio projects,
27+
as well as other libraries related to the Spring ecosystem (Reactor, Micrometer, and Spring community projects),
28+
to do the same.
29+
2830

2931
[[null-safety-applications]]
3032
== Leveraging JSpecify annotations in Spring applications
3133

32-
Developing applications with IDEs that support nullness annotations will provide warnings in Java and errors in Kotlin
33-
when the nullability contracts are not honored, allowing Spring application developers to refine their null handling to
34-
prevent a `NullPointerException` from being thrown at runtime.
34+
Developing applications with IDEs that support nullness annotations will provide warnings in Java and errors in
35+
Kotlin when the nullability contracts are not honored, allowing Spring application developers to refine their
36+
null handling to prevent a `NullPointerException` from being thrown at runtime.
3537

3638
Optionally, Spring application developers can annotate their codebase and use build plugins like
3739
https://github.com/uber/NullAway[NullAway] to enforce null-safety at the application level during build time.
@@ -47,11 +49,11 @@ Spring-related libraries or applications.
4749

4850
==== Defaults to non-null
4951

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

5658
[source,java,subs="verbatim,quotes",chomp="-packages",fold="none"]
5759
----
@@ -133,6 +135,7 @@ The Java specification also enforces that annotations defined with `@Target(Elem
133135
- `Cache.@Nullable ValueWrapper`
134136
- `jakarta.validation.@Nullable Validator`
135137

138+
136139
[[null-safety-guidelines-nullaway]]
137140
=== NullAway
138141

@@ -146,9 +149,9 @@ The recommended configuration is:
146149
can be used to express complementary semantics to avoid irrelevant warnings in your codebase.
147150

148151
A good example of the benefits of a `@Contract` declaration can be seen with
149-
{spring-framework-api}/util/Assert.html#notNull(java.lang.Object,java.lang.String)[`Assert.notNull()`] which is annotated
150-
with `@Contract("null, _ -> fail")`. With that contract declaration, NullAway will understand that the value passed as a
151-
parameter cannot be null after a successful invocation of `Assert.notNull()`.
152+
{spring-framework-api}/util/Assert.html#notNull(java.lang.Object,java.lang.String)[`Assert.notNull()`]
153+
which is annotated with `@Contract("null, _ -> fail")`. With that contract declaration, NullAway will understand
154+
that the value passed as a parameter cannot be null after a successful invocation of `Assert.notNull()`.
152155

153156
Optionally, it is possible to set `NullAway:JSpecifyMode=true` to enable
154157
https://github.com/uber/NullAway/wiki/JSpecify-Support[checks on the full JSpecify semantics], including annotations on
@@ -160,8 +163,8 @@ generates no warning with the recommended configuration mentioned previously in
160163

161164
==== Warnings suppression
162165

163-
There are a few valid use cases where NullAway will incorrectly detect nullability problems. In such cases, it is recommended
164-
to suppress related warnings and to document the reason:
166+
There are a few valid use cases where NullAway will incorrectly detect nullability problems. In such cases,
167+
it is recommended to suppress related warnings and to document the reason:
165168

166169
- `@SuppressWarnings("NullAway.Init")` at field, constructor, or class level can be used to avoid unnecessary warnings
167170
due to the lazy initialization of fields – for example, due to a class implementing
@@ -172,8 +175,8 @@ able to detect that the path involving a nullability problem will never happen.
172175
outside of a lambda for the code path within the lambda.
173176
- `@SuppressWarnings("NullAway") // Reflection` can be used for some reflection operations that are known to return
174177
non-null values even if that cannot be expressed by the API.
175-
- `@SuppressWarnings("NullAway") // Well-known map keys` can be used when `Map#get` invocations are performed with keys that are known
176-
to be present and when non-null related values have been inserted previously.
178+
- `@SuppressWarnings("NullAway") // Well-known map keys` can be used when `Map#get` invocations are performed with keys
179+
that are known to be present and when non-null related values have been inserted previously.
177180
- `@SuppressWarnings("NullAway") // Overridden method does not define nullability` can be used when the superclass does
178181
not define nullability (typically when the superclass comes from an external dependency).
179182
- `@SuppressWarnings("NullAway") // See https://github.com/uber/NullAway/issues/1075` can be used when NullAway is not able to detect type variable nullness in generic methods.
@@ -208,7 +211,6 @@ with JSpecify annotations.
208211
- For method return types, instead of `@Nullable public String method()` with Spring annotations, use
209212
`public @Nullable String method()` with JSpecify annotations.
210213

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 will apply (type usage is considered non-null unless explicitly annotated as nullable).
214-
214+
Also, with JSpecify, you do not need to specify `@NonNull` when overriding a type usage annotated with `@Nullable`
215+
in the super method to "undo" the nullable declaration in null-marked code. Just declare it unannotated, and the
216+
null-marked defaults will apply (type usage is considered non-null unless explicitly annotated as nullable).

spring-orm/src/main/java/org/springframework/orm/jpa/hibernate/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Hibernate-specific support classes, integrated with JPA.
33
*
44
* <p>Contains Hibernate-specific setup options as an alternative to JPA bootstrapping,
5-
* primarily for use with Hibernate's native {@code sessionFactory.getCurrentSession()}
5+
* primarily for use with Hibernate's native {@code SessionFactory#getCurrentSession()}
66
* but potentially also for JPA repositories or mixed use of native Hibernate and JPA.
77
*
88
* <p>As of Spring Framework 7.0, this package supersedes {@code orm.hibernate5} -

0 commit comments

Comments
 (0)