Skip to content

Commit a66030e

Browse files
committed
fix paret lookup and add more tests
1 parent 4b0cb13 commit a66030e

File tree

6 files changed

+159
-32
lines changed

6 files changed

+159
-32
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 SwiftJava
16+
17+
public class A {
18+
public init() {}
19+
20+
public class B {
21+
public init() {}
22+
23+
public struct C {
24+
public init() {}
25+
26+
public func g(a: A, b: B, bbc: BB.C) {}
27+
}
28+
}
29+
30+
public class BB {
31+
public init() {}
32+
33+
public struct C {
34+
public init() {}
35+
}
36+
}
37+
38+
public func f(a: A, b: A.B, c: A.B.C, bb: BB, bbc: BB.C) {}
39+
}
40+
41+
public enum NestedEnum {
42+
case one(OneStruct)
43+
44+
public struct OneStruct {
45+
public init() {}
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 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+
package com.example.swift;
16+
17+
import org.junit.jupiter.api.Test;
18+
import org.swift.swiftkit.core.SwiftArena;
19+
20+
import static org.junit.jupiter.api.Assertions.*;
21+
22+
public class NestedTypesTest {
23+
@Test
24+
void testClassesAndStructs() {
25+
try (var arena = SwiftArena.ofConfined()) {
26+
var a = A.init(arena);
27+
var b = A.B.init(arena);
28+
var c = A.B.C.init(arena);
29+
var bb = A.BB.init(arena);
30+
var abbc = A.BB.C.init(arena);
31+
32+
a.f(a, b, c, bb, abbc);
33+
c.g(a, b, abbc);
34+
}
35+
}
36+
37+
@Test
38+
void testStructInEnum() {
39+
try (var arena = SwiftArena.ofConfined()) {
40+
var obj = NestedEnum.one(NestedEnum.OneStruct.init(arena), arena);
41+
var one = obj.getAsOne(arena);
42+
assertTrue(one.isPresent());
43+
}
44+
}
45+
}

Sources/JExtractSwiftLib/JNI/JNICaching.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
enum JNICaching {
1616
static func cacheName(for type: ImportedNominalType) -> String {
17-
cacheName(for: type.swiftNominal.name)
17+
cacheName(for: type.swiftNominal.qualifiedName)
1818
}
1919

2020
static func cacheName(for type: SwiftNominalType) -> String {
21-
cacheName(for: type.nominalTypeDecl.name)
21+
cacheName(for: type.nominalTypeDecl.qualifiedName)
2222
}
2323

24-
private static func cacheName(for name: String) -> String {
25-
"_JNI_\(name)"
24+
private static func cacheName(for qualifiedName: String) -> String {
25+
"_JNI_\(qualifiedName.replacingOccurrences(of: ".", with: "_"))"
2626
}
2727

2828
static func cacheMemberName(for enumCase: ImportedEnumCase) -> String {

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ extension JNISwift2JavaGenerator {
430430
let cName =
431431
"Java_"
432432
+ self.javaPackage.replacingOccurrences(of: ".", with: "_")
433-
+ "_\(parentName.escapedJNIIdentifier)_"
433+
+ "_\(parentName.replacingOccurrences(of: ".", with: "$").escapedJNIIdentifier)_"
434434
+ javaMethodName.escapedJNIIdentifier
435435
+ "__"
436436
+ jniSignature.escapedJNIIdentifier
@@ -474,7 +474,7 @@ extension JNISwift2JavaGenerator {
474474
printCDecl(
475475
&printer,
476476
javaMethodName: "$typeMetadataAddressDowncall",
477-
parentName: type.swiftNominal.name,
477+
parentName: type.swiftNominal.qualifiedName,
478478
parameters: [],
479479
resultType: .long
480480
) { printer in
@@ -493,7 +493,7 @@ extension JNISwift2JavaGenerator {
493493
printCDecl(
494494
&printer,
495495
javaMethodName: "$destroy",
496-
parentName: type.swiftNominal.name,
496+
parentName: type.swiftNominal.qualifiedName,
497497
parameters: [
498498
selfPointerParam
499499
],

Sources/JExtractSwiftLib/SwiftTypes/SwiftTypeLookupContext.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,9 @@ class SwiftTypeLookupContext {
120120
/// Create a nominal type declaration instance for the specified syntax node.
121121
private func nominalTypeDeclaration(for node: NominalTypeDeclSyntaxNode, sourceFilePath: String) throws -> SwiftNominalTypeDeclaration {
122122

123-
if let symbolTableDeclaration = try self.symbolTable.lookupType(
124-
node.name.identifier!.name,
125-
parent: node.parent.flatMap {
126-
if let asNominalType = $0 as? NominalTypeDeclSyntaxNode {
127-
return try self.nominalTypeDeclaration(for: asNominalType, sourceFilePath: "FIXME.swift")
128-
}
129-
return nil
130-
}
123+
if let symbolTableDeclaration = self.symbolTable.lookupType(
124+
node.name.text,
125+
parent: try parentTypeDecl(for: node)
131126
) {
132127
return symbolTableDeclaration
133128
}

Tests/JExtractSwiftTests/JNI/JNINestedTypesTests.swift

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ import Testing
1717

1818
@Suite
1919
struct JNINestedTypesTests {
20-
@Test("Import: class and struct A.B.C")
20+
let source1 = """
21+
public class A {
22+
public class B {
23+
public func g(c: C) {}
24+
25+
public struct C {
26+
public func h(b: B) {}
27+
}
28+
}
29+
}
30+
31+
public func f(a: A, b: A.B, c: A.B.C) {}
32+
"""
33+
34+
@Test("Import: class and struct A.B.C (Java)")
2135
func nestedClassesAndStructs_java() throws {
2236
try assertOutput(
23-
input: """
24-
public class A {
25-
public class B {
26-
public func g(c: B) {}
27-
28-
public struct C {
29-
30-
}
31-
}
32-
}
33-
34-
public func f(a: A, b: A.B, c: A.B.C) {}
35-
""",
37+
input: source1,
3638
.jni, .java,
3739
detectChunkByInitialLines: 1,
3840
expectedChunks: [
@@ -42,19 +44,57 @@ struct JNINestedTypesTests {
4244
public static final class B implements JNISwiftInstance {
4345
...
4446
public static final class C implements JNISwiftInstance {
45-
...
47+
...
48+
public void h(A.B b) {
49+
...
4650
}
4751
...
48-
public static void g(A.B.C c) {
52+
public void g(A.B.C c) {
4953
...
5054
}
51-
...
55+
...
5256
}
5357
""",
5458
"""
5559
public static void f(A a, A.B b, A.B.C c) {
5660
...
5761
}
62+
...
63+
"""
64+
]
65+
)
66+
}
67+
68+
@Test("Import: class and struct A.B.C (Swift)")
69+
func nestedClassesAndStructs_swift() throws {
70+
try assertOutput(
71+
input: source1,
72+
.jni, .swift,
73+
detectChunkByInitialLines: 1,
74+
expectedChunks: [
75+
"""
76+
@_cdecl("Java_com_example_swift_A__00024destroy__J")
77+
func Java_com_example_swift_A__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
78+
...
79+
}
80+
""",
81+
"""
82+
@_cdecl("Java_com_example_swift_A_00024B__00024destroy__J")
83+
func Java_com_example_swift_A_00024B__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
84+
...
85+
}
86+
""",
87+
"""
88+
@_cdecl("Java_com_example_swift_A_00024B__00024destroy__J")
89+
func Java_com_example_swift_A_00024B__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
90+
...
91+
}
92+
""",
93+
"""
94+
@_cdecl("Java_com_example_swift_A_00024B_00024C__00024h__JJ")
95+
func Java_com_example_swift_A_00024B_00024C__00024h__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, b: jlong, self: jlong) {
96+
...
97+
}
5898
"""
5999
]
60100
)

0 commit comments

Comments
 (0)