diff --git a/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/NestedTypes.swift b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/NestedTypes.swift index e2b2633fb..372aa0be2 100644 --- a/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/NestedTypes.swift +++ b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/NestedTypes.swift @@ -50,4 +50,8 @@ public enum NamespaceEnum { public enum Nested { public static func something() {} } + + public struct NestedGenericType { + public var value: Int + } } diff --git a/Sources/JExtractSwiftLib/ImportedDecls.swift b/Sources/JExtractSwiftLib/ImportedDecls.swift index 3df8b932d..12becf550 100644 --- a/Sources/JExtractSwiftLib/ImportedDecls.swift +++ b/Sources/JExtractSwiftLib/ImportedDecls.swift @@ -167,7 +167,7 @@ package final class ImportedNominalType: ImportedDecl { } var swiftType: SwiftType { - .nominal(.init(nominalTypeDecl: swiftNominal)) + swiftNominal.asSwiftType } /// Structured Java-facing type name — "FishBox" for specialized, "Box" for base diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift index 0d5246dc1..4d92206e1 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift @@ -920,7 +920,7 @@ extension JNISwift2JavaGenerator { } private func openerProtocolName(for type: SwiftNominalTypeDeclaration) -> String { - "_\(swiftModuleName)_\(type.name)_opener" + "_\(swiftModuleName)_\(type.flatName)_opener" } private func printOpenerProtocol(_ printer: inout CodePrinter, _ type: ImportedNominalType) { @@ -969,7 +969,7 @@ extension JNISwift2JavaGenerator { } } printer.println() - printer.printBraceBlock("extension \(type.swiftNominal.name): \(protocolName)") { printer in + printer.printBraceBlock("extension \(type.swiftNominal.qualifiedName): \(protocolName)") { printer in for variable in type.variables { if variable.isStatic { continue } printFunctionDecl(&printer, decl: variable, skipMethodBody: false) diff --git a/Sources/JExtractSwiftLib/SwiftTypes/SwiftType.swift b/Sources/JExtractSwiftLib/SwiftTypes/SwiftType.swift index 95940a475..97e2bf5fa 100644 --- a/Sources/JExtractSwiftLib/SwiftTypes/SwiftType.swift +++ b/Sources/JExtractSwiftLib/SwiftTypes/SwiftType.swift @@ -589,3 +589,18 @@ enum TypeTranslationError: Error { /// Unknown nominal type. case unknown(TypeSyntax, file: StaticString = #file, line: Int = #line) } + +extension SwiftNominalTypeDeclaration { + var asSwiftNominalType: SwiftNominalType { + let genericArguments = genericParameters.map { SwiftType.genericParameter($0) } + return SwiftNominalType( + parent: parent?.asSwiftNominalType, + nominalTypeDecl: self, + genericArguments: genericArguments.isEmpty ? nil : genericArguments + ) + } + + var asSwiftType: SwiftType { + .nominal(asSwiftNominalType) + } +} diff --git a/Tests/JExtractSwiftTests/JNI/JNIGenericTypeTests.swift b/Tests/JExtractSwiftTests/JNI/JNIGenericTypeTests.swift index b0b3c4305..5f82b3bdf 100644 --- a/Tests/JExtractSwiftTests/JNI/JNIGenericTypeTests.swift +++ b/Tests/JExtractSwiftTests/JNI/JNIGenericTypeTests.swift @@ -121,7 +121,7 @@ struct JNIGenericTypeTests { static func _get_description(environment: UnsafeMutablePointer!, thisClass: jclass, selfPointer: jlong) -> jstring? { assert(selfPointer != 0, "selfPointer memory address was null") let selfPointerBits$ = Int(Int64(fromJNI: selfPointer, in: environment)) - let selfPointer$ = UnsafeMutablePointer(bitPattern: selfPointerBits$) + let selfPointer$ = UnsafeMutablePointer>(bitPattern: selfPointerBits$) guard let selfPointer$ else { fatalError("selfPointer memory address was null in call to \(#function)!") } @@ -239,7 +239,6 @@ struct JNIGenericTypeTests { ] ) - try assertOutput( input: input, .jni, @@ -253,6 +252,39 @@ struct JNIGenericTypeTests { ) } + @Test + func nestedGenericType() throws { + let input = + #""" + public enum Foo { + public struct Bar { + public func work() {} + } + } + """# + + try assertOutput( + input: input, + .jni, + .swift, + detectChunkByInitialLines: 1, + expectedChunks: [ + #""" + extension Foo.Bar: _SwiftModule_Foo_Bar_opener { + static func _work(environment: UnsafeMutablePointer!, thisClass: jclass, selfPointer: jlong) { + ... + let selfPointer$ = UnsafeMutablePointer>(bitPattern: selfPointerBits$) + guard let selfPointer$ else { + fatalError("selfPointer memory address was null in call to \(#function)!") + } + selfPointer$.pointee.work() + } + } + """# + ], + ) + } + @Test("Constrained extensions are ignored") func constrainedExtensionsAreIgnored() throws { let input =