Skip to content

Commit d1b71c3

Browse files
committed
JavaKit: Update user guide for the move to Swift classes
1 parent 4c86346 commit d1b71c3

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

USER_GUIDE.md

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ If you build the project, there will be a generated file `BigInteger.swift` that
4545

4646
```swift
4747
@JavaClass("java.math.BigInteger")
48-
public struct BigInteger {
48+
open class BigInteger: JavaNumber {
4949
@JavaMethod
5050
public init(_ arg0: String, environment: JNIEnvironment? = nil)
5151

5252
@JavaMethod
53-
public func toString() -> String
53+
open func toString() -> String
5454

5555
@JavaMethod
56-
public func isProbablePrime(_ arg0: Int32) -> Bool
56+
open func isProbablePrime(_ arg0: Int32) -> Bool
5757

5858
// many more methods
5959
}
@@ -219,7 +219,7 @@ do {
219219

220220
Note that we are passing the Jar file in the `classPath` argument when initializing the `JavaVirtualMachine` instance. Otherwise, the program will fail with an error because it cannot find the Java class `com.gazman.quadratic_sieve.primes.SieveOfEratosthenes`.
221221

222-
### Up and downcasting
222+
### Downcasting
223223

224224
All Java classes available in Swift provide `is` and `as` methods to check whether an object dynamically matches another type. The `is` operation is the equivalent of Java's `instanceof` and Swift's `is` operator, and will checkin whether a given object is of the specified type, e.g.,
225225

@@ -288,31 +288,31 @@ This section describes how Java libraries and mapped into Swift and their use fr
288288

289289
### Translation from Java classes into Swift
290290

291-
Each Java class that can be used from Swift is translated to a Swift `struct` that provides information about the Java class itself and is populated with the Swift projection of each of its constructors, methods, and fields. For example, here is an excerpt of the Swift projection of [`java.util.jar.JarFile`](https://docs.oracle.com/javase/8/docs/api/java/util/jar/JarFile.html):
291+
Each Java class that can be used from Swift is translated to a Swift `class` that provides information about the Java class itself and is populated with the Swift projection of each of its constructors, methods, and fields. For example, here is an excerpt of the Swift projection of [`java.util.jar.JarFile`](https://docs.oracle.com/javase/8/docs/api/java/util/jar/JarFile.html):
292292

293293
```swift
294-
@JavaClass("java.util.jar.JarFile", extends: ZipFile.self, implements: AutoCloseable.self)
295-
public struct JarFile {
294+
@JavaClass("java.util.jar.JarFile", extends: AutoCloseable.self)
295+
open class JarFile: ZipFile {
296296
@JavaMethod
297-
public init(_ arg0: String, _ arg1: Bool, environment: JNIEnvironment? = nil)
297+
@_nonoverride public convenience init(_ arg0: String, _ arg1: Bool, environment: JNIEnvironment? = nil) throws
298298

299299
@JavaMethod
300-
public func entries() -> Enumeration<JarEntry>?
300+
@_nonoverride public convenience init(_ arg0: String, environment: JNIEnvironment? = nil) throws
301301

302302
@JavaMethod
303-
public func getManifest() -> Manifest?
303+
open func entries() -> Enumeration<JarEntry>!
304304

305305
@JavaMethod
306-
public func getJarEntry(_ arg0: String) -> JarEntry?
306+
open func getManifest() throws -> Manifest!
307307

308308
@JavaMethod
309-
public func isMultiRelease() -> Bool
309+
open func getEntry(_ arg0: String) -> ZipEntry!
310310

311311
@JavaMethod
312-
public func getName() -> String
312+
open func getJarEntry(_ arg0: String) -> JarEntry!
313313

314314
@JavaMethod
315-
public func size() -> Int32
315+
open func isMultiRelease() -> Bool
316316
}
317317
```
318318

@@ -328,7 +328,7 @@ Each of the public Java constructors, methods, and fields in the Java class will
328328

329329
```swift
330330
@JavaMethod
331-
public init(_ arg0: String, _ arg1: Bool, environment: JNIEnvironment? = nil)
331+
public convenience init(_ arg0: String, _ arg1: Bool, environment: JNIEnvironment? = nil)
332332
```
333333

334334
corresponds to the Java constructor:
@@ -424,6 +424,20 @@ extension JavaClass<URLConnection> {
424424

425425
Java interfaces are similar to classes, and are projected into Swift in much the same way, but with the macro `JavaInterface`. The `JavaInterface` macro takes the Java interface name as well as any Java interfaces that this interface extends. As an example, here is the Swift projection of the [`java.util.Enumeration`](https://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html) generic interface:
426426

427+
```swift
428+
@JavaInterface("java.util.Enumeration")
429+
public struct Enumeration<E: AnyJavaObject> {
430+
@JavaMethod
431+
public func asIterator() -> JavaIterator<JavaObject>!
432+
433+
@JavaMethod
434+
public func hasMoreElements() -> Bool
435+
436+
@JavaMethod
437+
public func nextElement() -> JavaObject!
438+
}
439+
```
440+
427441
## Translating Java classes with `Java2Swift`
428442

429443
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

0 commit comments

Comments
 (0)