Vsevolod Tolstopyatov | 70a7487 | 2020-04-24 14:59:28 +0300 | [diff] [blame] | 1 | /* |
Aurimas Liutikas | 7b14046 | 2021-05-12 21:56:16 +0000 | [diff] [blame^] | 2 | * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Vsevolod Tolstopyatov | 70a7487 | 2020-04-24 14:59:28 +0300 | [diff] [blame] | 3 | */ |
| 4 | @file:Suppress("unused") |
| 5 | package kotlinx.coroutines.debug.internal |
| 6 | |
| 7 | import net.bytebuddy.* |
| 8 | import net.bytebuddy.agent.* |
| 9 | import net.bytebuddy.dynamic.loading.* |
| 10 | |
| 11 | /* |
| 12 | * This class is used reflectively from kotlinx-coroutines-core when this module is present in the classpath. |
| 13 | * It is a substitute for service loading. |
| 14 | */ |
| 15 | internal class ByteBuddyDynamicAttach : Function1<Boolean, Unit> { |
| 16 | override fun invoke(value: Boolean) { |
| 17 | if (value) attach() else detach() |
| 18 | } |
| 19 | |
| 20 | private fun attach() { |
| 21 | ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.ForEmulatedAttachment.INSTANCE) |
| 22 | val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt") |
| 23 | val cl2 = Class.forName("kotlinx.coroutines.debug.DebugProbesKt") |
| 24 | |
| 25 | ByteBuddy() |
| 26 | .redefine(cl2) |
| 27 | .name(cl.name) |
| 28 | .make() |
| 29 | .load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent()) |
| 30 | } |
| 31 | |
| 32 | private fun detach() { |
| 33 | val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt") |
| 34 | val cl2 = Class.forName("kotlinx.coroutines.debug.internal.NoOpProbesKt") |
| 35 | ByteBuddy() |
| 36 | .redefine(cl2) |
| 37 | .name(cl.name) |
| 38 | .make() |
| 39 | .load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent()) |
| 40 | } |
| 41 | } |