Binding Into Map With KClass Type

前端 未结 1 1909
孤城傲影
孤城傲影 2021-02-07 04:01

I am trying to bind subclasses of ViewModel into a map by their KClass types:

@Module abstract class ViewModelModule {

    @Binds @Int         


        
相关标签:
1条回答
  • 2021-02-07 04:06

    When using KClass in an annotation, it actually gets compiled to Java's Class. But the actual issue is the wildcard in java.util.Map<kotlin.reflect.KClass<? extends android.arch.lifecycle.ViewModel>,? extends javax.inject.Provider<android.arch.lifecycle.ViewModel>> that the Kotlin compiler is generating.

    Assuming that @ViewModelKey is defined as

    @MapKey
    annotation class ViewModelKey(val value: KClass<out ViewModel>)
    

    You'll need to define your injection site as

    Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
    

    Using @JvmSuppressWildcards will prevent the compiler from generating wildcards.

    I don't actually know, why wildcards are not supported by the Dagger compiler. You can see a similar issue here: Dagger 2: How to inject Map<Class<? extends Foo>, Provider<? extends Foo>>

    0 讨论(0)
提交回复
热议问题