2

I want to setup Guice bindings so I created a module in Java that works perfectly:

public class CrashLoggerModule extends AbstractModule {
    @Override
    public void configure() {
        bind(CrashLogger.class).to(ConcreteCrashLogger.class);
    }
}

Then I converted this code to Kotlin:

public class CrashLoggerModule : AbstractModule() {
    override fun configure() {
        bind(javaClass<CrashLogger>()).to(javaClass<ConcreteCrashLogger>())
    }
}

Unfortunately, the Kotlin version of this class doesn't work anymore. This happens because Kotlin calls its internal method public fun <A, B> A.to(that: B): Pair<A, B> instead of LinkedBindingBuilder<T>.to(Class<? extends T> c) which results in Guice binding not being set up correctly.

How can I specify explicitly that I want to use the class method and not the extension function?

Michael
  • 53,859
  • 22
  • 133
  • 139

2 Answers2

4

Easiest way is:

bind(javaClass<CrashLogger>())!!.to(javaClass<ConcreteCrashLogger>())

you can also do an explicit cast to the type bind returns or run KAnnotator on Guice.

Hadi Hariri
  • 4,656
  • 2
  • 24
  • 13
4

I had equal problem with this code, but in my case I can't import function kotlin.jvm.javaClass in Intellij IDEA 2016 no matter what I do. So I resolve problem in following way:

bind(CrashLogger::class.java).to(ConcreteCrashLogger::class.java)

//kotlin version = '1.0.6'

Dmitriy L
  • 41
  • 1