I am new to Kotlin. Facing an issue with reflection and generics. Below is my code.
abstract class Action {
fun sleep(body: Person.() -> Unit){
var p = Person("a");
p.body()
println(p.name + " is zzzzzzz...")
}
}
class Person(var name:String =""){
companion object:Action();
}
inline fun <reified T> test(){
val companionObject = T::class.companionObject
if (companionObject != null) {
println(companionObject.javaObjectType)
val functionEx = companionObject.functions.filter { it.name.equals("sleep") }.first()
// How to invoke functionEx with block and "this"
}
}
fun main(args: Array<String>) {
Person.sleep {
this.name = "abc"
}
test<Person>()
}
I want to invoke the sleep
function through functionEx
with the same block code which is in the main. I am struggling with the this
operator.
I am using some API and have emulated the problem through Action
and Person
. So can't change their implementation. Its just the test
function which is under my control.