@ -6,32 +6,34 @@ annotated with https://jspecify.dev/docs/start-here/[JSpecify] annotations to de
@@ -6,32 +6,34 @@ annotated with https://jspecify.dev/docs/start-here/[JSpecify] annotations to de
fields, and related type usages. Reading the https://jspecify.dev/docs/user-guide/[JSpecify user guide] is highly
recommended in order to get familiar with those annotations and semantics.
The primary goal of this null-safety arrangement is to prevent a `NullPointerException` from being thrown at runtime via build
time checks and to use explicit nullability as a way to express the possible absence of value. It is useful in both
Java by leveraging some tooling (https://github.com/uber/NullAway[NullAway] or IDEs supporting JSpecify annotations
such as IntelliJ IDEA) and Kotlin where JSpecify annotations are automatically translated to
The primary goal of this null-safety arrangement is to prevent a `NullPointerException` from being thrown at
runtime via build time checks and to use explicit nullability as a way to express the possible absence of value.
It is useful in both Java by leveraging some tooling (https://github.com/uber/NullAway[NullAway] or IDEs supporting
JSpecify annotations such as IntelliJ IDEA) and Kotlin where JSpecify annotations are automatically translated to
@ -133,6 +135,7 @@ The Java specification also enforces that annotations defined with `@Target(Elem
@@ -133,6 +135,7 @@ The Java specification also enforces that annotations defined with `@Target(Elem
- `Cache.@Nullable ValueWrapper`
- `jakarta.validation.@Nullable Validator`
[[null-safety-guidelines-nullaway]]
=== NullAway
@ -146,9 +149,9 @@ The recommended configuration is:
@@ -146,9 +149,9 @@ The recommended configuration is:
can be used to express complementary semantics to avoid irrelevant warnings in your codebase.
A good example of the benefits of a `@Contract` declaration can be seen with
{spring-framework-api}/util/Assert.html#notNull(java.lang.Object,java.lang.String)[`Assert.notNull()`] which is annotated
with `@Contract("null, _ -> fail")`. With that contract declaration, NullAway will understand that the value passed as a
parameter cannot be null after a successful invocation of `Assert.notNull()`.
which is annotated with `@Contract("null, _ -> fail")`. With that contract declaration, NullAway will understand
that the value passed as a parameter cannot be null after a successful invocation of `Assert.notNull()`.
Optionally, it is possible to set `NullAway:JSpecifyMode=true` to enable
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
@@ -160,8 +163,8 @@ generates no warning with the recommended configuration mentioned previously in
==== Warnings suppression
There are a few valid use cases where NullAway will incorrectly detect nullability problems. In such cases, it is recommended
to suppress related warnings and to document the reason:
There are a few valid use cases where NullAway will incorrectly detect nullability problems. In such cases,
it is recommended to suppress related warnings and to document the reason:
- `@SuppressWarnings("NullAway.Init")` at field, constructor, or class level can be used to avoid unnecessary warnings
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.
@@ -172,8 +175,8 @@ able to detect that the path involving a nullability problem will never happen.
outside of a lambda for the code path within the lambda.
- `@SuppressWarnings("NullAway") // Reflection` can be used for some reflection operations that are known to return
non-null values even if that cannot be expressed by the API.
- `@SuppressWarnings("NullAway") // Well-known map keys` can be used when `Map#get` invocations are performed with keys that are known
to be present and when non-null related values have been inserted previously.
- `@SuppressWarnings("NullAway") // Well-known map keys` can be used when `Map#get` invocations are performed with keys
that are known to be present and when non-null related values have been inserted previously.
- `@SuppressWarnings("NullAway") // Overridden method does not define nullability` can be used when the superclass does
not define nullability (typically when the superclass comes from an external dependency).
- `@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.
@@ -208,7 +211,6 @@ with JSpecify annotations.
- For method return types, instead of `@Nullable public String method()` with Spring annotations, use
`public @Nullable String method()` with JSpecify annotations.
Also, with JSpecify, you do not need to specify `@NonNull` when overriding a type usage annotated with `@Nullable` in the
super method to "undo" the nullable declaration in null-marked code. Just declare it unannotated, and the null-marked
defaults will apply (type usage is considered non-null unless explicitly annotated as nullable).
Also, with JSpecify, you do not need to specify `@NonNull` when overriding a type usage annotated with `@Nullable`
in the super method to "undo" the nullable declaration in null-marked code. Just declare it unannotated, and the
null-marked defaults will apply (type usage is considered non-null unless explicitly annotated as nullable).