简体   繁体   中英

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 . 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. 错误 This is my first time using reflection java/kotlin, please let me know if it can be fixed.

Edit 1:

It works perfectly if I use name of the this.javaClass.kotlin directly like this

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.

className::class.memberProperties (where className = this.javaClass.kotlin ) returns a collection of member properties of class of className , which is KClass<out Test> . In short you are getting members of KClass instead of Test .

Solution: change className::class.memberProperties to className.memberProperties .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM