dimanche 7 avril 2019

How to extract Class from KProperty1 in Kotlin

I have the following code :

class User : MyType {
  val address: Address? = null
  val company: Company? = null
  ...
}

class Address : MyType {
  val city: String? = null
}

class Company : MyType {
  val name: String? = null
}

var properties: MutableMap<String, Collection<KProperty1<Any, *>>>? = mutableMapOf()

fun init() {
    fillPropertiesMap(User::class.java)
}

private inline fun <reified T : Any> fillPropertiesMap(clazz: Class<T>) {
    val prop = clazz.kotlin.memberProperties
            .filter { it.visibility == KVisibility.PUBLIC } as Collection<KProperty1<Any, *>>
    fill(clazz as Class<Any>, prop)
}

fun fill(clazz: Class<Any>, props: Collection<KProperty1<Any, *>>) {
    properties[clazz.simpleName] = props
    props.forEach()
    { prop ->
        if (isMyCustomType(prop)) { 
            fillPropertiesMap(prop.????) . // how get class from KProperty1 for Address.class and Company.class
        }
    }
}

I want to fill Map with KProperty1 of my class User. The class User has a hierarchical structure, and i need to get all the KProperty1 list of the classes that are in the class User. Question is how get class from KProperty1





Aucun commentaire:

Enregistrer un commentaire