Skip to content

Commit 49ba299

Browse files
committed
fallback
1 parent e885144 commit 49ba299

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
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
}

0 commit comments

Comments
 (0)