Dev: add support of Set
This commit is contained in:
parent
b47a4f4fce
commit
e72d52c36c
|
@ -23,6 +23,7 @@ let HashMapClassname = "java/util/HashMap"
|
|||
let SetClassname = "java/util/Set"
|
||||
let UriClassname = "android/net/Uri"
|
||||
let DateClassname = "java/util/Date"
|
||||
let HashSetClassname = "java/util/HashSet"
|
||||
|
||||
// MARK: Java Classes
|
||||
var IntegerClass = try! JNI.getJavaClass("java/lang/Integer")
|
||||
|
@ -36,6 +37,7 @@ let ExceptionClass = try! JNI.getJavaClass("java/lang/Exception")
|
|||
let UriClass = try! JNI.getJavaClass("android/net/Uri")
|
||||
let DateClass = try! JNI.getJavaClass("java/util/Date")
|
||||
let VMDebugClass = try! JNI.getJavaClass("dalvik/system/VMDebug")
|
||||
let HashSetClass = try! JNI.getJavaClass("java/util/HashSet")
|
||||
|
||||
// MARK: Java methods
|
||||
let UriConstructor = JNI.api.GetStaticMethodID(JNI.env, UriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;")
|
||||
|
@ -63,7 +65,10 @@ let HashMapSizeMethod = try! JNI.getJavaMethod(forClass: HashMapClassname, metho
|
|||
let SetToArrayMethod = try! JNI.getJavaMethod(forClass: SetClassname, method: "toArray", sig: "()[L\(ObjectClassname);")
|
||||
let ArrayListGetMethod = try! JNI.getJavaMethod(forClass: ArrayListClassname, method: "get", sig: "(I)L\(ObjectClassname);")
|
||||
let ArrayListSizeMethod = try! JNI.getJavaMethod(forClass: ArrayListClassname, method: "size", sig: "()I")
|
||||
let ArrayListAddMethod = try! JNI.getJavaMethod(forClass: ArrayListClassname, method: "add", sig: "(Ljava/lang/Object;)Z")
|
||||
let CollectionAddMethod = try! JNI.getJavaMethod(forClass: "java/util/Collection", method: "add", sig: "(Ljava/lang/Object;)Z")
|
||||
let CollectionIteratorMethod = try! JNI.getJavaMethod(forClass: "java/util/Collection", method: "iterator", sig: "()Ljava/util/Iterator;")
|
||||
let CollectionSizeMethod = try! JNI.getJavaMethod(forClass: "java/util/Collection", method: "size", sig: "()I")
|
||||
let IteratorNextMethod = try! JNI.getJavaMethod(forClass: "java/util/Iterator", method: "next", sig: "()Ljava/lang/Object;")
|
||||
let DateGetTimeMethod = try! JNI.getJavaMethod(forClass: "java/util/Date", method: "getTime", sig:"()J")
|
||||
let VMDebugDumpReferenceTablesMethod = JNI.api.GetStaticMethodID(JNI.env, VMDebugClass, "dumpReferenceTables", "()V")
|
||||
|
||||
|
|
|
@ -449,13 +449,17 @@ fileprivate class JavaArrayContainer: UnkeyedDecodingContainer {
|
|||
|
||||
let decoder: JavaDecoder
|
||||
let jniStorage: JNIStorageObject
|
||||
let javaObject: jobject
|
||||
let javaIterator: jobject
|
||||
|
||||
fileprivate init(decoder: JavaDecoder, jniStorage: JNIStorageObject) {
|
||||
self.decoder = decoder
|
||||
self.jniStorage = jniStorage
|
||||
self.javaObject = jniStorage.javaObject
|
||||
self.count = Int(JNI.CallIntMethod(self.javaObject, methodID: ArrayListSizeMethod))
|
||||
self.count = Int(JNI.CallIntMethod(jniStorage.javaObject, methodID: CollectionSizeMethod))
|
||||
self.javaIterator = JNI.CallObjectMethod(jniStorage.javaObject, methodID: CollectionIteratorMethod)!
|
||||
}
|
||||
|
||||
deinit {
|
||||
JNI.DeleteLocalRef(javaIterator)
|
||||
}
|
||||
|
||||
func decodeNil() throws -> Bool {
|
||||
|
@ -463,7 +467,7 @@ fileprivate class JavaArrayContainer: UnkeyedDecodingContainer {
|
|||
}
|
||||
|
||||
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
|
||||
guard let object = JNI.CallObjectMethod(self.javaObject, methodID: ArrayListGetMethod, args: [self.index]) else {
|
||||
guard let object = JNI.CallObjectMethod(self.javaIterator, methodID: IteratorNextMethod) else {
|
||||
throw JavaCodingError.cantFindObject("Array out of range: \(self.currentIndex)")
|
||||
}
|
||||
defer {
|
||||
|
@ -661,6 +665,9 @@ extension JavaDecoder {
|
|||
if className == ArrayListClassname {
|
||||
codableType = .array
|
||||
}
|
||||
else if className == HashSetClassname {
|
||||
codableType = .array
|
||||
}
|
||||
else if className == HashMapClassname {
|
||||
codableType = .dictionary
|
||||
}
|
||||
|
@ -679,6 +686,8 @@ extension JavaDecoder {
|
|||
switch stringType {
|
||||
case _ where stringType.starts(with: "Array<"):
|
||||
storageObject = JNIStorageObject(type: .array, javaObject: obj)
|
||||
case _ where stringType.starts(with: "Set<"):
|
||||
storageObject = JNIStorageObject(type: .array, javaObject: obj)
|
||||
case _ where stringType.starts(with: "Dictionary<"):
|
||||
storageObject = JNIStorageObject(type: .dictionary, javaObject: obj)
|
||||
default:
|
||||
|
@ -735,6 +744,9 @@ extension JavaDecoder {
|
|||
else if "\(forType)".starts(with: "Array<") {
|
||||
return ArrayListClassname
|
||||
}
|
||||
else if "\(forType)".starts(with: "Set<") {
|
||||
return HashSetClassname
|
||||
}
|
||||
else if "\(forType)".starts(with: "Dictionary<") {
|
||||
return HashMapClassname
|
||||
}
|
||||
|
@ -752,6 +764,8 @@ extension JavaDecoder {
|
|||
switch className {
|
||||
case ArrayListClassname:
|
||||
return .array
|
||||
case HashSetClassname:
|
||||
return .array
|
||||
case HashMapClassname:
|
||||
return .dictionary
|
||||
default:
|
||||
|
|
|
@ -376,7 +376,7 @@ fileprivate class JavaArrayContainer : UnkeyedEncodingContainer {
|
|||
|
||||
public func encode<T : Encodable>(_ value: T) throws {
|
||||
let storeObject = try self.encoder.box(value)
|
||||
let rewrite = JNI.CallBooleanMethod(self.javaObject, methodID: ArrayListAddMethod, args: [jvalue(l: storeObject.javaObject)])
|
||||
let rewrite = JNI.CallBooleanMethod(self.javaObject, methodID: CollectionAddMethod, args: [jvalue(l: storeObject.javaObject)])
|
||||
assert(rewrite == JNI.TRUE, "ArrayList should always return true from add()")
|
||||
count += 1
|
||||
}
|
||||
|
@ -645,6 +645,10 @@ extension JavaEncoder {
|
|||
fullClassName = ArrayListClassname
|
||||
storageType = .array
|
||||
}
|
||||
else if value is Set<AnyHashable> {
|
||||
fullClassName = HashSetClassname
|
||||
storageType = .array
|
||||
}
|
||||
else {
|
||||
fullClassName = package + "/" + String(describing: type(of: value))
|
||||
storageType = .object(className: fullClassName)
|
||||
|
|
Loading…
Reference in New Issue