Skip to content

Commit 4c86346

Browse files
committed
JavaKit: Enable imported Java classes as Swift classes by default
Flip the switch to have Java2Swift generate Swift classes to represent each imported Java class, and make the (relatively few) adjustments needed to the JavaKit library to make that work. Implements issue #132.
1 parent 37bce97 commit 4c86346

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1026
-2774
lines changed

Sources/Java2Swift/JavaToSwift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct JavaToSwift: ParsableCommand {
184184
let translator = JavaTranslator(
185185
swiftModuleName: moduleName,
186186
environment: environment,
187-
translateAsClass: false
187+
translateAsClass: true
188188
)
189189

190190
// Keep track of all of the Java classes that will have

Sources/JavaKit/AnyJavaObject.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ public protocol AnyJavaObject {
4242
/// Retrieve the full Java class name (e.g., java.util.Vector)
4343
static var fullJavaClassName: String { get }
4444

45-
/// The Java superclass type
46-
associatedtype JavaSuperclass: AnyJavaObject
47-
4845
/// Initialize a Java object from the Swift instance that keeps it alive.
4946
init(javaHolder: JavaObjectHolder)
5047

Sources/JavaKit/Exceptions/Exception+Error.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
// Translate all Java Exception instances in a Swift error.
16-
extension Exception: Error, CustomStringConvertible {
17-
public var description: String {
18-
return getMessage()
19-
}
20-
}
21-
2215
extension JavaClass<Exception> {
2316
/// Determine whether this instance is a checked exception (which must be
2417
/// handled) vs. an unchecked exception (which is not handled).

Sources/JavaKit/JavaClass+Initialization.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ extension JavaClass {
1818
public typealias ObjectType = T
1919

2020
/// Lookup this Java class within the given environment.
21-
public init(environment: JNIEnvironment? = nil) throws {
21+
@_nonoverride
22+
public convenience init(environment: JNIEnvironment? = nil) throws {
2223
let environment = try environment ?? JavaVirtualMachine.shared().environment()
2324
self.init(
2425
javaThis: try ObjectType.getJNIClass(in: environment),

Sources/JavaKit/JavaObject+Inheritance.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
import JavaRuntime
1616

1717
extension AnyJavaObject {
18-
/// Retrieve the same object instance but with the superclass type.
19-
public var `super`: JavaSuperclass {
20-
JavaSuperclass(javaHolder: javaHolder)
21-
}
22-
2318
/// Look up the other class type
2419
///
2520
/// - Returns: `nil` when either `OtherClass` isn't known to the

Sources/JavaKit/JavaObject+MethodCalls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ extension JavaClass {
387387
return FieldType(fromJNI: jniMethod(javaEnvironment, javaThis, fieldID), in: javaEnvironment)
388388
}
389389

390-
nonmutating set {
390+
set {
391391
let fieldID = getJNIStaticFieldID(fieldName, fieldType: fieldType)!
392392
let jniMethod = FieldType.jniStaticFieldSet(in: javaEnvironment)
393393
jniMethod(javaEnvironment, javaThis, fieldID, newValue.getJNIValue(in: javaEnvironment))
Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,17 @@
11
// Auto-generated by Java-to-Swift wrapper generator.
22
import JavaRuntime
33

4-
@JavaClass("java.lang.Exception", extends: Throwable.self)
5-
public struct Exception {
4+
@JavaClass("java.lang.Exception")
5+
open class Exception: Throwable {
66
@JavaMethod
7-
public init(_ arg0: Throwable?, environment: JNIEnvironment? = nil)
7+
@_nonoverride public convenience init(_ arg0: Throwable?, environment: JNIEnvironment? = nil)
88

99
@JavaMethod
10-
public init(_ arg0: String, _ arg1: Throwable?, environment: JNIEnvironment? = nil)
10+
@_nonoverride public convenience init(_ arg0: String, _ arg1: Throwable?, environment: JNIEnvironment? = nil)
1111

1212
@JavaMethod
13-
public init(_ arg0: String, environment: JNIEnvironment? = nil)
13+
@_nonoverride public convenience init(_ arg0: String, environment: JNIEnvironment? = nil)
1414

1515
@JavaMethod
16-
public init(environment: JNIEnvironment? = nil)
17-
18-
@JavaMethod
19-
public func printStackTrace()
20-
21-
@JavaMethod
22-
public func fillInStackTrace() -> Throwable!
23-
24-
@JavaMethod
25-
public func getCause() -> Throwable!
26-
27-
@JavaMethod
28-
public func initCause(_ arg0: Throwable?) -> Throwable!
29-
30-
@JavaMethod
31-
public func toString() -> String
32-
33-
@JavaMethod
34-
public func getMessage() -> String
35-
36-
@JavaMethod
37-
public func getSuppressed() -> [Throwable?]
38-
39-
@JavaMethod
40-
public func getLocalizedMessage() -> String
41-
42-
@JavaMethod
43-
public func addSuppressed(_ arg0: Throwable?)
44-
45-
@JavaMethod
46-
public func equals(_ arg0: JavaObject?) -> Bool
47-
48-
@JavaMethod
49-
public func hashCode() -> Int32
50-
51-
@JavaMethod
52-
public func getClass() -> JavaClass<JavaObject>!
53-
54-
@JavaMethod
55-
public func notify()
56-
57-
@JavaMethod
58-
public func notifyAll()
59-
60-
@JavaMethod
61-
public func wait(_ arg0: Int64) throws
62-
63-
@JavaMethod
64-
public func wait(_ arg0: Int64, _ arg1: Int32) throws
65-
66-
@JavaMethod
67-
public func wait() throws
16+
@_nonoverride public convenience init(environment: JNIEnvironment? = nil)
6817
}

Sources/JavaKit/generated/JavaArray.swift

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
11
// Auto-generated by Java-to-Swift wrapper generator.
22
import JavaRuntime
33

4-
@JavaClass("java.lang.reflect.Array", extends: JavaObject.self)
5-
public struct JavaArray {
6-
@JavaMethod
7-
public func equals(_ arg0: JavaObject?) -> Bool
4+
@JavaClass("java.lang.reflect.Array")
5+
open class JavaArray: JavaObject {
86

9-
@JavaMethod
10-
public func toString() -> String
11-
12-
@JavaMethod
13-
public func hashCode() -> Int32
14-
15-
@JavaMethod
16-
public func getClass() -> JavaClass<JavaObject>!
17-
18-
@JavaMethod
19-
public func notify()
20-
21-
@JavaMethod
22-
public func notifyAll()
23-
24-
@JavaMethod
25-
public func wait(_ arg0: Int64) throws
26-
27-
@JavaMethod
28-
public func wait(_ arg0: Int64, _ arg1: Int32) throws
29-
30-
@JavaMethod
31-
public func wait() throws
327
}
338
extension JavaClass<JavaArray> {
349
@JavaStaticMethod

Sources/JavaKit/generated/JavaBoolean.swift

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,31 @@
11
// Auto-generated by Java-to-Swift wrapper generator.
22
import JavaRuntime
33

4-
@JavaClass("java.lang.Boolean", extends: JavaObject.self)
5-
public struct JavaBoolean {
4+
@JavaClass("java.lang.Boolean")
5+
open class JavaBoolean: JavaObject {
66
@JavaMethod
7-
public init(_ arg0: Bool, environment: JNIEnvironment? = nil)
7+
@_nonoverride public convenience init(_ arg0: Bool, environment: JNIEnvironment? = nil)
88

99
@JavaMethod
10-
public init(_ arg0: String, environment: JNIEnvironment? = nil)
10+
@_nonoverride public convenience init(_ arg0: String, environment: JNIEnvironment? = nil)
1111

1212
@JavaMethod
13-
public func equals(_ arg0: JavaObject?) -> Bool
13+
open override func equals(_ arg0: JavaObject?) -> Bool
1414

1515
@JavaMethod
16-
public func toString() -> String
16+
open override func toString() -> String
1717

1818
@JavaMethod
19-
public func hashCode() -> Int32
19+
open override func hashCode() -> Int32
2020

2121
@JavaMethod
22-
public func compareTo(_ arg0: JavaBoolean?) -> Int32
22+
open func compareTo(_ arg0: JavaBoolean?) -> Int32
2323

2424
@JavaMethod
25-
public func compareTo(_ arg0: JavaObject?) -> Int32
25+
open func compareTo(_ arg0: JavaObject?) -> Int32
2626

2727
@JavaMethod
28-
public func booleanValue() -> Bool
29-
30-
@JavaMethod
31-
public func getClass() -> JavaClass<JavaObject>!
32-
33-
@JavaMethod
34-
public func notify()
35-
36-
@JavaMethod
37-
public func notifyAll()
38-
39-
@JavaMethod
40-
public func wait(_ arg0: Int64) throws
41-
42-
@JavaMethod
43-
public func wait(_ arg0: Int64, _ arg1: Int32) throws
44-
45-
@JavaMethod
46-
public func wait() throws
28+
open func booleanValue() -> Bool
4729
}
4830
extension JavaClass<JavaBoolean> {
4931
@JavaStaticField(isFinal: true)

Sources/JavaKit/generated/JavaByte.swift

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,46 @@
11
// Auto-generated by Java-to-Swift wrapper generator.
22
import JavaRuntime
33

4-
@JavaClass("java.lang.Byte", extends: JavaNumber.self)
5-
public struct JavaByte {
4+
@JavaClass("java.lang.Byte")
5+
open class JavaByte: JavaNumber {
66
@JavaMethod
7-
public init(_ arg0: Int8, environment: JNIEnvironment? = nil)
7+
@_nonoverride public convenience init(_ arg0: Int8, environment: JNIEnvironment? = nil)
88

99
@JavaMethod
10-
public init(_ arg0: String, environment: JNIEnvironment? = nil) throws
10+
@_nonoverride public convenience init(_ arg0: String, environment: JNIEnvironment? = nil) throws
1111

1212
@JavaMethod
13-
public func equals(_ arg0: JavaObject?) -> Bool
13+
open override func equals(_ arg0: JavaObject?) -> Bool
1414

1515
@JavaMethod
16-
public func toString() -> String
16+
open override func toString() -> String
1717

1818
@JavaMethod
19-
public func hashCode() -> Int32
19+
open override func hashCode() -> Int32
2020

2121
@JavaMethod
22-
public func compareTo(_ arg0: JavaByte?) -> Int32
22+
open func compareTo(_ arg0: JavaByte?) -> Int32
2323

2424
@JavaMethod
25-
public func compareTo(_ arg0: JavaObject?) -> Int32
25+
open func compareTo(_ arg0: JavaObject?) -> Int32
2626

2727
@JavaMethod
28-
public func byteValue() -> Int8
28+
open override func byteValue() -> Int8
2929

3030
@JavaMethod
31-
public func shortValue() -> Int16
31+
open override func shortValue() -> Int16
3232

3333
@JavaMethod
34-
public func intValue() -> Int32
34+
open override func intValue() -> Int32
3535

3636
@JavaMethod
37-
public func longValue() -> Int64
37+
open override func longValue() -> Int64
3838

3939
@JavaMethod
40-
public func floatValue() -> Float
40+
open override func floatValue() -> Float
4141

4242
@JavaMethod
43-
public func doubleValue() -> Double
44-
45-
@JavaMethod
46-
public func getClass() -> JavaClass<JavaObject>!
47-
48-
@JavaMethod
49-
public func notify()
50-
51-
@JavaMethod
52-
public func notifyAll()
53-
54-
@JavaMethod
55-
public func wait(_ arg0: Int64) throws
56-
57-
@JavaMethod
58-
public func wait(_ arg0: Int64, _ arg1: Int32) throws
59-
60-
@JavaMethod
61-
public func wait() throws
43+
open override func doubleValue() -> Double
6244
}
6345
extension JavaClass<JavaByte> {
6446
@JavaStaticField(isFinal: true)

0 commit comments

Comments
 (0)