From 62ce8669338c2e008593f02bb09ee4b86201f372 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Sat, 9 Aug 2025 09:36:32 -0400 Subject: [PATCH 01/14] update gitgnore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 28dfbae2..b72ffed5 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ bin/ BuildLogic/out/ .index-build .build-vscode +**/.vscode # Ignore gradle build artifacts .gradle @@ -45,3 +46,4 @@ Package.resolved */**/*.swiftdeps */**/*.swiftdeps~ */**/.docc-build/ + From dce871fd2419462888cb154f8159b6358aedb5d6 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Thu, 21 Aug 2025 14:37:13 -0400 Subject: [PATCH 02/14] align with README JExtract from Java to Swift --- .../Documentation.docc/SupportedFeatures.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md index c5de6c45..5160b04b 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md @@ -38,7 +38,7 @@ Java `native` functions. JavaKit simplifies the type conversions ### JExtract: Java -> Swift -SwiftJava's `swift-java jextract` tool automates generating Java bindings from Swift sources. +SwiftJava's `swift-java jextract` tool automates generating Java bindings to Swift sources. > tip: This direction of interoperability is covered in the WWDC2025 session 'Explore Swift and Java interoperability' > around the [14-minute mark](https://youtu.be/QSHO-GUGidA?si=b9YUwAWDWFGzhRXN&t=842). From 0ca4e7e7572411ad9a1b2a2b5608ae546da08c18 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Thu, 21 Aug 2025 14:38:03 -0400 Subject: [PATCH 03/14] add watch to 'you may want to this' --- Sources/SwiftJavaDocumentation/Documentation.docc/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md index 7c695a71..456bbaab 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md @@ -13,7 +13,7 @@ Please refer to articles about the specific direction of interoperability you ar TODO: Some general intro -If you prefer a video introduction, you may want to this +If you prefer a video introduction, you may want to watch this [Explore Swift and Java interoperability](https://www.youtube.com/watch?v=QSHO-GUGidA) WWDC 2025 session, which is a quick overview of all the features and approaches offered by SwiftJava. From 12d5412cb937eea0c4deb33721c9bf627dd9bb81 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Thu, 21 Aug 2025 14:48:47 -0400 Subject: [PATCH 04/14] add intro to SwiftJavaDocumentation index.md --- .../Documentation.docc/index.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md index 456bbaab..71683e33 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md @@ -11,7 +11,21 @@ Please refer to articles about the specific direction of interoperability you ar ### Getting started -TODO: Some general intro +**SwiftJava** provides Java and Swift interoperability with minimal overhead. It eliminates the complex, error-prone process of manually compiling Java classes to C headers for native access. With SwiftJava, developers can achieve a flexible, safe, and high-performance connection between the two languages. + +#### Interoperability promotes +- Incremental adoption of Swift +- Reuse of libraries +- Support for ecosystem-specific build tools + +#### Tools +- Swift Package: + - JavaKit (Java -> Swift) +- Java Library: + - SwiftKit (Swift -> Java) +- Tooling: + - swift-java: command line tool [SwiftJavaCommandLineTool](#SwiftJavaCommandLineTool) + - Build tool integration: SwiftPM Plugin or Gradle [SwiftPMPlugin](#SwiftPMPlugin) If you prefer a video introduction, you may want to watch this [Explore Swift and Java interoperability](https://www.youtube.com/watch?v=QSHO-GUGidA) From 77a818b3778ee5ca6e09abde644716b5353168e5 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Thu, 21 Aug 2025 14:49:54 -0400 Subject: [PATCH 05/14] add intro to SwiftJavaDocumentation index.md --- Sources/SwiftJavaDocumentation/Documentation.docc/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md index 71683e33..695e8caa 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md @@ -24,8 +24,8 @@ Please refer to articles about the specific direction of interoperability you ar - Java Library: - SwiftKit (Swift -> Java) - Tooling: - - swift-java: command line tool [SwiftJavaCommandLineTool](#SwiftJavaCommandLineTool) - - Build tool integration: SwiftPM Plugin or Gradle [SwiftPMPlugin](#SwiftPMPlugin) + - swift-java: command line tool + - Build tool integration: SwiftPM Plugin or Gradle If you prefer a video introduction, you may want to watch this [Explore Swift and Java interoperability](https://www.youtube.com/watch?v=QSHO-GUGidA) From 85babfad3831b9eab523ff87abedef80f8053518 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Thu, 21 Aug 2025 15:00:58 -0400 Subject: [PATCH 06/14] add intro to SwiftJavaDocumentation index.md --- .../Documentation.docc/index.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md index 695e8caa..3acf24a4 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/index.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/index.md @@ -11,14 +11,16 @@ Please refer to articles about the specific direction of interoperability you ar ### Getting started -**SwiftJava** provides Java and Swift interoperability with minimal overhead. It eliminates the complex, error-prone process of manually compiling Java classes to C headers for native access. With SwiftJava, developers can achieve a flexible, safe, and high-performance connection between the two languages. +**SwiftJava** provides Java and Swift interoperability with minimal overhead. It eliminates the complex, error-prone process such as manually compiling Java classes to C headers for native access. With SwiftJava, developers can achieve a flexible, safe, and high-performance connection between the two languages. -#### Interoperability promotes +**Interoperability promotes** - Incremental adoption of Swift -- Reuse of libraries +- Reuse of libraries across languages + - Access libraries with or without native APIs + - Implement performance-sensitive code in Swift - Support for ecosystem-specific build tools -#### Tools +**Tools** - Swift Package: - JavaKit (Java -> Swift) - Java Library: @@ -27,6 +29,7 @@ Please refer to articles about the specific direction of interoperability you ar - swift-java: command line tool - Build tool integration: SwiftPM Plugin or Gradle + If you prefer a video introduction, you may want to watch this [Explore Swift and Java interoperability](https://www.youtube.com/watch?v=QSHO-GUGidA) WWDC 2025 session, From 01d7abb10c170527d18411e97966a2bc509bf0a2 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Tue, 26 Aug 2025 12:39:38 -0400 Subject: [PATCH 07/14] align documentation with module rename --- README.md | 6 ++-- .../{JavaKit.md => SwiftJava.md} | 32 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) rename Sources/SwiftJava/Documentation.docc/{JavaKit.md => SwiftJava.md} (90%) diff --git a/README.md b/README.md index 9f676905..6a09e50c 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ In order to run JMH benchmarks you can: ```bash cd Samples/SwiftKitSampleApp -gradle jmh +./gradlew jmh ``` Please read documentation of both performance testing tools and understand that results must be interpreted and not just taken at face value. Benchmarking is tricky and environment sensitive task, so please be careful when constructing and reading benchmarks and their results. If in doubt, please reach out on the forums. @@ -183,8 +183,8 @@ To view the rendered docc documentation you can use the docc preview command: ```bash xcrun docc preview Sources/SwiftJavaDocumentation/Documentation.docc -# OR JavaKit to view JavaKit documentation: -# xcrun docc preview Sources/SwiftJNI/Documentation.docc +# OR SwiftJava to view SwiftJava documentation: +# xcrun docc preview Sources/SwiftJava/Documentation.docc # ======================================== # Starting Local Preview Server diff --git a/Sources/SwiftJava/Documentation.docc/JavaKit.md b/Sources/SwiftJava/Documentation.docc/SwiftJava.md similarity index 90% rename from Sources/SwiftJava/Documentation.docc/JavaKit.md rename to Sources/SwiftJava/Documentation.docc/SwiftJava.md index a050e88b..7f53adfd 100644 --- a/Sources/SwiftJava/Documentation.docc/JavaKit.md +++ b/Sources/SwiftJava/Documentation.docc/SwiftJava.md @@ -1,8 +1,8 @@ -# JavaKit +# SwiftJava Library and tools to make it easy to use Java libraries from Swift using the Java Native Interface (JNI). -## JavaKit: Using Java libraries from Swift +## SwiftJava: Using Java libraries from Swift Existing Java libraries can be wrapped for use in Swift with the `swift-java` tool. In a Swift program, the most direct way to access a Java API is to use the SwiftPM plugin to provide Swift wrappers for the Java classes. To do so, add a configuration file `swift-java.config` into the source directory for the Swift target. This is a JSON file that specifies Java classes and the Swift type name that should be generated to wrap them. For example, the following file maps `java.math.BigInteger` to a Swift type named `BigInteger`: @@ -77,7 +77,7 @@ Swift ensures that the Java garbage collector will keep the object alive until ` ### Creating a Java Virtual Machine instance from Swift -When JavaKit requires a running Java Virtual Machine to use an operation (for example, to create an instance of `BigInteger`), it will query to determine if one is running and, if not, create one. To exercise more control over the creation and configuration of the Java virtual machine, use the `JavaVirtualMachine` class, which provides creation and query operations. One can create a shared instance by calling `JavaVirtualMachine.shared()`, optionally passing along extra options to the JVM (such as the class path): +When SwiftJava requires a running Java Virtual Machine to use an operation (for example, to create an instance of `BigInteger`), it will query to determine if one is running and, if not, create one. To exercise more control over the creation and configuration of the Java virtual machine, use the `JavaVirtualMachine` class, which provides creation and query operations. One can create a shared instance by calling `JavaVirtualMachine.shared()`, optionally passing along extra options to the JVM (such as the class path): ```swift let javaVirtualMachine = try JavaVirtualMachine.shared() @@ -100,7 +100,7 @@ let bigInt = BigInteger(veryBigNumber, environment: jniEnvironment) Java libraries are often distributed as Jar files. The `swift-java` tool can inspect a Jar file to create a `swift-java.config` file that will wrap all of the public classes for use in Swift. Following the example in `swift-java/Samples/JavaSieve`, we will wrap a small [Java library for computing prime numbers](https://github.com/gazman-sdk/quadratic-sieve-Java) for use in Swift. Assuming we have a Jar file `QuadraticSieve-1.0.jar` in the package directory, run the following command: ```swift -swift-java generate --module-name JavaSieve --jar QuadraticSieve-1.0.jar +swift-java configure --swift-module JavaSieve --jar QuadraticSieve-1.0.jar ``` The resulting configuration file will look something like this: @@ -138,7 +138,7 @@ The resulting configuration file will look something like this: } ``` -As with the previous `JavaProbablyPrime` sample, the `JavaSieve` target in `Package.swift` should depend on the `swift-java` package modules (`JavaKit`) and apply the `swift-java` plugin. This makes all of the Java classes found in the Jar file available to Swift within the `JavaSieve` target. +As with the previous `JavaProbablyPrime` sample, the `JavaSieve` target in `Package.swift` should depend on the `swift-java` package modules (`SwiftJava`) and apply the `swift-java` plugin. This makes all of the Java classes found in the Jar file available to Swift within the `JavaSieve` target. If you inspect the build output, there are a number of warnings that look like this: @@ -152,7 +152,7 @@ These warnings mean that some of the APIs in the Java library aren't available i .target( name: "JavaMath", dependencies: [ - .product(name: "JavaKit", package: "swift-java"), + .product(name: "SwiftJava", package: "swift-java"), ], plugins: [ .plugin(name: "SwiftJavaPlugin", package: "swift-java"), @@ -237,7 +237,7 @@ if let url = myObject.as(URL.self) { ### Implementing Java `native` methods in Swift -JavaKit supports implementing Java `native` methods in Swift using JNI with the `@JavaImplementation` macro. In Java, the method must be declared as `native`, e.g., +SwiftJava supports implementing Java `native` methods in Swift using JNI with the `@JavaImplementation` macro. In Java, the method must be declared as `native`, e.g., ```java package org.swift.javakit.example; @@ -261,7 +261,7 @@ On the Swift side, the Java class needs to be exposed to Swift through `swift-ja } ``` -Implementations of `native` methods are written in an extension of the Swift type that has been marked with `@JavaImplementation`. The methods themselves must be marked with `@JavaMethod`, indicating that they are available to Java as well. To help ensure that the Swift code implements all of the `native` methods with the right signatures, JavaKit produces a protocol with the Swift type name suffixed by `NativeMethods`. Declare conformance to that protocol and implement its requirements, for example: +Implementations of `native` methods are written in an extension of the Swift type that has been marked with `@JavaImplementation`. The methods themselves must be marked with `@JavaMethod`, indicating that they are available to Java as well. To help ensure that the Swift code implements all of the `native` methods with the right signatures, SwiftJava produces a protocol with the Swift type name suffixed by `NativeMethods`. Declare conformance to that protocol and implement its requirements, for example: ```swift @JavaImplementation("org.swift.javakit.HelloSwift") @@ -278,7 +278,7 @@ Java native methods that throw any checked exception should be marked as `throws The Swift implementations of Java `native` constructors and static methods require an additional Swift parameter `environment: JNIEnvironment? = nil`, which will receive the JNI environment in which the function is being executed. In case of nil, the `JavaVirtualMachine.shared().environment()` value will be used. -## JavaKit: Using Java libraries from Swift +## SwiftJava: Using Java libraries from Swift This section describes how Java libraries and mapped into Swift and their use from Swift. @@ -353,7 +353,7 @@ for entry in jarFile.entries()! { `JavaMethod` is a [function body macro](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0415-function-body-macros.md) that translates the argument and result types to/from Java and performs a call to the named method via JNI. -A Java method or constructor that throws a checked exception should be marked as `throws` in Swift. Swift's projection of Java throwable types (as `JavaKit.Throwable`) conforms to the Swift `Error` protocol, so Java exceptions will be rethrown as Swift errors. +A Java method or constructor that throws a checked exception should be marked as `throws` in Swift. Swift's projection of Java throwable types (as `SwiftJava.Throwable`) conforms to the Swift `Error` protocol, so Java exceptions will be rethrown as Swift errors. ### Java <-> Swift Type mapping @@ -380,14 +380,14 @@ For Swift projections of Java classes, the Swift type itself conforms to the `An Because Java has implicitly nullability of references, `AnyJavaObject` types do not directly conform to `JavaValue`: rather, optionals of `AnyJavaObject`-conforming type conform to `JavaValue`. This requires Swift code to deal with the optionality at interface boundaries rather than invite implicit NULL pointer dereferences. -A number of JavaKit modules provide Swift projections of Java classes and interfaces. Here are a few: +A number of SwiftJava modules provide Swift projections of Java classes and interfaces. Here are a few: | Java class | Swift class | Swift module | | --------------------- | -------------- | ---------------- | -| `java.lang.Object` | `JavaObject` | `JavaKit` | -| `java.lang.Class` | `JavaClass` | `JavaKit` | -| `java.lang.Throwable` | `Throwable` | `JavaKit` | -| `java.net.URL` | `URL` | `JavaKitNetwork` | +| `java.lang.Object` | `JavaObject` | `SwiftJava` | +| `java.lang.Class` | `JavaClass` | `SwiftJava` | +| `java.lang.Throwable` | `Throwable` | `SwiftJava` | +| `java.net.URL` | `URL` | `JavaNet` | The `swift-java` tool can translate any other Java classes into Swift projections. The easiest way to use `swift-java` is with the SwiftPM plugin described above. More information about using this tool directly are provided later in this document @@ -406,7 +406,7 @@ When building Java sources using the JavaCompilerPlugin this option is passed by ### Class objects and static methods -Every `AnyJavaObject` has a property `javaClass` that provides an instance of `JavaClass` specialized to the type. For example, `url.javaClass` will produce an instance of `JavaClass`. The `JavaClass` instance is a wrapper around a Java class object (`java.lang.Class`) that has two roles in Swift. First, it provides access to all of the APIs on the Java class object. The `JavaKitReflection` library, for example, exposes these APIs and the types they depend on (`Method`, +Every `AnyJavaObject` has a property `javaClass` that provides an instance of `JavaClass` specialized to the type. For example, `url.javaClass` will produce an instance of `JavaClass`. The `JavaClass` instance is a wrapper around a Java class object (`java.lang.Class`) that has two roles in Swift. First, it provides access to all of the APIs on the Java class object. The `JavaLangReflect` library, for example, exposes these APIs and the types they depend on (`Method`, `Constructor`, etc.) for dynamic reflection. Second, the `JavaClass` provides access to the `static` methods on the Java class. For example, [`java.net.URLConnection`](https://docs.oracle.com/javase/8/docs/api/java/net/URLConnection.html) has static methods to access default settings, such as the default for the `allowUserInteraction` field. These are exposed as instance methods on `JavaClass`, e.g., ```swift From f65061cf242697f10cbd738fbe76a21875c57a39 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Tue, 26 Aug 2025 21:41:39 -0400 Subject: [PATCH 08/14] SwiftJavaDocumentation correction --- Samples/JavaSieve/Sources/JavaSieve/swift-java.config | 1 - .../Documentation.docc/SupportedFeatures.md | 2 +- Sources/SwiftJavaTool/Commands/ConfigureCommand.swift | 7 ++----- Sources/SwiftJavaTool/CommonOptions.swift | 2 +- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Samples/JavaSieve/Sources/JavaSieve/swift-java.config b/Samples/JavaSieve/Sources/JavaSieve/swift-java.config index 40d01d4b..7e055d1c 100644 --- a/Samples/JavaSieve/Sources/JavaSieve/swift-java.config +++ b/Samples/JavaSieve/Sources/JavaSieve/swift-java.config @@ -29,4 +29,3 @@ "com.gazman.quadratic_sieve.wheel.Wheel" : "Wheel" } } - diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md index bbad804c..8d193149 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md @@ -38,7 +38,7 @@ Java `native` functions. JavaKit simplifies the type conversions ### JExtract – calling Swift from Java -SwiftJava's `swift-java jextract` tool automates generating Java bindings to Swift sources. +SwiftJava's `swift-java jextract` tool automates generating Java bindings from Swift sources. > tip: This direction of interoperability is covered in the WWDC2025 session 'Explore Swift and Java interoperability' > around the [14-minute mark](https://youtu.be/QSHO-GUGidA?si=b9YUwAWDWFGzhRXN&t=842). diff --git a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift index 8bafd257..4d6a39bf 100644 --- a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift +++ b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift @@ -35,8 +35,7 @@ extension SwiftJava { @OptionGroup var commonJVMOptions: SwiftJava.CommonJVMOptions // TODO: This should be a "make wrappers" option that just detects when we give it a jar - @Flag( - help: "Specifies that the input is a *.jar file whose public classes will be loaded. The output of swift-java will be a configuration file (swift-java.config) that can be used as input to a subsequent swift-java invocation to generate wrappers for those public classes." + @Flag(help: "Specifies that the input is a *.jar file whose public classes will be loaded. The output of swift-java will be a configuration file (swift-java.config) that can be used as input to a subsequent swift-java invocation to generate wrappers for those public classes." ) var jar: Bool = false @@ -57,9 +56,7 @@ extension SwiftJava { swiftModule } - @Argument( - help: "The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file." - ) + @Argument(help: "The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file.") var input: String? } } diff --git a/Sources/SwiftJavaTool/CommonOptions.swift b/Sources/SwiftJavaTool/CommonOptions.swift index c88ecbc8..f81342bc 100644 --- a/Sources/SwiftJavaTool/CommonOptions.swift +++ b/Sources/SwiftJavaTool/CommonOptions.swift @@ -125,4 +125,4 @@ extension HasCommonJVMOptions { func makeJVM(classpathEntries: [String]) throws -> JavaVirtualMachine { try JavaVirtualMachine.shared(classpath: classpathEntries) } -} \ No newline at end of file +} From f1c1d4b1224887211ef42ae993a78275d3e3ffa8 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Tue, 26 Aug 2025 21:48:04 -0400 Subject: [PATCH 09/14] Revert "SwiftJavaDocumentation correction" This reverts commit f65061cf242697f10cbd738fbe76a21875c57a39. --- Samples/JavaSieve/Sources/JavaSieve/swift-java.config | 1 + .../Documentation.docc/SupportedFeatures.md | 2 +- Sources/SwiftJavaTool/Commands/ConfigureCommand.swift | 7 +++++-- Sources/SwiftJavaTool/CommonOptions.swift | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Samples/JavaSieve/Sources/JavaSieve/swift-java.config b/Samples/JavaSieve/Sources/JavaSieve/swift-java.config index 7e055d1c..40d01d4b 100644 --- a/Samples/JavaSieve/Sources/JavaSieve/swift-java.config +++ b/Samples/JavaSieve/Sources/JavaSieve/swift-java.config @@ -29,3 +29,4 @@ "com.gazman.quadratic_sieve.wheel.Wheel" : "Wheel" } } + diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md index 8d193149..bbad804c 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md @@ -38,7 +38,7 @@ Java `native` functions. JavaKit simplifies the type conversions ### JExtract – calling Swift from Java -SwiftJava's `swift-java jextract` tool automates generating Java bindings from Swift sources. +SwiftJava's `swift-java jextract` tool automates generating Java bindings to Swift sources. > tip: This direction of interoperability is covered in the WWDC2025 session 'Explore Swift and Java interoperability' > around the [14-minute mark](https://youtu.be/QSHO-GUGidA?si=b9YUwAWDWFGzhRXN&t=842). diff --git a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift index 4d6a39bf..8bafd257 100644 --- a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift +++ b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift @@ -35,7 +35,8 @@ extension SwiftJava { @OptionGroup var commonJVMOptions: SwiftJava.CommonJVMOptions // TODO: This should be a "make wrappers" option that just detects when we give it a jar - @Flag(help: "Specifies that the input is a *.jar file whose public classes will be loaded. The output of swift-java will be a configuration file (swift-java.config) that can be used as input to a subsequent swift-java invocation to generate wrappers for those public classes." + @Flag( + help: "Specifies that the input is a *.jar file whose public classes will be loaded. The output of swift-java will be a configuration file (swift-java.config) that can be used as input to a subsequent swift-java invocation to generate wrappers for those public classes." ) var jar: Bool = false @@ -56,7 +57,9 @@ extension SwiftJava { swiftModule } - @Argument(help: "The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file.") + @Argument( + help: "The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file." + ) var input: String? } } diff --git a/Sources/SwiftJavaTool/CommonOptions.swift b/Sources/SwiftJavaTool/CommonOptions.swift index f81342bc..c88ecbc8 100644 --- a/Sources/SwiftJavaTool/CommonOptions.swift +++ b/Sources/SwiftJavaTool/CommonOptions.swift @@ -125,4 +125,4 @@ extension HasCommonJVMOptions { func makeJVM(classpathEntries: [String]) throws -> JavaVirtualMachine { try JavaVirtualMachine.shared(classpath: classpathEntries) } -} +} \ No newline at end of file From d9a0b3b10e9a35dc6ea3f25e0b381de16d1b3b99 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Tue, 26 Aug 2025 22:33:20 -0400 Subject: [PATCH 10/14] revert ConfigureCommand formatting --- Sources/SwiftJavaTool/Commands/ConfigureCommand.swift | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift index 8bafd257..3bce6c18 100644 --- a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift +++ b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift @@ -35,9 +35,7 @@ extension SwiftJava { @OptionGroup var commonJVMOptions: SwiftJava.CommonJVMOptions // TODO: This should be a "make wrappers" option that just detects when we give it a jar - @Flag( - help: "Specifies that the input is a *.jar file whose public classes will be loaded. The output of swift-java will be a configuration file (swift-java.config) that can be used as input to a subsequent swift-java invocation to generate wrappers for those public classes." - ) + @Flag(help: "Specifies that the input is a *.jar file whose public classes will be loaded. The output of swift-java will be a configuration file (swift-java.config) that can be used as input to a subsequent swift-java invocation to generate wrappers for those public classes.") var jar: Bool = false @Option( @@ -57,9 +55,7 @@ extension SwiftJava { swiftModule } - @Argument( - help: "The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file." - ) + @Argument(help: "The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file.") var input: String? } } @@ -199,10 +195,9 @@ extension SwiftJava.ConfigureCommand { javaCanonicalName.defaultSwiftNameForJavaClass } } - } package func fileOrDirectoryExists(at path: String) -> Bool { var isDirectory: ObjCBool = false return FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) -} \ No newline at end of file +} From a1dae3f0ecccf84494a9236e2cdc9c7b0890b6cc Mon Sep 17 00:00:00 2001 From: koloyyee Date: Tue, 26 Aug 2025 22:34:45 -0400 Subject: [PATCH 11/14] revert SupportFeatures.md --- .../Documentation.docc/SupportedFeatures.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md index bbad804c..8d193149 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md @@ -38,7 +38,7 @@ Java `native` functions. JavaKit simplifies the type conversions ### JExtract – calling Swift from Java -SwiftJava's `swift-java jextract` tool automates generating Java bindings to Swift sources. +SwiftJava's `swift-java jextract` tool automates generating Java bindings from Swift sources. > tip: This direction of interoperability is covered in the WWDC2025 session 'Explore Swift and Java interoperability' > around the [14-minute mark](https://youtu.be/QSHO-GUGidA?si=b9YUwAWDWFGzhRXN&t=842). From 7150c5c47faad8855e2cf2c1af7230946cbe7b8d Mon Sep 17 00:00:00 2001 From: koloyyee Date: Tue, 26 Aug 2025 22:53:16 -0400 Subject: [PATCH 12/14] update benchmark sample project name --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6a09e50c..c15db541 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ cd Samples/JavaKitSampleApp To run a simple example app showcasing the jextract (Java calling Swift) approach you can: ```bash -./gradlew Samples:SwiftKitSampleApp:run +./gradlew Samples:SwiftJavaExtractFFMSampleApp:run ``` This will also generate the necessary sources (by invoking jextract, extracting the `Sources/ExampleSwiftLibrary`) @@ -152,7 +152,7 @@ Please refer to the [Samples](Samples) directory for more sample apps which show You can run Swift [ordo-one/package-benchmark](https://github.com/ordo-one/package-benchmark) and OpenJDK [JMH](https://github.com/openjdk/jmh) benchmarks in this project. -Swift benchmarks are located under `Benchmarks/` and JMH benchmarks are currently part of the SwiftKit sample project: `Samples/SwiftKitSampleApp/src/jmh` because they depend on generated sources from the sample. +Swift benchmarks are located under `Benchmarks/` and JMH benchmarks are currently part of the SwiftKit sample project: `Samples/SwiftJavaExtractFFMSampleApp/src/jmh` because they depend on generated sources from the sample. ### Swift benchmarks @@ -168,7 +168,7 @@ swift package benchmark In order to run JMH benchmarks you can: ```bash -cd Samples/SwiftKitSampleApp +cd Samples/SwiftJavaExtractFFMSampleApp ./gradlew jmh ``` From cf95d4ebf04f441ae397391a3b026c24b36fe455 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Fri, 29 Aug 2025 20:45:48 -0400 Subject: [PATCH 13/14] renaming JavaKit to SwiftJava --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c15db541..52c61bb4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This repository contains two approaches to Swift/Java interoperability. -- Swift library (`JavaKit`) and bindings generator that allows a Swift program to make use of Java libraries by wrapping Java classes in corresponding Swift types, allowing Swift to directly call any wrapped Java API. +- Swift library (`SwiftJava`) and bindings generator that allows a Swift program to make use of Java libraries by wrapping Java classes in corresponding Swift types, allowing Swift to directly call any wrapped Java API. - The `swift-java` tool which which offers automated ways to import or "extract" bindings to sources or libraries in either language. The results are bindings for Swift or Java. ## :construction: :construction: :construction: Early Development :construction: :construction: :construction: @@ -19,11 +19,11 @@ The primary purpose of this repository is to create an environment for collabora This project consists of different modules which have different Swift and Java runtime requirements. -## JavaKit macros +## SwiftJava macros -JavaKit is a Swift library offering macros which simplify writing JNI code "by hand" but also calling Java code from Swift. +SwiftJava is a Swift library offering macros which simplify writing JNI code "by hand" but also calling Java code from Swift. -It is possible to generate Swift bindings to Java libraries using JavaKit by using the `swift-java wrap-java` command. +It is possible to generate Swift bindings to Java libraries using SwiftJava by using the `swift-java wrap-java` command. Required language/runtime versions: - **JDK 17+**, any recent JDK installation should be sufficient, as only general reflection and JNI APIs are used by this integration @@ -124,12 +124,12 @@ Please always use the gradle wrapper (`./gradlew`) to make sure to use the appro Sample apps are located in the `Samples/` directory, and they showcase full "roundtrip" usage of the library and/or tools. -#### JavaKit (Swift -> Java) +#### SwiftJava (Swift -> Java) To run a simple app showcasing a Swift process calling into a Java library you can run: ```bash -cd Samples/JavaKitSampleApp +cd Samples/SwiftJavaExtractFFMSampleApp ./ci-validate.sh # which is just `swift build` and a `java -cp ...` invocation of the compiled program ``` From d087a86adbd0f149d55b9f4b445ad5e7fcbec9b3 Mon Sep 17 00:00:00 2001 From: koloyyee Date: Fri, 29 Aug 2025 20:50:55 -0400 Subject: [PATCH 14/14] renaming JavaKit to SwiftJava in SupportedFeatures --- .../Documentation.docc/SupportedFeatures.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md index d4c12083..997e680e 100644 --- a/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md +++ b/Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md @@ -4,13 +4,13 @@ Summary of features supported by the swift-java interoperability libraries and t ## Overview -JavaKit supports both directions of interoperability, using Swift macros and source generation +SwiftJava supports both directions of interoperability, using Swift macros and source generation (via the `swift-java wrap-java` command). ### Java -> Swift -It is possible to use JavaKit macros and the `wrap-java` command to simplify implementing -Java `native` functions. JavaKit simplifies the type conversions +It is possible to use SwiftJava macros and the `wrap-java` command to simplify implementing +Java `native` functions. SwiftJava simplifies the type conversions > tip: This direction of interoperability is covered in the WWDC2025 session 'Explore Swift and Java interoperability' > around the [7-minute mark](https://youtu.be/QSHO-GUGidA?si=vUXxphTeO-CHVZ3L&t=448). @@ -74,8 +74,8 @@ SwiftJava's `swift-java jextract` tool automates generating Java bindings from S | Optional parameters: `func f(i: Int?, class: MyClass?)` | ✅ | ✅ | | Optional return types: `func f() -> Int?`, `func g() -> MyClass?` | ❌ | ✅ | | Primitive types: `Bool`, `Int`, `Int8`, `Int16`, `Int32`, `Int64`, `Float`, `Double` | ✅ | ✅ | -| Parameters: JavaKit wrapped types `JavaLong`, `JavaInteger` | ❌ | ✅ | -| Return values: JavaKit wrapped types `JavaLong`, `JavaInteger` | ❌ | ❌ | +| Parameters: SwiftJava wrapped types `JavaLong`, `JavaInteger` | ❌ | ✅ | +| Return values: SwiftJava wrapped types `JavaLong`, `JavaInteger` | ❌ | ❌ | | Unsigned primitive types: `UInt`, `UInt8`, `UInt16`, `UInt32`, `UInt64` | ✅ * | ✅ * | | String (with copying data) | ✅ | ✅ | | Variadic parameters: `T...` | ❌ | ❌ |