kotlin - Dagger 2 Set Multibinding 不适用于 Kotlin 中的 SimpleEntry?
问题描述
当提供Pair
as时,以下多重绑定有效IntoSet
@Provides
@IntoSet
fun entryOne(): Pair<String, String> {
val key = randomStringGenerator()
val value = "Random Value 1"
return Pair(key, value)
}
@Provides
@IntoSet
fun entryTwo(): Pair<String, String> {
val key = randomStringGenerator()
val value = "Random Value 2"
return Pair(key, value)
}
@Provides
fun randomKeyValueMap(entries: Set<Pair<String, String>>): Map<String, String> {
val randomKeyValueMap = LinkedHashMap<String, String>(entries.size)
for (entry in entries) {
randomKeyValueMap[entry.first] = entry.second
}
return randomKeyValueMap
}
但是,当变成 时Pair
,SimpleEntry
它不再起作用。
@Provides
@IntoSet
fun entryOne(): AbstractMap.SimpleEntry<String, String> {
val key = randomStringGenerator()
val value = "Random Value 1"
return AbstractMap.SimpleEntry(key, value)
}
@Provides
@IntoSet
fun entryTwo(): AbstractMap.SimpleEntry<String, String> {
val key = randomStringGenerator()
val value = "Random Value 2"
return AbstractMap.SimpleEntry(key, value)
}
@Provides
fun randomKeyValueMap(entries: Set<AbstractMap.SimpleEntry<String, String>>): Map<String, String> {
val randomKeyValueMap = LinkedHashMap<String, String>(entries.size)
for (entry in entries) {
randomKeyValueMap[entry.key] = entry.value
}
return randomKeyValueMap
}
它投诉
error: [Dagger/MissingBinding] java.util.Set<? extends java.util.AbstractMap.SimpleEntry<java.lang.String,java.lang.String>> cannot be provided without an @Provides-annotated method.
public abstract interface MyComponent {
^
java.util.Set<? extends java.util.AbstractMap.SimpleEntry<java.lang.String,java.lang.String>> is injected at
请注意,如果我使用Entry
for Java,它可以正常工作。仅不适用于 Kotlin。
解决方案
看起来我需要@JvmSuppressWildcards
@Provides
@IntoSet
fun entryOne(): Map.Entry<String, String> {
val key = randomStringGenerator()
val value = "Random Value 1"
return AbstractMap.SimpleEntry(key, value)
}
@Provides
@IntoSet
fun entryTwo(): Map.Entry<String, String> {
val key = randomStringGenerator()
val value = "Random Value 2"
return AbstractMap.SimpleEntry(key, value)
}
@Provides
@JvmSuppressWildcards
fun randomKeyValueMap(entries: Set<Map.Entry<String, String>>): Map<String, String> {
val randomKeyValueMap = LinkedHashMap<String, String>(entries.size)
for (entry in entries) {
randomKeyValueMap[entry.key] = entry.value
}
return randomKeyValueMap
}
推荐阅读
- operating-system - 操作系统中的进程表
- ios - 如何使用按钮增加/减少价格标签值?
- c++ - 带有 lib 的 Qt 子目录项目和使用该 lib 的应用程序
- python - Pandas 列值替换
- python - 在 Django 的更新视图模式中获取上下文数据
- firebase - Web 推送:从 GCM 迁移到 FCM
- ios - Reloading SubViews in Swift
- c# - C# - From DataGridView to Excel Problem When I Reset To PC
- java - JavaFX 在 GridPane 中动态制作按钮
- graphql - Apollo Graphql Import Issue with cacheControl directive