Skip to content

Instantly share code, notes, and snippets.

@morj
Created May 18, 2020 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morj/08c0e9539833800b4a2158cb188f6574 to your computer and use it in GitHub Desktop.
Save morj/08c0e9539833800b4a2158cb188f6574 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.random.Random
suspend fun build(): () -> Unit {
val number = Random(239).nextInt()
return withContext<() -> Unit>(CoroutineName("produce value")) {
delay(1)
object : () -> Unit() {
override fun invoke() {
if (number != 0) {
Thread.sleep(1)
}
}
}
}
}
fun main() {
val value = runBlocking { build() }
val wrapper = value.javaClass.getDeclaredField("this$0")
val delegate = wrapper.get(value)
println(delegate.javaClass.declaredFields.joinToString(
prefix = "{\n ", separator = "\n ", postfix = "\n}\n"
) {
"${it.name}: ${it.type.name}"
})
val coroutine = delegate.javaClass.getDeclaredField("L\$0")
println("found coroutine: ${coroutine.get(delegate).javaClass.name}")
value()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment