mercredi 9 septembre 2020

Access extention functions (values) through reflection in Kotlin

I would like to access Kotlin's extension functions/values through reflection. I have a very simple example, but I cannot figure out what is wrong here

import kotlin.reflect.full.*

class A

fun A.ext() = 2
val A.value: Int
    get() = 2

fun main() {
    val a = A()
    val reflection = a::class

    reflection.members.joinToString(", ") { it.name }
    reflection.declaredMemberExtensionProperties.joinToString(", ") { it.name }
    reflection.memberExtensionProperties.joinToString(", ") { it.name }
    reflection.memberExtensionFunctions.joinToString(", ") { it.name }
    reflection.declaredMembers.joinToString(", ") { it.name }
}

This, of course, won't work in the Playground, because kotlin-reflect.jar is not in the classpath there.

Neither of the options produces the expected output: ext and value. What am I missing?





Aucun commentaire:

Enregistrer un commentaire