1

I have some issues when converting dagger interfaces from java to Kotlin

I got [Dagger/MissingBinding] java.util.Map cannot be provided without an @Provides-annotated method.

Here is my interface

   interface TopicConfigModule {
    @Binds
    @IntoMap
    @StringKey(NAME)
    fun bindCommandHandler(handler: TopicCommandHandler): CommandHandler

    companion object {
        @JvmStatic
        @Provides
        @FragmentScope
        fun provideHubsConfig(
            commandRegistry: Map<String, CommandHandler>
        ): Config {
            return ...
        }
    }
}

and CommandHandler is java interface

public interface HubsCommandHandler {```}


I.S
  • 1,904
  • 2
  • 25
  • 48
  • 1
    try to use `commandRegistry: Map`, also you don't need `@JvmStatic` (I assume that you're using dagger 2.26 or higher) – IR42 May 04 '20 at 18:14
  • @IR42 It works but I, am not sure why we need JvmSuppressWildcards . And please write down as answer I will vote for your answer. – I.S May 04 '20 at 18:28

1 Answers1

4

Map in Kotlin is covariant (variance) on its value type (public interface Map<K, out V>), but Map in Java is not. Your function will be translated to

Config provideHubsConfig(Map<String, ? extends CommandHandler> commandRegistry) { ... }

but dagger provides exactly Map<String, CommandHandler>. So we need to suppress wildcards with @JvmSuppressWildcards

commandRegistry: Map<String, @JvmSuppressWildcards CommandHandler>

Calling Kotlin from Java - variant-generics

IR42
  • 8,587
  • 2
  • 23
  • 34