kotlin -> 协程 FunctionX 理解

kotlin suspend 对 反编译 java Function 部分进行分析是为为什么?

demo.kt

class MyCoroutine() : Continuation<String> {
    override fun resumeWith(result: Result<String>) {
        println("MyCoroutine 回调resumeWith 返回的结果 " + result.getOrNull())
    }
    override val context: CoroutineContext
        get() = kotlin.coroutines.EmptyCoroutineContext
}


@InternalCoroutinesApi
fun testOne() {
    val myCoroutineFun: suspend () -> String = {
        println("返回 hello结果")
        "hello"
    }
    val myCoroutine = MyCoroutine()
    myCoroutineFun.startCoroutineCancellable(myCoroutine)
}

@InternalCoroutinesApi
fun main() {
    testOne()
}

demo.java

   public static final void testOne() {
      Function1 myCoroutineFun = (Function1)(new Function1((Continuation)null) {
         int label;

         @Nullable
         public final Object invokeSuspend(@NotNull Object var1) {
            Object var4 = IntrinsicsKt.getCOROUTINE_SUSPENDED();
            switch(this.label) {
            case 0:
               ResultKt.throwOnFailure(var1);
               String var2 = "返回 hello结果";
               boolean var3 = false;
               System.out.println(var2);
               return "hello";
            default:
               throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
         }

         @NotNull
         public final Continuation create(@NotNull Continuation completion) {
            Intrinsics.checkParameterIsNotNull(completion, "completion");
            Function1 var2 = new <anonymous constructor>(completion);
            return var2;
         }

         public final Object invoke(Object var1) {
            return ((<undefinedtype>)this.create((Continuation)var1)).invokeSuspend(Unit.INSTANCE);
         }
      });
      MyCoroutine myCoroutine = new MyCoroutine();
      CancellableKt.startCoroutineCancellable(myCoroutineFun, (Continuation)myCoroutine);
   }

我们在看看 编译出来的 Function1 具体是什么?

/** A function that takes 1 argument. */
public interface Function1<in P1, out R> : Function<R> {
    /** Invokes the function with the specified argument. */
    public operator fun invoke(p1: P1): R
}

懵逼 懵逼 懵逼

为什么 Function1 里面只有一个 invoke, 没有 invokeSuspend , create 这些方法。那这写方法到底在哪里。

我们点击 demo.kt ->Tools->kotlin->show kotlin ByteCode 搜索一下

SuspendLambda

我们可以看到
xxx 代表省略

final class xxx/MyCoroutineKt t e s t O n e testOne testOnemyCoroutineFun$1 extends xxx/SuspendLambda implements xxx/Function1 {

这就是说明 myCoroutineFun 最终经过 kotlin 编译出来的产物是会继承 SuspendLambda 并且 implements Function1。

重点:我们到哪里找 invokeSuspend , create, 那我们肯定 直接看到了 SuspendLambda

因为是给自己记录的

答案:

SuspendLambda -> 找到  ContinuationImpl -> 找到 BaseContinuationImpl 

然后我们就可以在   BaseContinuationImpl 找到  invokeSuspend  , create  所以说,我们看到了编译出来的是简化过,最终可以带字节码当中看到具体的。

悬念:
myCoroutineFun 编译出来 invoke ,invokeSuspend , create 那么到底是执行那个方法呢。

速查纪律表:
1.
cancellable.kt ->startCoroutineCancellable
2.
IntrinsicsKt.kt IntrinsicsKtJvm ->createCoroutineUnintercepted
3.
Continuation.kt ->resume
4.
DispatchedContinuation.kt ->resumeWith
5.
BaseContinuationImpl -> SuspendLambda Suspend.kt ->suspend

透彻好文:
https://mp.weixin.qq.com/s/5HN1GFTochZqYsQoqOLVIA

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值