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
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
152
152
153
153
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.
154
154
155
-
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.
155
+
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.
156
156
157
157
### Swift benchmarks
158
158
@@ -168,8 +168,8 @@ swift package benchmark
168
168
In order to run JMH benchmarks you can:
169
169
170
170
```bash
171
-
cd Samples/SwiftKitSampleApp
172
-
gradle jmh
171
+
cd Samples/SwiftJavaExtractFFMSampleApp
172
+
./gradlew jmh
173
173
```
174
174
175
175
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:
Copy file name to clipboardExpand all lines: Sources/SwiftJava/Documentation.docc/SwiftJava.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
# JavaKit
1
+
# SwiftJava
2
2
3
3
Library and tools to make it easy to use Java libraries from Swift using the Java Native Interface (JNI).
4
4
5
-
## JavaKit: Using Java libraries from Swift
5
+
## SwiftJava: Using Java libraries from Swift
6
6
7
7
Existing Java libraries can be wrapped for use in Swift with the `swift-java`
8
8
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 `
77
77
78
78
### Creating a Java Virtual Machine instance from Swift
79
79
80
-
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):
80
+
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):
81
81
82
82
```swift
83
83
let javaVirtualMachine =try JavaVirtualMachine.shared()
@@ -100,7 +100,7 @@ let bigInt = BigInteger(veryBigNumber, environment: jniEnvironment)
100
100
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:
The resulting configuration file will look something like this:
@@ -138,7 +138,7 @@ The resulting configuration file will look something like this:
138
138
}
139
139
```
140
140
141
-
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.
141
+
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.
142
142
143
143
If you inspect the build output, there are a number of warnings that look like this:
144
144
@@ -152,7 +152,7 @@ These warnings mean that some of the APIs in the Java library aren't available i
@@ -237,10 +237,10 @@ if let url = myObject.as(URL.self) {
237
237
238
238
### Implementing Java `native` methods in Swift
239
239
240
-
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.,
240
+
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.,
241
241
242
242
```java
243
-
packageorg.swift.javakit.example;
243
+
packageorg.swift.swiftjava.example;
244
244
245
245
publicclassHelloSwift {
246
246
static {
@@ -256,15 +256,15 @@ On the Swift side, the Java class needs to be exposed to Swift through `swift-ja
256
256
```swift
257
257
{
258
258
"classes": {
259
-
"org.swift.javakit.example.HelloSwift":"Hello",
259
+
"org.swift.swiftjava.example.HelloSwift":"Hello",
260
260
}
261
261
}
262
262
```
263
263
264
-
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:
264
+
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:
@@ -278,7 +278,7 @@ Java native methods that throw any checked exception should be marked as `throws
278
278
279
279
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.
280
280
281
-
## JavaKit: Using Java libraries from Swift
281
+
## SwiftJava: Using Java libraries from Swift
282
282
283
283
This section describes how Java libraries and mapped into Swift and their use from Swift.
284
284
@@ -353,7 +353,7 @@ for entry in jarFile.entries()! {
353
353
354
354
`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.
355
355
356
-
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.
356
+
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.
357
357
358
358
### Java <-> Swift Type mapping
359
359
@@ -380,14 +380,14 @@ For Swift projections of Java classes, the Swift type itself conforms to the `An
380
380
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
381
381
at interface boundaries rather than invite implicit NULL pointer dereferences.
382
382
383
-
A number of JavaKit modules provide Swift projections of Java classes and interfaces. Here are a few:
383
+
A number of SwiftJava modules provide Swift projections of Java classes and interfaces. Here are a few:
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
393
393
@@ -406,7 +406,7 @@ When building Java sources using the JavaCompilerPlugin this option is passed by
406
406
407
407
### Class objects and static methods
408
408
409
-
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<URL>`. 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`,
409
+
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<URL>`. 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`,
410
410
`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.,
// TODO: This should be a "make wrappers" option that just detects when we give it a jar
38
-
@Flag(
39
-
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."
40
-
)
38
+
@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.")
41
39
varjar:Bool=false
42
40
43
41
@Option(
@@ -57,9 +55,7 @@ extension SwiftJava {
57
55
swiftModule
58
56
}
59
57
60
-
@Argument(
61
-
help:"The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file."
62
-
)
58
+
@Argument(help:"The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file.")
0 commit comments