为什么 dagger 不能处理这些 kotlin 泛型?
Why can't dagger process these kotlin generics?
我在使用 Dagger 时遇到了一些奇怪的 kotlin 通用问题,我已经修复了,但解决方案并不合理。
这是匕首classes:
@Module class P5Module {
@Provides fun pool(): RecyclerView.RecycledViewPool = RecyclerView.RecycledViewPool()
@Provides
fun adapters(fusion: P5FusionAdapter, personas: P5ListAdapter, skills: P5SkillsAdapter, info: InfoAdapter)
: List<Pageable> = 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<Pageable>, val pool: RecyclerView.RecycledViewPool) :PagerAdapter()
构建时,我在顶级组件中遇到此 kapt 错误:
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.<init>(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
我查看了生成的存根:
@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<com.daykm.p5executioner.view.Pageable> adapters(@org.jetbrains.annotations.NotNull()
显然不匹配因为当我修改匕首时 class 像这样:
@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()
问题消失了。
我是否遇到了一些关于协变和逆变的奇怪翻译问题?我宁愿不强迫下游演员。
Kotlin 的 List<out E>
默认转换为 Java 的 List<? extends E>
中的协方差。 Dagger 不喜欢那样。
要解决这个问题,您可以切换到不变的 MutableList<E>
,或者写成 List<@JvmSuppressWildcards E>
.
我在使用 Dagger 时遇到了一些奇怪的 kotlin 通用问题,我已经修复了,但解决方案并不合理。
这是匕首classes:
@Module class P5Module {
@Provides fun pool(): RecyclerView.RecycledViewPool = RecyclerView.RecycledViewPool()
@Provides
fun adapters(fusion: P5FusionAdapter, personas: P5ListAdapter, skills: P5SkillsAdapter, info: InfoAdapter)
: List<Pageable> = 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<Pageable>, val pool: RecyclerView.RecycledViewPool) :PagerAdapter()
构建时,我在顶级组件中遇到此 kapt 错误:
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.<init>(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
我查看了生成的存根:
@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<com.daykm.p5executioner.view.Pageable> adapters(@org.jetbrains.annotations.NotNull()
显然不匹配因为当我修改匕首时 class 像这样:
@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()
问题消失了。 我是否遇到了一些关于协变和逆变的奇怪翻译问题?我宁愿不强迫下游演员。
Kotlin 的 List<out E>
默认转换为 Java 的 List<? extends E>
中的协方差。 Dagger 不喜欢那样。
要解决这个问题,您可以切换到不变的 MutableList<E>
,或者写成 List<@JvmSuppressWildcards E>
.