Macros
swift4j provides three macros, all in the Swift4j module.
Marks a Swift class, struct, or enum for export to Java and Kotlin.
@jvmclass MyService { ... }
@jvmstruct Config { ... }
@jvmenum Status { case active, inactive }When applied, the macro:
- Adds JNI registration code that runs when the native library is loaded
- Generates native C entry points for each exported member
- Adds conformance to the internal
JObjectConvertibleprotocol used by the JNI bridging layer - Emits a compiler warning for any member that cannot be exported (unsupported signature, unexportable parameter type, etc.)
A member is exportable if it is non-private and all types in its signature are either built-in JNI-compatible types or other @jvm-annotated types.
@nonjvm
Section titled “@nonjvm”Excludes a specific member from JVM export. Use it to suppress the compiler warning that @jvm emits for unexportable members.
@jvmclass Parser { func parse(input: String) -> [Token] { ... } // exported
@nonjvm func parseInternal(buffer: UnsafeBufferPointer<UInt8>) { ... } // excluded, no warning}@nonjvm has no runtime effect — it is a marker that tells the macro system to skip the annotated member.
@jvm_exported
Section titled “@jvm_exported”Applied automatically by @jvm to exported properties. You do not apply this macro manually.
It generates the property getter (and setter for var properties) as separate JNI-registered functions so that the JVM proxy can access them individually.