kotlin等价于java`?延伸`

z9ju0rcb  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(349)

我正在尝试将模块类(dagger)转换为kotlin文件,但在尝试构建kotlin转换代码时遇到了这个问题。
@Binding 方法的参数类型必须可分配给返回类型
这就是我的问题所在。

@Binds
@IntoMap
@ClassKey(EnterEmailFragment::class)
abstract fun bindsEnterEmailInjector(injector: EnterEmailInjector): ScreenInjector<in Fragment, out Any>

java代码:(java代码运行良好!)

@Binds
@IntoMap
@ClassKey(EnterEmailFragment.class)
abstract ScreenInjector<? extends Fragment, ? extends Object> bindsEnterEmailInjector(EnterEmailInjector injector);

这就是.class文件的样子

public abstract class com.gojek.driver.ulysses.email.di.EmailScreenFactoryModule {
  public static final com.gojek.driver.ulysses.email.di.EmailScreenFactoryModule$Companion Companion;

  public abstract com.gojek.driver.base.ScreenInjector<androidx.fragment.app.Fragment, java.lang.Object> bindsEnterEmailInjector(com.gojek.driver.ulysses.email.di.EnterEmailInjector);

  public com.gojek.driver.ulysses.email.di.EmailScreenFactoryModule();
    Code:
       0: aload_0
       1: invokespecial #16                 // Method java/lang/Object."<init>":()V
       4: return

  static {};
    Code:
       0: new           #42                 // class com/gojek/driver/ulysses/email/di/EmailScreenFactoryModule$Companion
       3: dup
       4: aconst_null
       5: invokespecial #49                 // Method com/gojek/driver/ulysses/email/di/EmailScreenFactoryModule$Companion."<init>":(Lkotlin/jvm/internal/DefaultConstructorMarker;)V
       8: putstatic     #40                 // Field Companion:Lcom/gojek/driver/ulysses/email/di/EmailScreenFactoryModule$Companion;
      11: return

  public static final com.gojek.driver.ulysses.email.di.EnterEmailInjector providesEnterEmailInjector(com.gojek.driver.ulysses.driver.DriverProfileUsecase);
    Code:
       0: getstatic     #40                 // Field Companion:Lcom/gojek/driver/ulysses/email/di/EmailScreenFactoryModule$Companion;
       3: aload_0
       4: invokevirtual #44                 // Method com/gojek/driver/ulysses/email/di/EmailScreenFactoryModule$Companion.providesEnterEmailInjector:(Lcom/gojek/driver/ulysses/driver/DriverProfileUsecase;)Lcom/gojek/driver/ulysses/email/di/EnterEmailInjector;
       7: areturn
}

我不太清楚我在这里遗漏了什么。

cld4siwp

cld4siwp1#

这实际上是一个已知的问题,发生在kotlin的outprojected类型上。这是在匕首的github问题上报道的。
作为解决此问题的一种变通方法,github问题和本文建议使用 @JvmSuppressWildcards 抑制编译器创建的警告。

@Binds
@IntoMap
@ClassKey(EnterEmailFragment::class)
abstract fun bindsEnterEmailInjector(injector: EnterEmailInjector)
    : ScreenInjector<out @JvmSuppressWildcards Fragment, out @JvmSuppressWildcards Any>

相关问题