You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -312,6 +322,66 @@ nullability annotations support the `TYPE_USE` target (`org.jetbrains.annotation
312
322
>
313
323
{style="note"}
314
324
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
+
importorg.jspecify.annotations.*;
348
+
349
+
@NullMarked
350
+
publicclassInventoryService {
351
+
publicStringnotNull() { return""; }
352
+
public @NullableStringnullable() { returnnull; }
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
+
funtest(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:
0 commit comments