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
Copy file name to clipboardExpand all lines: USER_GUIDE.md
+51-44Lines changed: 51 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -8,8 +8,8 @@ Before using this package, set the `JAVA_HOME` environment variable to point at
8
8
9
9
### Using Java libraries from Swift
10
10
11
-
Existing Java libraries can be wrapped for use in Swift with the `Java2Swift`
12
-
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 `Java2Swift.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`:
11
+
Existing Java libraries can be wrapped for use in Swift with the `swift-java`
12
+
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`:
13
13
14
14
```json
15
15
{
@@ -31,11 +31,11 @@ or, equivalently, adding the following to the package dependencies:
Finally, update `Package.swift` so that the `Java2SwiftPlugin` plugin runs on the target in which you want to generate Swift wrappers. The plugin looks like this:
34
+
Finally, update `Package.swift` so that the `SwiftJavaPlugin` plugin runs on the target in which you want to generate Swift wrappers. The plugin looks like this:
@@ -101,10 +101,10 @@ let bigInt = BigInteger(veryBigNumber, environment: jniEnvironment)
101
101
102
102
### Importing a Jar file into Swift
103
103
104
-
Java libraries are often distributed as Jar files. The `Java2Swift` tool can inspect a Jar file to create a `Java2Swift.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:
104
+
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:
105
105
106
106
```swift
107
-
swift run Java2Swift--module-name JavaSieve --jar QuadraticSieve-1.0.jar
The resulting configuration file will look something like this:
@@ -142,7 +142,7 @@ The resulting configuration file will look something like this:
142
142
}
143
143
```
144
144
145
-
As with the previous `JavaProbablyPrime` sample, the `JavaSieve` target in `Package.swift` should depend on the `swift-java` package modules (`JavaKit`) and apply the `Java2Swift` plugin. This makes all of the Java classes found in the Jar file available to Swift within the `JavaSieve` target.
145
+
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.
146
146
147
147
If you inspect the build output, there are a number of warnings that look like this:
148
148
@@ -159,12 +159,12 @@ These warnings mean that some of the APIs in the Java library aren't available i
Then define a a Java2Swift configuration file in `Sources/JavaMath/Java2Swift.config` to bring in the types we need:
167
+
Then define a a swift-java configuration file in `Sources/JavaMath/swift-java.config` to bring in the types we need:
168
168
169
169
```json
170
170
{
@@ -255,7 +255,7 @@ public class HelloSwift {
255
255
}
256
256
```
257
257
258
-
On the Swift side, the Java class needs to be exposed to Swift through `Java2Swift.config`, e.g.,:
258
+
On the Swift side, the Java class needs to be exposed to Swift through `swift-java.config`, e.g.,:
259
259
260
260
```swift
261
261
{
@@ -393,7 +393,7 @@ A number of JavaKit modules provide Swift projections of Java classes and interf
393
393
|`java.lang.Throwable`|`Throwable`|`JavaKit`|
394
394
|`java.net.URL`|`URL`|`JavaKitNetwork`|
395
395
396
-
The `Java2Swift` tool can translate any other Java classes into Swift projections. The easiest way to use `Java2Swift` is with the SwiftPM plugin described above. More information about using this tool directly are provided later in this document
396
+
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
397
397
398
398
#### Improve parameter names of imported Java methods
399
399
When building Java libraries you can pass the `-parameters` option to javac
@@ -438,65 +438,71 @@ public struct Enumeration<E: AnyJavaObject> {
438
438
}
439
439
```
440
440
441
-
## Translating Java classes with `Java2Swift`
441
+
## Translating Java classes with `swift-java`
442
442
443
-
The `Java2Swift` is a Swift program that uses Java's runtime reflection facilities to translate the requested Java classes into their Swift projections. The output is a number of Swift source files, each of which corresponds to a
444
-
single Java class. The `Java2Swift` can be executed like this:
443
+
The `swift-java` is a Swift program that uses Java's runtime reflection facilities to translate the requested Java classes into their Swift projections. The output is a number of Swift source files, each of which corresponds to a
444
+
single Java class. The `swift-java` can be executed like this:
OVERVIEW: Generate sources and configuration for Swift and Java interoperability.
454
454
455
-
ARGUMENTS:
456
-
<input> The input file, which is either a Java2Swift
457
-
configuration file or (if '-jar' was specified)
458
-
a Jar file.
455
+
USAGE: swift-java <options> <subcommand>
459
456
460
457
OPTIONS:
461
-
--module-name <module-name>
462
-
The name of the Swift module into which the resulting
463
-
Swift types will be generated.
464
458
--depends-on <depends-on>
465
-
A Java2Swift configuration file for a given Swift
466
-
module name on which this module depends, e.g.,
467
-
JavaKitJar=Sources/JavaKitJar/Java2Swift.config.
468
-
There should be one of these options for each Swift
469
-
module that this module depends on (transitively)
470
-
that contains wrapped Java sources.
471
-
--jar Specifies that the input is a Jar file whose public
472
-
classes will be loaded. The output of Java2Swift will
473
-
be a configuration file (Java2Swift.config) that can
474
-
be used as input to a subsequent Java2Swift
475
-
invocation to generate wrappers for those public
476
-
classes.
477
-
--cp, --classpath <cp> Class search path of directories and zip/jar files
478
-
from which Java classes can be loaded.
459
+
A Java2Swift configuration file for a given Swift module name on which this module depends, e.g., JavaKitJar=Sources/JavaKitJar/Java2Swift.config. There should be one of these options for each Swift module that this
460
+
module depends on (transitively) that contains wrapped Java sources.
461
+
--jar Specifies that the input is a Jar file whose public classes will be loaded. The output of Java2Swift will be a configuration file (Java2Swift.config) that can be used as input to a subsequent Java2Swift invocation to
462
+
generate wrappers for those public classes.
463
+
--fetch Fetch dependencies from given target (containing swift-java configuration) or dependency string
The names of Java classes whose declared native methods will be implemented in Swift.
466
+
--output-swift <output-swift>
467
+
The directory where generated Swift files should be written. Generally used with jextract mode.
468
+
--output-java <output-java>
469
+
The directory where generated Java files should be written. Generally used with jextract mode.
470
+
--java-package <java-package>
471
+
The Java package the generated Java code should be emitted into.
472
+
-c, --cache-directory <cache-directory>
473
+
Directory where to write cached values (e.g. swift-java.classpath files)
479
474
-o, --output-directory <output-directory>
480
-
The directory in which to output the generated Swift
481
-
files or the Java2Swift configuration file. (default:
482
-
.)
475
+
The directory in which to output the generated Swift files or the SwiftJava configuration file.
476
+
--input-swift <input-swift>
477
+
Directory containing Swift files which should be extracted into Java bindings. Also known as 'jextract' mode. Must be paired with --output-java and --output-swift.
478
+
-l, --log-level <log-level>
479
+
Configure the level of logs that should be printed (values: trace, debug, info, notice, warning, error, critical; default: log level)
480
+
--cp, --classpath <cp> Class search path of directories and zip/jar files from which Java classes can be loaded.
481
+
-f, --filter-java-package <filter-java-package>
482
+
While scanning a classpath, inspect only types included in this package
483
483
-h, --help Show help information.
484
+
485
+
SUBCOMMANDS:
486
+
configure Configure and emit a swift-java.config file based on an input dependency or jar file
487
+
resolve Resolve dependencies and write the resulting swift-java.classpath file
488
+
489
+
See 'swift-java help <subcommand>' for detailed help.
484
490
```
485
491
486
492
For example, the `JavaKitJar` library is generated with this command line:
487
493
488
494
```swift
489
-
swift run Java2Swift--module-name JavaKitJar --depends-on JavaKit=Sources/JavaKit/Java2Swift.config-o Sources/JavaKitJar/generated Sources/JavaKitJar/Java2Swift.config
495
+
swift run swift-java--module-name JavaKitJar --depends-on JavaKit=Sources/JavaKit/swift-java.config-o Sources/JavaKitJar/generated Sources/JavaKitJar/swift-java.config
490
496
```
491
497
492
498
The `--module-name JavaKitJar` parameter describes the name of the Swift module in which the code will be generated.
493
499
494
-
The `--depends-on` option is followed by the Java2Swift configuration files for any library on which this Swift library depends. Each `--depends-on` option is of the form `<swift library name>=<Java2Swift.config path>`, and tells Java2Swift which other Java classes have already been translated to Swift. For example, if your Java class uses `java.net.URL`, then you should include
500
+
The `--depends-on` option is followed by the swift-java configuration files for any library on which this Swift library depends. Each `--depends-on` option is of the form `<swift library name>=<swift-java.config path>`, and tells swift-java which other Java classes have already been translated to Swift. For example, if your Java class uses `java.net.URL`, then you should include
495
501
`JavaKitNetwork`'s configuration file as a dependency here.
496
502
497
503
The `-o` option specifies the output directory. Typically, this will be `Sources/<module name>/generated` or similar to keep the generated Swift files separate from any hand-written ones. To see the output on the terminal rather than writing files to disk, pass `-` for this option.
498
504
499
-
Finally, the command line should contain the `Java2Swift.config` file containing the list of classes that should be translated into Swift and their corresponding Swift type names. The tool will output a single `.swift` file for each class, along with warnings for any public API that cannot be translated into Swift. The most common warnings are due to missing Swift projections for Java classes. For example, here we have not translated (or provided the translation manifests for) the Java classes
505
+
Finally, the command line should contain the `swift-java.config` file containing the list of classes that should be translated into Swift and their corresponding Swift type names. The tool will output a single `.swift` file for each class, along with warnings for any public API that cannot be translated into Swift. The most common warnings are due to missing Swift projections for Java classes. For example, here we have not translated (or provided the translation manifests for) the Java classes
500
506
`java.util.zip.ZipOutputStream` and `java.io.OutputStream`:
The result of such warnings is that certain information won't be statically available in Swift, e.g., the superclass won't be known (so we will assume it is `JavaObject`), or the specified constructors or methods won't be translated. If you don't need these APIs, the warnings can be safely ignored. The APIs can still be called dynamically via JNI.
509
515
510
-
The `--jar` option changes the operation of `Java2Swift`. Instead of wrapping Java classes in Swift, it scans the given input Jar file to find all public classes and outputs a configuration file `Java2Swift.config` mapping all of the Java classes in the Jar file to Swift types. The `--jar` mode is expected to be used to help import a Java library into Swift wholesale, after which Java2Swift should invoked again given the generated configuration file.
516
+
The `--jar` option changes the operation of `swift-java`. Instead of wrapping Java classes in Swift, it scans the given input Jar file to find all public classes and outputs a configuration file `swift-java.config` mapping all of the Java classes in the Jar file to Swift types. The `--jar` mode is expected to be used to help import a Java library into Swift wholesale, after which swift-java should invoked again given the generated configuration file.
511
517
512
518
### Under construction: Create a Java class to wrap the Swift library
513
519
@@ -651,6 +657,7 @@ A Swift function may accept a closure which is used as a callback:
651
657
funccallMe(maybe: () -> ()) {}
652
658
```
653
659
660
+
Minimal support for c-compatible closures is implemented, more documentation soon.
0 commit comments