Skip to content

Commit 7cc7359

Browse files
committed
Revert "make SwiftJava a dynamic lib and add class lookup fallbacks (swiftlang#493)"
This reverts commit 0685f55.
1 parent 0685f55 commit 7cc7359

File tree

18 files changed

+59
-99
lines changed

18 files changed

+59
-99
lines changed

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ let package = Package(
100100
// ==== SwiftJava (i.e. calling Java directly Swift utilities)
101101
.library(
102102
name: "SwiftJava",
103-
type: .dynamic,
104103
targets: ["SwiftJava"]
105104
),
106105

Samples/SwiftAndJavaJarSampleLib/Package.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ let package = Package(
6363
.target(
6464
name: "MySwiftLibrary",
6565
dependencies: [
66-
.product(name: "SwiftJava", package: "swift-java"),
67-
.product(name: "CSwiftJavaJNI", package: "swift-java"),
68-
.product(name: "SwiftRuntimeFunctions", package: "swift-java")
66+
.product(name: "SwiftRuntimeFunctions", package: "swift-java"),
6967
],
7068
exclude: [
7169
"swift-java.config",

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ extension FFMSwift2JavaGenerator {
195195
private static final boolean INITIALIZED_LIBS = initializeLibs();
196196
static boolean initializeLibs() {
197197
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE);
198-
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
199198
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS);
200199
System.loadLibrary(LIB_NAME);
201200
return true;
@@ -346,7 +345,6 @@ extension FFMSwift2JavaGenerator {
346345
private static SymbolLookup getSymbolLookup() {
347346
if (SwiftLibraries.AUTO_LOAD_LIBS) {
348347
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE);
349-
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
350348
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_RUNTIME_FUNCTIONS);
351349
System.loadLibrary(LIB_NAME);
352350
}

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ extension JNISwift2JavaGenerator {
9999
static final String LIB_NAME = "\(swiftModuleName)";
100100
101101
static {
102-
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
103102
System.loadLibrary(LIB_NAME);
104103
}
105104
"""
@@ -170,7 +169,6 @@ extension JNISwift2JavaGenerator {
170169
@SuppressWarnings("unused")
171170
private static final boolean INITIALIZED_LIBS = initializeLibs();
172171
static boolean initializeLibs() {
173-
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_JAVA);
174172
System.loadLibrary(LIB_NAME);
175173
return true;
176174
}

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ extension JNISwift2JavaGenerator {
162162
// If the underlying translated method requires
163163
// a SwiftArena, we pass in the global arena
164164
if translatedDecl.translatedFunctionSignature.requiresSwiftArena {
165-
upcallArguments.append("JavaSwiftArena.defaultAutoArena")
165+
upcallArguments.append("JNI.shared.defaultAutoArena")
166166
}
167167

168168
let tryClause = function.originalFunctionSignature.isThrowing ? "try " : ""
@@ -201,6 +201,8 @@ extension JNISwift2JavaGenerator {
201201
private func printGlobalSwiftThunkSources(_ printer: inout CodePrinter) throws {
202202
printHeader(&printer)
203203

204+
printJNIOnLoad(&printer)
205+
204206
for decl in analysis.importedGlobalFuncs {
205207
printSwiftFunctionThunk(&printer, decl)
206208
printer.println()
@@ -212,6 +214,18 @@ extension JNISwift2JavaGenerator {
212214
}
213215
}
214216

217+
private func printJNIOnLoad(_ printer: inout CodePrinter) {
218+
printer.print(
219+
"""
220+
@_cdecl("JNI_OnLoad")
221+
func JNI_OnLoad(javaVM: JavaVMPointer, reserved: UnsafeMutableRawPointer) -> jint {
222+
SwiftJavaRuntimeSupport._JNI_OnLoad(javaVM, reserved)
223+
return JNI_VERSION_1_6
224+
}
225+
"""
226+
)
227+
}
228+
215229
private func printNominalTypeThunks(_ printer: inout CodePrinter, _ type: ImportedNominalType) throws {
216230
printHeader(&printer)
217231

Sources/SwiftJava/AnyJavaObject.swift

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,41 +102,23 @@ extension AnyJavaObject {
102102
in environment: JNIEnvironment,
103103
_ body: (jclass) throws -> Result
104104
) throws -> Result {
105-
do {
106-
let resolvedClass = try environment.translatingJNIExceptions {
107-
environment.interface.FindClass(
108-
environment,
109-
fullJavaClassNameWithSlashes
110-
)
111-
}!
112-
return try body(resolvedClass)
113-
} catch {
114-
// If we are in a Java environment where we have loaded
115-
// SwiftJava dynamically, we have access to the application class loader
116-
// so lets try that as as a fallback
117-
if let applicationClassLoader = JNI.shared?.applicationClassLoader {
118-
return try _withJNIClassFromCustomClassLoader(
119-
applicationClassLoader,
120-
in: environment
121-
) { applicationLoadedClass in
122-
return try body(applicationLoadedClass)
123-
}
124-
} else {
125-
throw error
126-
}
127-
}
105+
let resolvedClass = try environment.translatingJNIExceptions {
106+
environment.interface.FindClass(
107+
environment,
108+
fullJavaClassNameWithSlashes
109+
)
110+
}!
111+
return try body(resolvedClass)
128112
}
129113

130114
/// Retrieve the Java class for this type using a specific class loader.
131115
private static func _withJNIClassFromCustomClassLoader<Result>(
132116
_ classLoader: JavaClassLoader,
133117
in environment: JNIEnvironment,
134-
_ body: (jclass) throws -> Result
118+
_ body: (jclass?) throws -> Result
135119
) throws -> Result {
136-
let resolvedClass = try classLoader.findClass(fullJavaClassName)
137-
// OK to force unwrap, as classLoader will throw ClassNotFoundException
138-
// if the class cannot be found.
139-
return try body(resolvedClass!.javaThis)
120+
let resolvedClass = try? classLoader.findClass(fullJavaClassName)
121+
return try body(resolvedClass?.javaThis)
140122
}
141123

142124
/// Retrieve the Java class for this type and execute body().
@@ -147,15 +129,16 @@ extension AnyJavaObject {
147129
) throws -> Result {
148130
if let AnyJavaObjectWithCustomClassLoader = self as? AnyJavaObjectWithCustomClassLoader.Type,
149131
let customClassLoader = try AnyJavaObjectWithCustomClassLoader.getJavaClassLoader(in: environment) {
150-
do {
151-
return try _withJNIClassFromCustomClassLoader(customClassLoader, in: environment) { clazz in
152-
return try body(clazz)
132+
try _withJNIClassFromCustomClassLoader(customClassLoader, in: environment) { clazz in
133+
guard let clazz else {
134+
// If the custom class loader did not find the class
135+
// let's look in the default class loader.
136+
return try _withJNIClassFromDefaultClassLoader(in: environment, body)
153137
}
154-
} catch {
155-
return try _withJNIClassFromDefaultClassLoader(in: environment, body)
138+
return try body(clazz)
156139
}
157140
} else {
158-
return try _withJNIClassFromDefaultClassLoader(in: environment, body)
141+
try _withJNIClassFromDefaultClassLoader(in: environment, body)
159142
}
160143
}
161144
}

Sources/SwiftJava/JVM/JavaVirtualMachine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ extension JavaVirtualMachine {
220220
/// TODO: If the use of the lock itself ends up being slow, we could
221221
/// use an atomic here instead because our access pattern is fairly
222222
/// simple.
223-
static let sharedJVM: LockedState<JavaVirtualMachine?> = .init(initialState: nil)
223+
private static let sharedJVM: LockedState<JavaVirtualMachine?> = .init(initialState: nil)
224224

225225
/// Access the shared Java Virtual Machine instance.
226226
///
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import SwiftJava
1516
import CSwiftJavaJNI
1617

1718
/// A type that represents the shared JNI environment
@@ -20,28 +21,36 @@ import CSwiftJavaJNI
2021
/// This is initialized when the `JNI_OnLoad` is triggered,
2122
/// which happens when you call `System.loadLibrary(...)`
2223
/// from Java.
23-
package final class JNI {
24+
public final class JNI {
2425
/// The shared JNI object, initialized by `JNI_OnLoad`
25-
///
26-
/// This may be `nil` in the case where `SwiftJava` is not loaded as a dynamic lib
27-
/// by the Java sources.
28-
package fileprivate(set) static var shared: JNI?
26+
public fileprivate(set) static var shared: JNI!
2927

3028
/// The default application class loader
31-
package let applicationClassLoader: JavaClassLoader
29+
public let applicationClassLoader: JavaClassLoader
30+
31+
/// The default auto arena of SwiftKitCore
32+
public let defaultAutoArena: JavaSwiftArena
3233

3334
init(fromVM javaVM: JavaVirtualMachine) {
34-
// Update the global JavaVM
35-
JavaVirtualMachine.sharedJVM.withLock {
36-
$0 = javaVM
37-
}
3835
let environment = try! javaVM.environment()
36+
3937
self.applicationClassLoader = try! JavaClass<JavaThread>(environment: environment).currentThread().getContextClassLoader()
38+
39+
// Find global arena
40+
let swiftMemoryClass = environment.interface.FindClass(environment, "org/swift/swiftkit/core/SwiftMemoryManagement")!
41+
let arenaFieldID = environment.interface.GetStaticFieldID(
42+
environment,
43+
swiftMemoryClass,
44+
"DEFAULT_SWIFT_JAVA_AUTO_ARENA",
45+
JavaSwiftArena.mangledName
46+
)
47+
let localObject = environment.interface.GetStaticObjectField(environment, swiftMemoryClass, arenaFieldID)!
48+
self.defaultAutoArena = JavaSwiftArena(javaThis: localObject, environment: environment)
49+
environment.interface.DeleteLocalRef(environment, localObject)
4050
}
4151
}
4252

43-
@_cdecl("JNI_OnLoad")
44-
func SwiftJava_JNI_OnLoad(javaVM: JavaVMPointer, reserved: UnsafeMutableRawPointer) -> jint {
53+
// Called by generated code, and not automatically by Java.
54+
public func _JNI_OnLoad(_ javaVM: JavaVMPointer, _ reserved: UnsafeMutableRawPointer) {
4555
JNI.shared = JNI(fromVM: JavaVirtualMachine(adoptingJVM: javaVM))
46-
return JNI_VERSION_1_6
4756
}

Sources/SwiftJavaRuntimeSupport/_JNIMethodIDCache.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public final class _JNIMethodIDCache: Sendable {
5555
// Clear any ClassNotFound exceptions from FindClass
5656
environment.interface.ExceptionClear(environment)
5757

58-
// OK to force unwrap, we are in a jextract environment.
59-
if let javaClass = try? JNI.shared!.applicationClassLoader.loadClass(
58+
if let javaClass = try? JNI.shared.applicationClassLoader.loadClass(
6059
className.replacingOccurrences(of: "/", with: ".")
6160
) {
6261
clazz = javaClass.javaThis

Sources/SwiftJavaRuntimeSupport/generated/JavaJNISwiftInstance.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public struct JavaJNISwiftInstance: AnyJavaObjectWithCustomClassLoader {
2020
public func memoryAddress() -> Int64
2121

2222
public static func getJavaClassLoader(in environment: JNIEnvironment) throws -> JavaClassLoader! {
23-
// OK to force unwrap, we are in a jextract environment.
24-
JNI.shared!.applicationClassLoader
23+
JNI.shared.applicationClassLoader
2524
}
2625
}

0 commit comments

Comments
 (0)