Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

public enum EnumWithValueCases {
case firstCase(UInt)
case secondCase
}
4 changes: 4 additions & 0 deletions Sources/JExtractSwiftLib/JNI/JNICaching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: ", "))) {}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,17 @@ extension JNISwift2JavaGenerator {
}

private func printJNICache(_ printer: inout CodePrinter, _ type: ImportedNominalType) {
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))"
)
}
}
Expand Down
36 changes: 36 additions & 0 deletions Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<init>", signature: "(Ljava/lang/String;)V")])
static let myEnumThirdCache = _JNIMethodIDCache(className: "com/example/swift/MyEnum$Third$_NativeParameters", methods: [.init(name: "<init>", signature: "(JI)V")])
}
""",
"""
@_cdecl("Java_com_example_swift_MyEnum__00024getAsSecond__J")
public func Java_com_example_swift_MyEnum__00024getAsSecond__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) -> jobject? {
Expand Down Expand Up @@ -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(
""",
]
)
}
Expand Down
Loading