Skip to content

Commit

Permalink
Merge pull request swiftlang#158 from PADL/lhoward/155
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso authored Nov 7, 2024
2 parents 651cc89 + 4fa32a1 commit 79237aa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/JavaKit/BridgedValues/JavaValue+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ extension String: JavaValue {
}

public init(fromJNI value: JNIType, in environment: JNIEnvironment) {
guard let value else {
self.init()
return
}
let cString = environment.interface.GetStringUTFChars(environment, value, nil)!
defer { environment.interface.ReleaseStringUTFChars(environment, value, cString) }
self = String(cString: cString)
Expand Down
6 changes: 6 additions & 0 deletions Tests/JavaKitTests/BasicRuntimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class BasicRuntimeTests: XCTestCase {
XCTAssertEqual(String(describing: error), "org/swift/javakit/Nonexistent")
}
}

func testNullJavaStringConversion() throws {
let environment = try jvm.environment()
let nullString = String(fromJNI: nil, in: environment)
XCTAssertEqual(nullString, "")
}
}

@JavaClass("org.swift.javakit.Nonexistent")
Expand Down

0 comments on commit 79237aa

Please sign in to comment.