Dev: fix typeName compatibility with AnyCodable

This commit is contained in:
Andrew Druk 2017-12-26 18:04:54 +02:00
parent 0d15cf94df
commit 3b4391a193
3 changed files with 9 additions and 5 deletions

View File

@ -5,8 +5,8 @@
import Foundation import Foundation
import java_swift import java_swift
public typealias JavaDecodableClosure = (jobject) throws -> Decodable
public typealias JavaEncodableClosure = (Encodable) throws -> jobject public typealias JavaEncodableClosure = (Encodable) throws -> jobject
public typealias JavaDecodableClosure = (jobject) throws -> Decodable
public struct JavaCoderConfig { public struct JavaCoderConfig {
@ -24,7 +24,7 @@ public struct JavaCoderConfig {
defer { defer {
closuresLock.unlock() closuresLock.unlock()
} }
let typeName = String(reflecting: type) let typeName = String(describing: type)
NSLog("JavaCoderConfig register: \(typeName)") NSLog("JavaCoderConfig register: \(typeName)")
codableClassNames[typeName] = javaClassname codableClassNames[typeName] = javaClassname
encodableClosures[typeName] = encodableClosure encodableClosures[typeName] = encodableClosure

View File

@ -605,7 +605,7 @@ fileprivate class JavaAnyCodableContainer<K : CodingKey> : KeyedDecodingContaine
extension JavaDecoder { extension JavaDecoder {
fileprivate func unbox<T: Decodable>(type: T.Type, javaObject: jobject) throws -> T { fileprivate func unbox<T: Decodable>(type: T.Type, javaObject: jobject) throws -> T {
let typeName = String(reflecting: type) let typeName = String(describing: type)
if let decodableClosure = JavaCoderConfig.decodableClosures[typeName] { if let decodableClosure = JavaCoderConfig.decodableClosures[typeName] {
return try decodableClosure(javaObject) as! T return try decodableClosure(javaObject) as! T
} }
@ -653,7 +653,7 @@ extension JavaDecoder {
} }
fileprivate func getJavaClassname<T>(forType: T.Type) -> String { fileprivate func getJavaClassname<T>(forType: T.Type) -> String {
let typeName = String(reflecting: forType) let typeName = String(describing: forType)
if let className = JavaCoderConfig.codableClassNames[typeName] { if let className = JavaCoderConfig.codableClassNames[typeName] {
return className return className
} }

View File

@ -513,7 +513,7 @@ extension JavaEncoder {
fileprivate func box<T: Encodable>(_ value: T) throws -> JNIStorageObject { fileprivate func box<T: Encodable>(_ value: T) throws -> JNIStorageObject {
let storage: JNIStorageObject let storage: JNIStorageObject
let typeName = String(reflecting: type(of: value)) let typeName = String(describing: type(of: value))
if let encodableClosure = JavaCoderConfig.encodableClosures[typeName] { if let encodableClosure = JavaCoderConfig.encodableClosures[typeName] {
let javaObject = try encodableClosure(value) let javaObject = try encodableClosure(value)
storage = JNIStorageObject(type: .object(className: JavaCoderConfig.codableClassNames[typeName]!), javaObject: javaObject) storage = JNIStorageObject(type: .object(className: JavaCoderConfig.codableClassNames[typeName]!), javaObject: javaObject)
@ -530,6 +530,10 @@ extension JavaEncoder {
fullClassName = ArrayListClassname fullClassName = ArrayListClassname
storageType = .anyCodable(codable: .array) storageType = .anyCodable(codable: .array)
} }
else if let javaClassname = JavaCoderConfig.codableClassNames[anyCodableValue.typeName] {
fullClassName = javaClassname
storageType = .anyCodable(codable: .object(className: fullClassName))
}
else { else {
fullClassName = package + "/" + anyCodableValue.typeName fullClassName = package + "/" + anyCodableValue.typeName
storageType = .anyCodable(codable: .object(className: fullClassName)) storageType = .anyCodable(codable: .object(className: fullClassName))