There is an coroutine block that can runs suspend functions.
But I call the function by invoke
via reflection. This is java style invocation, apparently a simple call will not work.
Are there ways to run reflected method asynchronously? How to await this method?
import kotlin.coroutines.experimental.*
class TestClass(val InString: String) {
suspend fun printString() {
println(InString)
}
}
fun launch(context: CoroutineContext, block: suspend () -> Unit) =
block.startCoroutine(StandaloneCoroutine(context))
private class StandaloneCoroutine(override val context: CoroutineContext): Continuation<Unit> {
override fun resume(value: Unit) {}
override fun resumeWithException(exception: Throwable) {
val currentThread = Thread.currentThread()
currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, exception)
}
}
fun main(args: Array<String>) {
launch(EmptyCoroutineContext) {
val a = TestClass("TestString");
for (method in a.javaClass.methods) {
if (method.name == "printString")
method.invoke(a) // Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
}
}
}