Tech
06/15/2024, 8:32 PMprivate fun getFields(): List<KProperty<*>> {
val kClass = this::class
return cachedFields.getOrPut(kClass) {
kClass.declaredMemberProperties
.filterNot { it.javaField?.isSynthetic == true }
.onEach { it.isAccessible = true }
.map { it }
}
}
It works fine however, when it retrieves all declared properties it doesn't do them in the order they're declared, example
data class Example(val a: String, val b: String)
So b can be gotten before a, is there any way to do this that maintains order that they're declared without using annotations?Youssef Shoaib [MOD]
06/15/2024, 8:35 PMdata
classTech
06/15/2024, 8:35 PMYoussef Shoaib [MOD]
06/15/2024, 8:36 PMTech
06/15/2024, 8:37 PMTech
06/15/2024, 8:38 PMreturn cachedFields.getOrPut(kClass) {
val p = kClass.primaryConstructor
?.parameters
?.withIndex()
?.map { (index, param) -> param to index }
?: error("Primary constructor not found for class ${kClass.simpleName}")
kClass.declaredMemberProperties
.filterNot { it.javaField?.isSynthetic == true }
.onEach { it.isAccessible = true }
.sortedBy { p.firstOrNull { (param, _) -> param.name == it.name }?.second }
.map { it }
}
Tech
06/15/2024, 8:38 PMYoussef Shoaib [MOD]
06/15/2024, 8:41 PMreturn cachedFields.getOrPut(kClass) {
val p = kClass.primaryConstructor
?.parameters
?: error("Primary constructor not found for class ${kClass.simpleName}")
kClass.declaredMemberProperties
.filterNot { it.javaField?.isSynthetic == true }
.onEach { it.isAccessible = true }
.sortedBy { p.indexOfFirst { param -> param.name == it.name } }
}
Cleaned up a little bit!Tech
06/15/2024, 8:42 PM