Skip to content

Commit d22318c

Browse files
authored
feat: JSpecify support (#5365)
1 parent 1a8136d commit d22318c

File tree

1 file changed

+77
-7
lines changed

1 file changed

+77
-7
lines changed

docs/topics/jvm/java-interop.md

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,32 @@ Kotlin types. The compiler supports several flavors of nullability annotations,
199199

200200
* [JetBrains](https://www.jetbrains.com/idea/help/nullable-and-notnull-annotations.html)
201201
(`@Nullable` and `@NotNull` from the `org.jetbrains.annotations` package)
202-
* [JSpecify](https://jspecify.dev/) (`org.jspecify.annotations`)
202+
* [JSpecify](#jspecify-support) (`org.jspecify.annotations`)
203203
* Android (`com.android.annotations` and `android.support.annotations`)
204-
* JSR-305 (`javax.annotation`, more details below)
204+
* [JSR-305](#jsr-305-support) (`javax.annotation`)
205205
* FindBugs (`edu.umd.cs.findbugs.annotations`)
206206
* Eclipse (`org.eclipse.jdt.annotation`)
207-
* Lombok (`lombok.NonNull`)
207+
* [Lombok](lombok.md) (`lombok.NonNull`)
208208
* RxJava 3 (`io.reactivex.rxjava3.annotations`)
209209

210-
You can specify whether the compiler reports a nullability mismatch based on the information from specific types of
211-
nullability annotations. Use the compiler option `-Xnullability-annotations=@<package-name>:<report-level>`.
212-
In the argument, specify the fully qualified nullability annotations package and one of these report levels:
210+
You can instruct the compiler to report nullability mismatches for specific nullability annotations with the following compiler option:
211+
212+
```bash
213+
-Xnullability-annotations=@<package-name>:<report-level>
214+
```
215+
216+
Specify the package name for the fully qualified nullability annotations and one of these report levels:
217+
213218
* `ignore` to ignore nullability mismatches
214219
* `warn` to report warnings
215220
* `strict` to report errors.
216221

217-
See the full list of supported nullability annotations in the
222+
> [JSpecify](#jspecify-support) is the only supported flavor that uses `strict` report level by default.
223+
> Use it to report errors on nullability annotations without additional configuration.
224+
>
225+
{style="note"}
226+
227+
See the full list of supported nullability annotations in the
218228
[Kotlin compiler source code](https://github.com/JetBrains/kotlin/blob/master/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt).
219229

220230
### Annotating type arguments and type parameters
@@ -312,6 +322,66 @@ nullability annotations support the `TYPE_USE` target (`org.jetbrains.annotation
312322
>
313323
{style="note"}
314324

325+
### JSpecify support
326+
327+
Kotlin supports the [JSpecify](https://jspecify.dev/) nullability annotations, which provide a unified set of annotations
328+
for Java nullability. JSpecify allows you to provide detailed nullability information for Java declarations,
329+
helping Kotlin maintain null-safety when working with Java code.
330+
331+
Kotlin supports the following annotations in the `org.jspecify.annotations` package:
332+
333+
* `@Nullable` marks a type as nullable.
334+
* `@NonNull` marks a type as non-nullable.
335+
* `@NullMarked` marks all types within a scope, for example a class or package, as non-nullable by default unless annotated
336+
otherwise.
337+
338+
This annotation doesn't apply to local variables and [type variables (generics)](https://jspecify.dev/docs/user-guide/#using-type-variables-in-generic-types).
339+
Type variables remain "null-agnostic" until a specific nullable or non-nullable type is provided.
340+
341+
* `@NullUnmarked` reverses the effect of `@NullMarked`, making all types within the scope as [platform types](#null-safety-and-platform-types).
342+
343+
Consider the following Java class with JSpecify annotations:
344+
345+
```java
346+
// Java
347+
import org.jspecify.annotations.*;
348+
349+
@NullMarked
350+
public class InventoryService {
351+
public String notNull() { return ""; }
352+
public @Nullable String nullable() { return null; }
353+
}
354+
```
355+
356+
In Kotlin, these are treated as regular nullable and non-nullable types rather than [platform types](#null-safety-and-platform-types):
357+
358+
```kotlin
359+
// Kotlin
360+
fun test(inventory: InventoryService) {
361+
inventory.notNull().length // OK
362+
inventory.nullable().length // Error: only safe (?.) or non-null asserted (!!) calls are allowed
363+
}
364+
```
365+
366+
By default, the Kotlin compiler reports nullability mismatches for JSpecify annotations as errors.
367+
You can customize the severity of JSpecify nullability diagnostics using the following compiler option:
368+
369+
```bash
370+
-Xjspecify-annotations=<report-level>
371+
```
372+
373+
Available report levels are:
374+
375+
| Level | Description |
376+
|----------|------------------------------------------------------|
377+
| `strict` | Reports errors for nullability mismatches (default). |
378+
| `warn` | Reports warnings. |
379+
| `ignore` | Ignores nullability mismatches. |
380+
381+
> For more information on JSpecify annotations, see the [JSpecify user guide](https://jspecify.dev/docs/user-guide).
382+
>
383+
{type="tip"}
384+
315385
### JSR-305 support
316386

317387
The [`@Nonnull`](https://www.javadoc.io/doc/com.google.code.findbugs/jsr305/latest/javax/annotation/Nonnull.html) annotation defined

0 commit comments

Comments
 (0)