1 /* 2 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 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> { invokenull16 override fun invoke(value: Boolean) { 17 if (value) attach() else detach() 18 } 19 attachnull20 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.internal.DebugProbesKt") 24 25 ByteBuddy() 26 .redefine(cl2) 27 .name(cl.name) 28 .make() 29 .load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent()) 30 } 31 detachnull32 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 } 42