From 6c4d9f7704f31e58b37d2c43020f8c18fd3f0a82 Mon Sep 17 00:00:00 2001 From: Iceman Date: Thu, 9 Apr 2026 09:16:56 +0900 Subject: [PATCH 1/3] Skip printing JNICache for non enum types. --- .../JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift index 233a95df..e3880c75 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift @@ -174,6 +174,10 @@ extension JNISwift2JavaGenerator { } private func printJNICache(_ printer: inout CodePrinter, _ type: ImportedNominalType) { + if type.cases.isEmpty { + return + } + printer.printBraceBlock("enum \(JNICaching.cacheName(for: type))") { printer in for enumCase in type.cases { guard let translatedCase = translatedEnumCase(for: enumCase) else { continue } From 3086055ccf6dfcadd852a028d49ba78935a2b00c Mon Sep 17 00:00:00 2001 From: Iceman Date: Thu, 9 Apr 2026 10:10:35 +0900 Subject: [PATCH 2/3] Skip printing for no associated value cases --- .../Sources/MySwiftLibrary/EnumWithValueCases.swift | 1 + Sources/JExtractSwiftLib/JNI/JNICaching.swift | 4 ++++ ...JNISwift2JavaGenerator+JavaBindingsPrinting.swift | 4 ++-- .../JNISwift2JavaGenerator+SwiftThunkPrinting.swift | 12 +++++++----- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/EnumWithValueCases.swift b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/EnumWithValueCases.swift index 1faa2c62..f3a7f45e 100644 --- a/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/EnumWithValueCases.swift +++ b/Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/EnumWithValueCases.swift @@ -14,4 +14,5 @@ public enum EnumWithValueCases { case firstCase(UInt) + case secondCase } diff --git a/Sources/JExtractSwiftLib/JNI/JNICaching.swift b/Sources/JExtractSwiftLib/JNI/JNICaching.swift index 446c6fc3..e2faeb8c 100644 --- a/Sources/JExtractSwiftLib/JNI/JNICaching.swift +++ b/Sources/JExtractSwiftLib/JNI/JNICaching.swift @@ -28,4 +28,8 @@ enum JNICaching { static func cacheMemberName(for enumCase: ImportedEnumCase) -> String { "\(enumCase.enumType.nominalTypeDecl.name.firstCharacterLowercased)\(enumCase.name.firstCharacterUppercased)Cache" } + + static func cacheMemberName(for translatedEnumCase: JNISwift2JavaGenerator.TranslatedEnumCase) -> String { + cacheMemberName(for: translatedEnumCase.original) + } } diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift index 05d4b5b9..fe13ca74 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift @@ -506,10 +506,10 @@ extension JNISwift2JavaGenerator { // Print record printer.printBraceBlock("public record \(caseName)(\(members.joined(separator: ", "))) implements Case") { printer in - let nativeParameters = zip(translatedCase.translatedValues, translatedCase.parameterConversions).flatMap { + let nativeParameters = zip(translatedCase.translatedValues, translatedCase.parameterConversions).map { value, conversion in - ["\(conversion.native.javaType) \(value.parameter.name)"] + "\(conversion.native.javaType) \(value.parameter.name)" } printer.print("record _NativeParameters(\(nativeParameters.joined(separator: ", "))) {}") diff --git a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift index e3880c75..4f0fb6c3 100644 --- a/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift +++ b/Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift @@ -174,15 +174,17 @@ extension JNISwift2JavaGenerator { } private func printJNICache(_ printer: inout CodePrinter, _ type: ImportedNominalType) { - if type.cases.isEmpty { + let targetCases = type.cases + .compactMap(translatedEnumCase(for:)) + .filter { !$0.translatedValues.isEmpty } + if targetCases.isEmpty { return } - + printer.printBraceBlock("enum \(JNICaching.cacheName(for: type))") { printer in - for enumCase in type.cases { - guard let translatedCase = translatedEnumCase(for: enumCase) else { continue } + for translatedCase in targetCases { printer.print( - "static let \(JNICaching.cacheMemberName(for: enumCase)) = \(renderEnumCaseCacheInit(translatedCase))" + "static let \(JNICaching.cacheMemberName(for: translatedCase)) = \(renderEnumCaseCacheInit(translatedCase))" ) } } From 09a1c3bbb2b0be061c76325801a3bb47594a9e84 Mon Sep 17 00:00:00 2001 From: Iceman Date: Thu, 9 Apr 2026 10:21:03 +0900 Subject: [PATCH 3/3] Update test --- Sources/JExtractSwiftLib/JNI/JNICaching.swift | 2 +- .../JExtractSwiftTests/JNI/JNIEnumTests.swift | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Sources/JExtractSwiftLib/JNI/JNICaching.swift b/Sources/JExtractSwiftLib/JNI/JNICaching.swift index e2faeb8c..07f56876 100644 --- a/Sources/JExtractSwiftLib/JNI/JNICaching.swift +++ b/Sources/JExtractSwiftLib/JNI/JNICaching.swift @@ -30,6 +30,6 @@ enum JNICaching { } static func cacheMemberName(for translatedEnumCase: JNISwift2JavaGenerator.TranslatedEnumCase) -> String { - cacheMemberName(for: translatedEnumCase.original) + cacheMemberName(for: translatedEnumCase.original) } } diff --git a/Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift b/Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift index e09c215c..153d1fa5 100644 --- a/Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift +++ b/Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift @@ -305,6 +305,12 @@ struct JNIEnumTests { .swift, detectChunkByInitialLines: 1, expectedChunks: [ + """ + enum _JNI_MyEnum { + static let myEnumSecondCache = _JNIMethodIDCache(className: "com/example/swift/MyEnum$Second$_NativeParameters", methods: [.init(name: "", signature: "(Ljava/lang/String;)V")]) + static let myEnumThirdCache = _JNIMethodIDCache(className: "com/example/swift/MyEnum$Third$_NativeParameters", methods: [.init(name: "", signature: "(JI)V")]) + } + """, """ @_cdecl("Java_com_example_swift_MyEnum__00024getAsSecond__J") public func Java_com_example_swift_MyEnum__00024getAsSecond__J(environment: UnsafeMutablePointer!, thisClass: jclass, selfPointer: jlong) -> jobject? { @@ -335,6 +341,36 @@ struct JNIEnumTests { return environment.interface.NewObjectA(environment, class$, constructorID$, newObjectArgs$) } """, + ], + notExpectedChunks: [ + "public func Java_com_example_swift_MyEnum__00024getAsFirst__J(" + ] + ) + } + + @Test + func nonGeneratesGetAsCase_swift() throws { + try assertOutput( + input: """ + public enum MyEnum { + case first + case second + } + """, + .jni, + .swift, + detectChunkByInitialLines: 1, + expectedChunks: [], + notExpectedChunks: [ + """ + enum _JNI_MyEnum + """, + """ + public func Java_com_example_swift_MyEnum__00024getAsFirst__J(" + """, + """ + public func Java_com_example_swift_MyEnum__00024getAsSecond__J( + """, ] ) }