Skip to content

Commit 33d2997

Browse files
authored
implement JNIObject.getField(), fix threading bug (#17)
implement JNIObject.getField() make methodSignature() and asJavaParameters() public fix threading bug in JNIObject.javaClass
1 parent a765442 commit 33d2997

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Sources/JNI/Array+JavaParameterConvertible.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension String {
1515
}
1616

1717
extension Array where Element == JavaParameterConvertible {
18-
func asJavaParameters() -> [JavaParameter] {
18+
public func asJavaParameters() -> [JavaParameter] {
1919
return self.map { $0.toJavaParameter() }
2020
}
2121

@@ -27,12 +27,12 @@ extension Array where Element == JavaParameterConvertible {
2727

2828
/// Returns the String of ordered arguments enclosed in brackets, followed by the `returnType`'s type string, or 'V'
2929
/// (Void) if nil is provided. e.g. Returns "(II)V" for `[JavaInt(1), JavaInt(99)].methodSignature(returnType: nil)`
30-
func methodSignature(returnType: JavaParameterConvertible.Type?) -> String {
30+
public func methodSignature(returnType: JavaParameterConvertible.Type?) -> String {
3131
let returnTypeString = returnType?.asJNIParameterString ?? "V"
3232
return "(" + self.argumentSignature() + ")" + returnTypeString
3333
}
3434

35-
func methodSignature(customReturnType: String) -> String {
35+
public func methodSignature(customReturnType: String) -> String {
3636
let returnTypeString = customReturnType.replacingFullstopsWithSlashes()
3737
return "(" + self.argumentSignature() + ")" + returnTypeString
3838
}

Sources/JNI/JNIFields.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public extension JNI {
1818
func GetField<T: JavaInitializableFromField & JavaParameterConvertible>(_ fieldName: String, from javaObject: JavaObject) throws -> T {
1919
let env = self._env
2020
let javaClass = try GetObjectClass(obj: javaObject)
21+
defer {
22+
jni.DeleteLocalRef(javaClass)
23+
}
2124
let fieldID = env.pointee.pointee.GetFieldID(env, javaClass, fieldName, T.asJNIParameterString)
2225
try checkAndThrowOnJNIError()
2326
return try T.fromField(fieldID!, on: javaObject)

Sources/JNI/JNIObjects.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CJNI
22
import Dispatch
3+
import Foundation
34

45
/// Designed to simplify calling a constructor and methods on a JavaClass
56
/// Subclass this and add the methods appropriate to the object you are constructing.
@@ -11,7 +12,7 @@ open class JNIObject {
1112
private static var classInstances = [String: JavaClass]()
1213

1314
public static var javaClass: JavaClass {
14-
return DispatchQueue.main.sync {
15+
func getClassInstance() -> JavaClass {
1516
if let classInstance = classInstances[className] {
1617
return classInstance
1718
}
@@ -23,6 +24,15 @@ open class JNIObject {
2324

2425
return classInstance
2526
}
27+
28+
if Thread.isMainThread {
29+
return getClassInstance()
30+
} else {
31+
return DispatchQueue.main.sync {
32+
return getClassInstance()
33+
}
34+
}
35+
2636
}
2737

2838
public let instance: JavaObject
@@ -71,6 +81,12 @@ open class JNIObject {
7181
return try jni.call(methodName, on: self.instance, arguments: arguments)
7282
}
7383

84+
public func getField<T: JavaInitializableFromField & JavaParameterConvertible>(
85+
_ fieldName: String
86+
) throws -> T {
87+
return try jni.GetField(fieldName, from: self.instance)
88+
}
89+
7490
public static func callStatic(methodName: String, arguments: [JavaParameterConvertible] = []) throws {
7591
try jni.callStatic(methodName, on: self.javaClass, arguments: arguments)
7692
}

0 commit comments

Comments
 (0)