Skip to content

Commit 5e874ab

Browse files
authored
jextract: fix protocol threading (#483)
1 parent 2534f41 commit 5e874ab

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

Sources/SwiftJava/AnyJavaObject.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ extension AnyJavaObject {
110110
private static func _withJNIClassFromCustomClassLoader<Result>(
111111
_ classLoader: JavaClassLoader,
112112
in environment: JNIEnvironment,
113-
_ body: (jclass) throws -> Result
113+
_ body: (jclass?) throws -> Result
114114
) throws -> Result {
115-
let resolvedClass = try classLoader.findClass(fullJavaClassName)
116-
return try body(resolvedClass!.javaThis)
115+
let resolvedClass = try? classLoader.findClass(fullJavaClassName)
116+
return try body(resolvedClass?.javaThis)
117117
}
118118

119119
/// Retrieve the Java class for this type and execute body().
@@ -124,7 +124,14 @@ extension AnyJavaObject {
124124
) throws -> Result {
125125
if let AnyJavaObjectWithCustomClassLoader = self as? AnyJavaObjectWithCustomClassLoader.Type,
126126
let customClassLoader = try AnyJavaObjectWithCustomClassLoader.getJavaClassLoader(in: environment) {
127-
try _withJNIClassFromCustomClassLoader(customClassLoader, in: environment, body)
127+
try _withJNIClassFromCustomClassLoader(customClassLoader, in: environment) { clazz in
128+
guard let clazz else {
129+
// If the custom class loader did not find the class
130+
// let's look in the default class loader.
131+
return try _withJNIClassFromDefaultClassLoader(in: environment, body)
132+
}
133+
return try body(clazz)
134+
}
128135
} else {
129136
try _withJNIClassFromDefaultClassLoader(in: environment, body)
130137
}

Sources/SwiftJava/JavaObject+Inheritance.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ extension AnyJavaObject {
2525
guard let this: jobject = javaThisOptional else {
2626
return nil
2727
}
28+
let environment = try! JavaVirtualMachine.shared().environment()
2829

29-
return try? otherClass.withJNIClass(in: javaEnvironment) { otherJavaClass in
30-
if javaEnvironment.interface.IsInstanceOf(javaEnvironment, this, otherJavaClass) == 0 {
30+
return try? otherClass.withJNIClass(in: environment) { otherJavaClass in
31+
if environment.interface.IsInstanceOf(environment, this, otherJavaClass) == 0 {
3132
return nil
3233
}
3334

Sources/SwiftJavaRuntimeSupport/generated/JavaJNISwiftInstance.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import SwiftJava
1616

1717
@JavaInterface("org.swift.swiftkit.core.JNISwiftInstance")
18-
public struct JavaJNISwiftInstance {
18+
public struct JavaJNISwiftInstance: AnyJavaObjectWithCustomClassLoader {
1919
@JavaMethod("$memoryAddress")
2020
public func memoryAddress() -> Int64
21+
22+
public static func getJavaClassLoader(in environment: JNIEnvironment) throws -> JavaClassLoader! {
23+
JNI.shared.applicationClassLoader
24+
}
2125
}

0 commit comments

Comments
 (0)