Repository navigation
JNI: nullableAnnotation and nonNullAnnotation for Kotlin types - #24
Open
piercifani wants to merge 2 commits into
Open
piercifani wants to merge 2 commits into
piercifani wants to merge 2 commits into
Conversation
Kotlin sees the parameters and results of generated Java methods as platform types (`String!`): it neither requires null checks nor infers nullable types. Two new options name a Java annotation for each case, such as `androidx.annotation.Nullable`/`NonNull` or JetBrains' `Nullable`/`NotNull`. jextract adds them to a declaration's own parameters and results after every other conversion: `nullableAnnotation` on optionals with `optionalsAsNullable`, `nonNullAnnotation` on every other reference type (strings, jextracted types, `Optional`, futures, arrays). Primitives aren't annotated. The annotations are imported by name, ahead of SwiftKitCore's own `annotations.*`. swift-java takes no dependency: the app supplies the annotation, and must pick one applicable to methods and parameters. Type-use only annotations, such as JSpecify's, don't fit before the qualified types jextract prints, so they aren't supported. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack 20/21 · base:
jni/19-enum-swift-case-names· previous: #23 · next: #25 (jni/21-error-values)Problem
Kotlin sees the parameters and results of jextract's Java methods as platform types, such as
String!. Even withoptionalsAsNullable(#19),Quotes.last("a")was aQuote!, not aQuote?. Kotlin neither required the null check for an optional nor knew a non-optional was nevernull.Changes
Two new options name the Java annotation for each case:
nullableAnnotationmarks optionals whenoptionalsAsNullableis on.nonNullAnnotationmarks every other reference type: strings, jextracted types,Optional, futures and arrays.Examples are
androidx.annotation.NullableandNonNull, or JetBrains'NullableandNotNull.SwiftAsyncStreamare non-null. Primitives aren't annotated.org.swift.swiftkit.core.annotations.*.androidx.annotation, and Kotlin's stdlib brings JetBrains' annotations.java.lang.String), so they aren't supported.Kotlin in this PR
The Kotlin sample turns both options on with JetBrains' annotations, so every Kotlin test in it now compiles against
Quote?andStringrather than platform types.NullablesTest.kt:Why Skip needs it
Skip's Kotlin has real nullable and non-null types. With annotations, Kotlin code calling jextracted APIs gets the same checks it gets calling Skip-generated Kotlin.
Testing
./gradlew :Samples:SwiftJavaExtractJNIKotlinSampleApp:testpasses (47 tests), all compiled against the annotated API.JNINullabilityAnnotationsTests(3 tests):Bool?result;Optionals annotated non-null withoutoptionalsAsNullable;swift test --disable-experimental-prebuilts --disable-sandbox(690 tests),./gradlew :Samples:SwiftJavaExtractJNISampleApp:test(302 tests) and./gradlew :SwiftKitCore:testpass.swift run generate-config-docs --check;swift format formatleaves changed files unchanged;git diff --check.Not in this PR
🤖 Generated with Claude Code