Skip to content

Commit 99899cc

Browse files
committed
remember global ref
1 parent 963f23f commit 99899cc

File tree

4 files changed

+5
-13
lines changed

4 files changed

+5
-13
lines changed

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ extension JNISwift2JavaGenerator {
236236
let methodSignature = MethodSignature(resultType: .void, parameterTypes: enumCase.parameterConversions.map(\.native.javaType))
237237
let methods = #"[.init(name: "<init>", signature: "\#(methodSignature.mangledName)")]"#
238238

239-
return #"_JNIMethodIDCache(environment: try! JavaVirtualMachine.shared().environment(), className: "\#(nativeParametersClassName)", methods: \#(methods), isSystemClass: false)"#
239+
return #"_JNIMethodIDCache(className: "\#(nativeParametersClassName)", methods: \#(methods), isSystemClass: false)"#
240240
}
241241

242242
private func printEnumGetAsCaseThunk(

Sources/SwiftJavaRuntimeSupport/DefaultCaches.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ extension _JNIMethodIDCache {
2727
)
2828

2929
private static let cache = _JNIMethodIDCache(
30-
environment: try! JavaVirtualMachine.shared().environment(),
3130
className: "java/util/concurrent/CompletableFuture",
3231
methods: [completeMethod, completeExceptionallyMethod],
3332
isSystemClass: true
@@ -52,7 +51,6 @@ extension _JNIMethodIDCache {
5251
private static let messageConstructor = Method(name: "<init>", signature: "(Ljava/lang/String;)V")
5352

5453
private static let cache = _JNIMethodIDCache(
55-
environment: try! JavaVirtualMachine.shared().environment(),
5654
className: "java/lang/Exception",
5755
methods: [messageConstructor],
5856
isSystemClass: true

Sources/SwiftJavaRuntimeSupport/_JNIBoxedConversions.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,46 @@ public enum _JNIBoxedConversions {
2626
private static let doubleMethod = _JNIMethodIDCache.Method(name: "valueOf", signature: "(D)Ljava/lang/Double;", isStatic: true)
2727

2828
private static let booleanCache = _JNIMethodIDCache(
29-
environment: try! JavaVirtualMachine.shared().environment(),
3029
className: "java/lang/Boolean",
3130
methods: [booleanMethod],
3231
isSystemClass: true
3332
)
3433

3534
private static let byteCache = _JNIMethodIDCache(
36-
environment: try! JavaVirtualMachine.shared().environment(),
3735
className: "java/lang/Byte",
3836
methods: [byteMethod],
3937
isSystemClass: true
4038
)
4139
private static let charCache = _JNIMethodIDCache(
42-
environment: try! JavaVirtualMachine.shared().environment(),
4340
className: "java/lang/Character",
4441
methods: [charMethod],
4542
isSystemClass: true
4643
)
4744

4845
private static let shortCache = _JNIMethodIDCache(
49-
environment: try! JavaVirtualMachine.shared().environment(),
5046
className: "java/lang/Short",
5147
methods: [shortMethod],
5248
isSystemClass: true
5349
)
5450
private static let intCache = _JNIMethodIDCache(
55-
environment: try! JavaVirtualMachine.shared().environment(),
5651
className: "java/lang/Integer",
5752
methods: [intMethod],
5853
isSystemClass: true
5954
)
6055

6156
private static let longCache = _JNIMethodIDCache(
62-
environment: try! JavaVirtualMachine.shared().environment(),
6357
className: "java/lang/Long",
6458
methods: [longMethod],
6559
isSystemClass: true
6660
)
6761

6862
private static let floatCache = _JNIMethodIDCache(
69-
environment: try! JavaVirtualMachine.shared().environment(),
7063
className: "java/lang/Float",
7164
methods: [floatMethod],
7265
isSystemClass: true
7366
)
7467

7568
private static let doubleCache = _JNIMethodIDCache(
76-
environment: try! JavaVirtualMachine.shared().environment(),
7769
className: "java/lang/Double",
7870
methods: [doubleMethod],
7971
isSystemClass: true

Sources/SwiftJavaRuntimeSupport/_JNIMethodIDCache.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ public final class _JNIMethodIDCache: Sendable {
4444
/// This is to make sure that the underlying reference remains valid
4545
nonisolated(unsafe) private let javaObjectHolder: JavaObjectHolder?
4646

47-
public init(environment: UnsafeMutablePointer<JNIEnv?>!, className: String, methods: [Method], isSystemClass: Bool) {
47+
public init(className: String, methods: [Method], isSystemClass: Bool) {
48+
let environment = try! JavaVirtualMachine.shared().environment()
49+
4850
let clazz: jobject
4951
if isSystemClass {
5052
guard let jniClass = environment.interface.FindClass(environment, className) else {
5153
fatalError("Class \(className) could not be found!")
5254
}
53-
clazz = jniClass
55+
clazz = environment.interface.NewGlobalRef(environment, jniClass)!
5456
self.javaObjectHolder = nil
5557
} else {
5658
guard let javaClass = try? JNI.shared.applicationClassLoader.loadClass(className.replacingOccurrences(of: "/", with: ".")) else {

0 commit comments

Comments
 (0)