Skip to content

Commit 3de3079

Browse files
committed
add tests and docs
1 parent 02f6a1d commit 3de3079

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

Sources/JExtractSwiftLib/Swift2JavaTranslator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class Swift2JavaTranslator {
4040
var inputs: [Input] = []
4141

4242
/// A list of used Swift class names that live in dependencies
43-
var dependenciesClasses: [String] = []
43+
package var dependenciesClasses: [String] = []
4444

4545
// ==== Output state
4646

Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ SwiftJava's `swift-java jextract` tool automates generating Java bindings from S
6565
| Protocols: `protocol`, existential parameters `any Collection` |||
6666
| Optional types: `Int?`, `AnyObject?` |||
6767
| Primitive types: `Bool`, `Int`, `Int8`, `Int16`, `Int32`, `Int64`, `Float`, `Double` |||
68+
| Parameters: JavaKit wrapped types `JavaLong`, `JavaInteger` |||
69+
| Return values: JavaKit wrapped types `JavaLong`, `JavaInteger` |||
6870
| Unsigned primitive types: `UInt`, `UInt8`, `UInt16`, `UInt32`, `UInt64` |||
6971
| String (with copying data) |||
7072
| Variadic parameters: `T...` |||

Tests/JExtractSwiftTests/Asserts/TextAssertions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func assertOutput(
2929
_ renderKind: RenderKind,
3030
swiftModuleName: String = "SwiftModule",
3131
detectChunkByInitialLines _detectChunkByInitialLines: Int = 4,
32+
javaClassLookupTable: [String: String] = [:],
3233
expectedChunks: [String],
3334
fileID: String = #fileID,
3435
filePath: String = #filePath,
@@ -38,6 +39,7 @@ func assertOutput(
3839
var config = Configuration()
3940
config.swiftModule = swiftModuleName
4041
let translator = Swift2JavaTranslator(config: config)
42+
translator.dependenciesClasses = Array(javaClassLookupTable.keys)
4143

4244
try! translator.analyze(file: "/fake/Fake.swiftinterface", text: input)
4345

@@ -64,7 +66,8 @@ func assertOutput(
6466
translator: translator,
6567
javaPackage: "com.example.swift",
6668
swiftOutputDirectory: "/fake",
67-
javaOutputDirectory: "/fake"
69+
javaOutputDirectory: "/fake",
70+
javaClassLookupTable: javaClassLookupTable
6871
)
6972

7073
switch renderKind {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import JExtractSwiftLib
16+
import Testing
17+
18+
@Suite
19+
struct JNIJavaKitTests {
20+
let source =
21+
"""
22+
public func function(javaLong: JavaLong, javaInteger: JavaInteger, int: Int64) {}
23+
"""
24+
25+
let classLookupTable = [
26+
"JavaLong": "java.lang.Long",
27+
"JavaInteger": "java.lang.Integer"
28+
]
29+
30+
@Test
31+
func function_javaBindings() throws {
32+
try assertOutput(
33+
input: source,
34+
.jni,
35+
.java,
36+
javaClassLookupTable: classLookupTable,
37+
expectedChunks: [
38+
"""
39+
/**
40+
* Downcall to Swift:
41+
* {@snippet lang=swift :
42+
* public func function(javaLong: JavaLong, javaInteger: JavaInteger, int: Int64)
43+
* }
44+
*/
45+
public static void function(java.lang.Long javaLong, java.lang.Integer javaInteger, long int) {
46+
SwiftModule.$function(javaLong, javaInteger, int);
47+
}
48+
""",
49+
"""
50+
private static native void $function(java.lang.Long javaLong, java.lang.Integer javaInteger, long int);
51+
"""
52+
]
53+
)
54+
}
55+
56+
@Test
57+
func function_swiftThunks() throws {
58+
try assertOutput(
59+
input: source,
60+
.jni,
61+
.swift,
62+
detectChunkByInitialLines: 1,
63+
javaClassLookupTable: classLookupTable,
64+
expectedChunks: [
65+
"""
66+
@_cdecl("Java_com_example_swift_SwiftModule__00024function__Ljava_lang_Long_2Ljava_lang_Integer_2J")
67+
func Java_com_example_swift_SwiftModule__00024function__Ljava_lang_Long_2Ljava_lang_Integer_2J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, javaLong: jobject, javaInteger: jobject, int: jlong) {
68+
SwiftModule.function(javaLong: JavaLong(javaThis: javaLong, environment: environment!), javaInteger: JavaInteger(javaThis: javaInteger, environment: environment!), int: Int64(fromJNI: int, in: environment!))
69+
}
70+
"""
71+
]
72+
)
73+
}
74+
}

0 commit comments

Comments
 (0)