https://kotlinlang.org logo
#getting-started
Title
# getting-started
w

wrongwrong

08/16/2021, 8:06 PM
Hi. I was trying to see if I could get some information from a
KFunction
without
kotlin-reflect
(using
kotlinx-metadata-jvm
), but I couldn't get
Metadata
with valid information, so I have a question. What I want to achieve is as follows. 1. get the information of arguments and return values from
KFunction
. 2. get
javaConstructor
or
javaMethod
from
KFunction
.
A simple verification with the following code shows that although the
Metadata
annotation can be retrieved from a
KFunction
, the contents are almost empty.
Copy code
package org.example

import kotlin.reflect.KFunction

data class DataClass(val value: String)

fun main() {
    val function: KFunction<*> = ::DataClass
    val metadata: Metadata = function::class.java.getAnnotation(Metadata::class.java)!!

    println(metadata.data1.asList()) // -> []
    println(metadata.data2.asList()) // -> []
}
On the other hand, looking at the bytecode of the
synthetic class
generated as a
FunctionReference
, it looks like there is a valid
Metadata
. How can I get this
Metadata
?
If there is another way to get information from
KFunction
without using
kotlin-reflect
, I would be grateful if you could let me know. Thank you.
It solved itself in another way.
KFunction
retrieved as
Reference
can be cast to
kotlin.jvm.internal.FunctionReference
, so various information could be read from it. From
(functionReference.owner as ClassBasedDeclarationContainer).jClass
, we could also read information about the
Class
from which it was defined.