Hi everyone. I am trying to use `java.lang.reflect...
# coroutines
m
Hi everyone. I am trying to use
java.lang.reflect.Proxy
with suspending functions in the proxied interface, and as such I need a way to turn a suspending function call that is the actual representation of such a call (i.e. something that returns Any? and has a Continuation parameter at the end) into an actual suspending thing. I was wondering if this was the right approach.
R
is the return type, args contains the actual list of arguments, including the continuation as the last argument. Note that I also have to deal with synchronously thrown exceptions, hence the try/catch and ensureSuspension, see here for more info: https://github.com/Kotlin/kotlinx.coroutines/pull/1667#issuecomment-556106349 (This code is what is called by the proxy, it's the invocation handler, so this is what is called when my interface's suspending functions are called)
Copy code
val continuation = args.lastOrNull() as? Continuation<R>
        ?: error("No continuation in call")
    val actualContinuation = continuation.intercepted()
    return suspend {
        try {
            // Do something, we're now suspending and all is good
        } catch (e: Exception) {
            ensureSuspension()
            throw e
        }
    }.startCoroutineUninterceptedOrReturn(actualContinuation)