Migrating Java nullness annotations to JSpecify
Move from JSR 305 annotations to JSpecify, model nullable generics and arrays, and verify how Spring Framework 7 and Kotlin interpret the new contracts.
I use nullness annotations to make the API’s Java and Kotlin types agree. JSpecify makes it possible to describe a nullable collection separately from a collection containing nullable elements.
The examples target JSpecify 1.0, Spring Framework 7 or Spring Boot 4, and Kotlin 2.1 or later.
Where JSR 305 falls short
JSR 305 attempted to standardize annotations for defect detection. It became dormant, and users rely on interpretations from individual tools. It predates Java 8 type-use annotations, so it does not give modern tools a consistent way to express qualifiers on generic arguments or array components.
A separate migration risk appears on the module path. Some JSR 305 artifacts put types under javax.annotation. Other modules may own that package. JPMS cannot resolve a named module that reads conflicting exports of the same package. This is a module-path problem that may stay hidden while a classpath build runs.
flowchart TD
OLD["JSR 305 annotations"] --> LIMIT["Different tool interpretations"]
LIMIT --> LEGACY["Legacy classpath contracts"]
LIMIT --> MOD["Potential module-path package conflict"]
MOD --> MODERN["JSpecify type-use contracts"]
What the four JSpecify annotations mean
| Annotation | Meaning |
|---|---|
@NullMarked | Establishes a default nullness contract for types in its scope |
@Nullable | Allows null at the annotated type use |
@NonNull | Expresses an explicitly non-null type use |
@NullUnmarked | Clears the marked defaults for an unmigrated scope |
A List<@Nullable String> and a @Nullable List<String> describe different locations for null. In a marked scope, the first list reference is non-null and its elements may be null. The second list reference can be null and its string elements are non-null.
1
2
3
// Mark the collection and its type arguments separately.
List<@Nullable String> listWithNullableElements; // Non-null list, nullable elements
@Nullable List<String> nullableList; // Nullable list, non-null elements
Nullness defaults also interact with type variables and bounds. Inspect those contracts explicitly when publishing your own generic interfaces. A blanket non-null assertion in a Kotlin caller can conceal a mismatch until runtime.
Marking a Java package and calling it from Kotlin
Declare the dependency and apply @NullMarked in package-info.java. This gives the interfaces in that package a consistent starting point. The repository marks only values that can actually be absent.
1
2
3
4
5
// build.gradle.kts
dependencies {
// Expose JSpecify to compilation and static analysis.
implementation("org.jspecify:jspecify:1.0.0")
}
1
2
3
4
5
// src/main/java/com/example/member/package-info.java
@NullMarked
package com.example.member;
import org.jspecify.annotations.NullMarked;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// src/main/java/com/example/member/MemberRepository.java
package com.example.member;
import java.util.List;
import org.jspecify.annotations.Nullable;
public interface MemberRepository {
// The marked scope makes the ID and result non-null by default.
Member getById(String id);
// Mark a lookup result nullable when it may be absent.
@Nullable Member findByEmail(String email);
// The list is non-null and individual nicknames can be null.
List<@Nullable String> findNicknamesByTeam(String teamId);
}
Kotlin reads the JSpecify annotations at the Java boundary. A nullable repository result can become a Kotlin nullable type, while nullable elements become visible in the Kotlin collection type. Kotlin 2.1 uses strict reporting by default for JSpecify. Check the compiler settings if a mixed-version project reports a warning or accepts a call that you expect it to reject.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// src/main/kotlin/com/example/member/MemberService.kt
package com.example.member
class MemberService(
private val memberRepository: MemberRepository
) {
fun resolveDisplayName(email: String): String {
// Kotlin infers Member? from the Java lookup.
val member: Member? = memberRepository.findByEmail(email)
return member?.name ?: "Guest"
}
fun collectValidNicknames(teamId: String): List<String> {
// Kotlin infers List<String?>, then filterNotNull() removes null entries.
val nicknames: List<String?> = memberRepository.findNicknamesByTeam(teamId)
return nicknames.filterNotNull()
}
}
Array annotations and gradual migration
Java arrays have a type use for the array reference and another for its elements. The position next to the brackets describes the corresponding array dimension. The annotation before the element type describes the elements.
1
2
3
4
5
6
7
8
// 1) The array is non-null and its String elements can be null.
@Nullable String[] nullableElements;
// 2) The array reference can be null and its elements are non-null.
String @Nullable [] nullableArray;
// 3) Both the array and its elements can be null.
@Nullable String @Nullable [] bothNullable;
Annotations alone do not add runtime null checks. In a Java project, connect a checker such as NullAway or Error Prone where it can enforce the contract in CI. I would migrate one package with clear boundaries first, leave uncertain legacy scopes unmarked, and add negative compile checks for important cross-language APIs. Keep older annotations only where a consumer still needs them and avoid introducing another conflicting annotation artifact onto the module path.