diff --git a/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/Throw.swift b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/Throw.swift new file mode 100644 index 00000000..3a6a56c7 --- /dev/null +++ b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/Throw.swift @@ -0,0 +1,19 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftJava + +public func throwString(input: String) throws -> String { + return input +} diff --git a/Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/ThrowTest.java b/Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/ThrowTest.java new file mode 100644 index 00000000..666b618b --- /dev/null +++ b/Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/ThrowTest.java @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +package com.example.swift; + +import com.example.swift.MySwiftLibrary; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ThrowTest { + @Test + void throwString() throws Exception { + String result = MySwiftLibrary.throwString("hey"); + assertEquals("hey", result); + } +} diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift index 78a4fb6c..0447a73d 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift @@ -657,12 +657,14 @@ extension JNISwift2JavaGenerator { } private func dummyReturn(for nativeSignature: NativeFunctionSignature) -> String { - return if nativeSignature.result.javaType.isVoid { - "return" - } else { - // We assume it is something that implements JavaValue - "return \(nativeSignature.result.javaType.swiftTypeName(resolver: { _ in "" })).jniPlaceholderValue" - } + return if nativeSignature.result.javaType.isVoid { + "return" + } else if nativeSignature.result.javaType.isString { + "return String.jniPlaceholderValue" + } else { + // We assume it is something that implements JavaValue + "return \(nativeSignature.result.javaType.swiftTypeName(resolver: { _ in "" })).jniPlaceholderValue" + } } private func printCDecl( diff --git a/Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift b/Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift index 047399f2..b1f0f2b8 100644 --- a/Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift +++ b/Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift @@ -30,6 +30,7 @@ struct JNIModuleTests { let globalMethodThrowing = """ public func methodA() throws public func methodB() throws -> Int64 + public func methodC() throws -> String """ @Test @@ -256,6 +257,17 @@ struct JNIModuleTests { } } """, + """ + @_cdecl("Java_com_example_swift_SwiftModule__00024methodC__") + public func Java_com_example_swift_SwiftModule__00024methodC__(environment: UnsafeMutablePointer!, thisClass: jclass) -> jstring? { + do { + return try SwiftModule.methodC().getJNIValue(in: environment) + } catch { + environment.throwAsException(error) + return String.jniPlaceholderValue + } + } + """ ] ) }