[英]Issue while using kotlin reflaction to map object member properties to hashmap
open class Test {
fun getAsHashMap() : HashMap<String, Any> {
val hashMap = HashMap<String, Any>()
val className = this.javaClass.kotlin
for (prop in className::class.memberProperties) {
val field = className::class.java.getDeclaredField(prop.name)
val fieldSerializedName : SerializedName? = field.getAnnotation(SerializedName::class.java)
fieldSerializedName?.let {
hashMap[fieldSerializedName.value] = prop.get(this)!!
} ?: run {
hashMap[prop.name] = prop.get(this)!!
}
}
return hashMap
}
}
I have wrote above function to map the memberProperties of object instance
of its child class to hashmap
. 我已经编写了上面的函数来将其子类的
object instance
的memberProperties hashmap
到hashmap
。 It either uses serialized name of the member or prop name [Based on availability of serialized name for that property] But unfortunately I get the following error. 它要么使用成员的序列化名称,要么使用prop名称。[基于该属性的序列化名称的可用性],但是不幸的是,我收到以下错误。
This is my first time using reflection java/kotlin, please let me know if it can be fixed.
这是我第一次使用反射java / kotlin,请让我知道它是否可以修复。
Edit 1: 编辑1:
It works perfectly if I use name of the this.javaClass.kotlin directly like this 如果我像这样直接使用this.javaClass.kotlin的名称,它将非常有效
data class ProductInformation (
@field:SerializedName("productid")
val productId: Int,
@field:SerializedName("productname")
val productName: String,
@field:SerializedName("brandname")
val brandName: String,
@field:SerializedName("originalprice")
val originalPrice: Int,
@field:SerializedName("sellingprice")
val sellingPrice: Int,
@field:SerializedName("productgender")
val productGender: String,
@field:SerializedName("productvariant")
val productVariant: String,
@field:SerializedName("discounted")
val discounted: String,
@field:SerializedName("productcategory")
val productCategory: String
) : StructuredEventAttribute {
override fun getAsHashMap(): HashMap<String, Any> {
val hashMap = HashMap<String, Any>()
for (prop in ProductInformation::class.memberProperties) {
val field = ProductInformation::class.java.getDeclaredField(prop.name)
val fieldSerializedName : SerializedName? = field.getAnnotation(SerializedName::class.java)
fieldSerializedName?.let {
hashMap[fieldSerializedName.value] = prop.get(this)!!
} ?: run {
hashMap[prop.name] = prop.get(this)!!
}
}
return hashMap
}
}
interface StructuredEventAttribute {
fun getAsHashMap() : HashMap<String, Any>
}
It works perfectly fine 它工作得很好
ProductInformation::class.memberProperties
returns a collection of ProductInformation
class member properties. ProductInformation::class.memberProperties
返回ProductInformation
类成员属性的集合。
className::class.memberProperties
(where className = this.javaClass.kotlin
) returns a collection of member properties of class of className
, which is KClass<out Test>
. className::class.memberProperties
(其中className = this.javaClass.kotlin
)返回className
类的成员属性的集合,该类是KClass<out Test>
。 In short you are getting members of KClass
instead of Test
. 简而言之,您将获得
KClass
成员,而不是Test
。
Solution: change className::class.memberProperties
to className.memberProperties
. 解决方案:将
className::class.memberProperties
更改为className.memberProperties
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.