blob: cd4cc2a52e7eeb8ba091f3debfe6fabf4a95ed5d [file] [log] [blame]
Vsevolod Tolstopyatov70a74872020-04-24 14:59:28 +03001/*
Aurimas Liutikas7b140462021-05-12 21:56:16 +00002 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Vsevolod Tolstopyatov70a74872020-04-24 14:59:28 +03003 */
4@file:Suppress("unused")
5package kotlinx.coroutines.debug.internal
6
7import net.bytebuddy.*
8import net.bytebuddy.agent.*
9import 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 */
15internal 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}