-
Notifications
You must be signed in to change notification settings - Fork 67
[jextract/jni] Add support for importing nested types #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/NestedTypes.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 class A { | ||
| public init() {} | ||
|
|
||
| public class B { | ||
| public init() {} | ||
|
|
||
| public struct C { | ||
| public init() {} | ||
|
|
||
| public func g(a: A, b: B, bbc: BB.C) {} | ||
| } | ||
| } | ||
|
|
||
| public class BB { | ||
| public init() {} | ||
|
|
||
| public struct C { | ||
| public init() {} | ||
| } | ||
| } | ||
|
|
||
| public func f(a: A, b: A.B, c: A.B.C, bb: BB, bbc: BB.C) {} | ||
| } | ||
|
|
||
| public enum NestedEnum { | ||
| case one(OneStruct) | ||
|
|
||
| public struct OneStruct { | ||
| public init() {} | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/NestedTypesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2024 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 org.junit.jupiter.api.Test; | ||
| import org.swift.swiftkit.core.SwiftArena; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class NestedTypesTest { | ||
| @Test | ||
| void testClassesAndStructs() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| var a = A.init(arena); | ||
| var b = A.B.init(arena); | ||
| var c = A.B.C.init(arena); | ||
| var bb = A.BB.init(arena); | ||
| var abbc = A.BB.C.init(arena); | ||
|
|
||
| a.f(a, b, c, bb, abbc); | ||
| c.g(a, b, abbc); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testStructInEnum() { | ||
| try (var arena = SwiftArena.ofConfined()) { | ||
| var obj = NestedEnum.one(NestedEnum.OneStruct.init(arena), arena); | ||
| var one = obj.getAsOne(arena); | ||
| assertTrue(one.isPresent()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 JExtractSwiftLib | ||
| import Testing | ||
|
|
||
| @Suite | ||
| struct JNINestedTypesTests { | ||
| let source1 = """ | ||
| public class A { | ||
| public class B { | ||
| public func g(c: C) {} | ||
|
|
||
| public struct C { | ||
| public func h(b: B) {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public func f(a: A, b: A.B, c: A.B.C) {} | ||
| """ | ||
|
|
||
| @Test("Import: class and struct A.B.C (Java)") | ||
| func nestedClassesAndStructs_java() throws { | ||
| try assertOutput( | ||
| input: source1, | ||
| .jni, .java, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| public final class A implements JNISwiftInstance { | ||
| ... | ||
| public static final class B implements JNISwiftInstance { | ||
| ... | ||
| public static final class C implements JNISwiftInstance { | ||
| ... | ||
| public void h(A.B b) { | ||
| ... | ||
| } | ||
| ... | ||
| public void g(A.B.C c) { | ||
| ... | ||
| } | ||
| ... | ||
| } | ||
| """, | ||
| """ | ||
| public static void f(A a, A.B b, A.B.C c) { | ||
| ... | ||
| } | ||
| ... | ||
| """ | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| @Test("Import: class and struct A.B.C (Swift)") | ||
| func nestedClassesAndStructs_swift() throws { | ||
| try assertOutput( | ||
| input: source1, | ||
| .jni, .swift, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| @_cdecl("Java_com_example_swift_A__00024destroy__J") | ||
| func Java_com_example_swift_A__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) { | ||
| ... | ||
| } | ||
| """, | ||
| """ | ||
| @_cdecl("Java_com_example_swift_A_00024B__00024destroy__J") | ||
| func Java_com_example_swift_A_00024B__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) { | ||
| ... | ||
| } | ||
| """, | ||
| """ | ||
| @_cdecl("Java_com_example_swift_A_00024B__00024destroy__J") | ||
| func Java_com_example_swift_A_00024B__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) { | ||
| ... | ||
| } | ||
| """, | ||
| """ | ||
| @_cdecl("Java_com_example_swift_A_00024B_00024C__00024h__JJ") | ||
| func Java_com_example_swift_A_00024B_00024C__00024h__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, b: jlong, self: jlong) { | ||
| ... | ||
| } | ||
| """ | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| @Test("Import: nested in enum") | ||
| func nestedEnums_java() throws { | ||
| try assertOutput( | ||
| input: """ | ||
| public enum MyError { | ||
| case text(TextMessage) | ||
|
|
||
| public struct TextMessage {} | ||
| } | ||
|
|
||
| public func f(text: MyError.TextMessage) {} | ||
| """, | ||
| .jni, .java, | ||
| detectChunkByInitialLines: 1, | ||
| expectedChunks: [ | ||
| """ | ||
| public final class MyError implements JNISwiftInstance { | ||
| ... | ||
| public static final class TextMessage implements JNISwiftInstance { | ||
| ... | ||
| } | ||
| ... | ||
| public static MyError text(MyError.TextMessage arg0, SwiftArena swiftArena$) { | ||
| ... | ||
| } | ||
| """, | ||
| """ | ||
| public static void f(MyError.TextMessage text) { | ||
| ... | ||
| } | ||
| """ | ||
| ] | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.