Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct JNIModuleTests {
let globalMethodThrowing = """
public func methodA() throws
public func methodB() throws -> Int64
public func methodC() throws -> String
"""

@Test
Expand Down Expand Up @@ -256,6 +257,17 @@ struct JNIModuleTests {
}
}
""",
"""
@_cdecl("Java_com_example_swift_SwiftModule__00024methodC__")
public func Java_com_example_swift_SwiftModule__00024methodC__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> jstring? {
do {
return try SwiftModule.methodC().getJNIValue(in: environment)
} catch {
environment.throwAsException(error)
return String.jniPlaceholderValue
}
}
"""
]
)
}
Expand Down
Loading