File tree Expand file tree Collapse file tree
Samples/JavaKitSampleApp/Tests/JavaKitExampleTests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,15 +21,6 @@ import Testing
2121@ Suite
2222struct JavaKitMethodCalledInitTests {
2323
24- @ Test
25- func test ( ) throws {
26- let jvm = try JavaKitSampleJVM . shared ( )
27- let env = try jvm . environment ( )
28-
29- let hello = HelloSwift ( environment : env )
30-
31- let reply = hello . `init` ( 128 )
32- #expect ( reply == 128 )
33- }
24+
3425
3526}
Original file line number Diff line number Diff line change @@ -21,10 +21,11 @@ import Testing
2121@ Suite
2222struct ManglingTests {
2323
24+ let jvm = try JavaKitSampleJVM . shared
25+
2426 @ Test
2527 func methodMangling ( ) throws {
26- let jvm = try JavaKitSampleJVM . shared ( )
27- let env = try! jvm . environment ( )
28+ let env = try jvm . environment ( )
2829
2930 let helper = ThreadSafeHelperClass ( environment : env )
3031
@@ -49,4 +50,14 @@ struct ManglingTests {
4950 #expect ( #"Optional(21)"# == String ( describing : longOpt ) )
5051 }
5152
53+ @ Test
54+ func methodNamedInit ( ) throws {
55+ let env = try jvm . environment ( )
56+
57+ let hello = HelloSwift ( environment : env )
58+
59+ let reply = hello . `init` ( 128 )
60+ #expect ( reply == 128 )
61+ }
62+
5263}
Original file line number Diff line number Diff line change @@ -21,15 +21,15 @@ import JavaUtilFunction
2121/// classes defined in this JavaKitExample project
2222struct JavaKitSampleJVM {
2323
24- static func shared ( ) throws -> JavaVirtualMachine {
25- try JavaVirtualMachine . shared (
24+ static var shared : JavaVirtualMachine = {
25+ try! JavaVirtualMachine . shared (
2626 classpath : [
2727 " .build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java " ,
2828 ] ,
2929 vmOptions : [
3030 KnownJavaVMOptions . javaLibraryPath ( " .build/ \( SwiftPlatform . debugOrRelease ) / " ) ,
3131 ]
3232 )
33- }
33+ } ( )
3434
3535}
Original file line number Diff line number Diff line change @@ -28,15 +28,28 @@ package final class JNI {
2828 package fileprivate( set) static var shared : JNI ?
2929
3030 /// The default application class loader
31- package let applicationClassLoader : JavaClassLoader
31+ package let applicationClassLoader : JavaClassLoader ?
3232
3333 init ( fromVM javaVM : JavaVirtualMachine ) {
3434 // Update the global JavaVM
3535 JavaVirtualMachine . sharedJVM . withLock {
3636 $0 = javaVM
3737 }
3838 let environment = try! javaVM . environment ( )
39- self . applicationClassLoader = try! JavaClass < JavaThread > ( environment : environment ) . currentThread ( ) . getContextClassLoader ( )
39+ do {
40+ let clazz = try JavaClass < JavaThread > ( environment : environment )
41+ guard let thread : JavaThread = clazz . currentThread ( ) else {
42+ applicationClassLoader = nil
43+ return
44+ }
45+ guard let cl = thread . getContextClassLoader ( ) else {
46+ applicationClassLoader = nil
47+ return
48+ }
49+ self . applicationClassLoader = cl
50+ } catch {
51+ fatalError ( " Failed to get current thread's ContextClassLoader: \( error ) " )
52+ }
4053 }
4154}
4255
You can’t perform that action at this time.
0 commit comments