Created
May 18, 2020 19:45
-
-
Save morj/08c0e9539833800b4a2158cb188f6574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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