Why can't dagger process these kotlin generics?

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

I'm having some weird kotlin generic issues with Dagger that I kinda fixed but the solution isn't sound.

Here's the dagger classes:

@Module class P5Module {     @Provides fun pool(): RecyclerView.RecycledViewPool = RecyclerView.RecycledViewPool()     @Provides     fun adapters(fusion: P5FusionAdapter, personas: P5ListAdapter, skills: P5SkillsAdapter, info: InfoAdapter)             : List = listOf(fusion, personas, skills, info) }  @ActivityScope @Subcomponent(modules = arrayOf(P5Module::class)) interface P5Component {     fun adapter(): PageableAdapter }  interface Pageable {     fun manager(ctx: Context): LayoutManager     fun attach()     fun adapter(): Adapter }  class PageableAdapter @Inject constructor(val pageables: List, val pool: RecyclerView.RecycledViewPool) :PagerAdapter() 

When I build, I get this kapt error in the top level component:

e: C:\Users\daykm\StudioProjects\P5Executioner\app\build\tmp\kapt3\stubs\appDebug\com\daykm\p5executioner\AppComponent.java:17: error: [com.daykm.p5executioner.main.P5Component.adapter()] java.util.List extends com.daykm.p5executioner.view.Pageable> cannot be provided without an @Provides-annotated method. e:   e: public abstract interface AppComponent { e:                 ^ e:       java.util.List extends com.daykm.p5executioner.view.Pageable> is injected at e:           com.daykm.p5executioner.view.PageableAdapter.(pageables, …) e:       com.daykm.p5executioner.view.PageableAdapter is provided at e:           com.daykm.p5executioner.main.P5Component.adapter() e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing 

I took a look at the stubs generated:

@javax.inject.Inject() public PageableAdapter(@org.jetbrains.annotations.NotNull() java.util.List extends com.daykm.p5executioner.view.Pageable> pageables, @org.jetbrains.annotations.NotNull() android.support.v7.widget.RecyclerView.RecycledViewPool pool) {     super(); }  @org.jetbrains.annotations.NotNull() @dagger.Provides() public final java.util.List adapters(@org.jetbrains.annotations.NotNull() 

Apparently doesn't match with because when I modify the dagger class like so:

@Module class P5Module {     @Provides fun pool(): RecyclerView.RecycledViewPool = RecyclerView.RecycledViewPool()     @Provides     fun adapters(fusion: P5FusionAdapter, personas: P5ListAdapter, skills: P5SkillsAdapter, info: InfoAdapter)             : List = listOf(fusion, personas, skills, info) }  @ActivityScope @Subcomponent(modules = arrayOf(P5Module::class)) interface P5Component {     fun adapter(): PageableAdapter }  interface Pageable {     fun manager(ctx: Context): LayoutManager     fun attach()     fun adapter(): Adapter }  class PageableAdapter @Inject constructor(val pageables: List, val pool: RecyclerView.RecycledViewPool) :PagerAdapter() 

The issue disappears. Am I bumping into some weird translation issue with covariance and contravariance? I'd rather not force a cast downstream.

回答1:

Kotlin's List translates to covariance in Java's List extends E> by default. Dagger doesn't like that.

To get around this issue, you can either switch to MutableList which is invariant, or write List.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!