Elye
Elye

Reputation: 60251

Dagger 2 Set Multibinding doesn't work for SimpleEntry in Kotlin?

The below multibinding works, when provide a 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
    }

However when turn Pair into SimpleEntry, it doesn't work anymore.

    @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
    }

It complaints

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

Note, if I use the Entry for Java, it works fine. Only doesn't work for Kotlin.

Upvotes: 0

Views: 508

Answers (1)

Elye
Elye

Reputation: 60251

Looks like I need @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
    }

Upvotes: 2

Related Questions