codingwell / scala-guice

Scala extensions for Google Guice
Apache License 2.0
341 stars 44 forks source link

Issue using ScalaMapBinder with Scala Int as a key or a value #100

Open Daniel-Khodabakhsh opened 2 years ago

Daniel-Khodabakhsh commented 2 years ago

I can't get ScalaMapBinder to work with Scala Int in version 5.0.2.

Take the example in the readme;

class MyModule extends AbstractModule with ScalaModule {
  def configure(): Unit = {
    val mBinder = ScalaMapBinder.newMapBinder[String, Int](binder)
    mBinder.addBinding("1").toInstance(1)
  }
}

When I try to inject it as per the readme:

class MyClass @Inject() (myMap: immutable.Map[String, Int]) {
  ...
}

I get the following error:

[error] (run-main-0) com.google.inject.ConfigurationException: Guice configuration errors:
[error]
[error] 1) [Guice/MissingImplementation]: No implementation for Map<String, Object> was bound.
[error]
[error] Requested by:
[error] 1  : MyClass.<init>(MyClass.scala:25)
[error]       \_ for 1st parameter migrationDefinitions
[error]      while locating MyClass
[error]
[error] Learn more:
[error]   https://github.com/google/guice/wiki/MISSING_IMPLEMENTATION
[error]
[error] 1 error
[error]
[error] ======================
[error] Full classname legend:
[error] ======================
[error] MyClass: "MyClass"
[error] Map:              "scala.collection.immutable.Map"
[error] ========================
[error] End of classname legend:
[error] ========================
[error] com.google.inject.ConfigurationException: Guice configuration errors:
[error]
[error] 1) [Guice/MissingImplementation]: No implementation for Map<String, Object> was bound.
[error]
[error] Requested by:
[error] 1  : MyClass.<init>(MyClass.scala:25)
[error]       \_ for 1st parameter migrationDefinitions
[error]      while locating MyClass
[error]
[error] Learn more:
[error]   https://github.com/google/guice/wiki/MISSING_IMPLEMENTATION
[error]
[error] 1 error
[error]
[error] ======================
[error] Full classname legend:
[error] ======================
[error] MyClass: "MyClass"
[error] Map:              "scala.collection.immutable.Map"
[error] ========================
[error] End of classname legend:
[error] ========================
[error]
[error]         at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1126)
[error]         at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1133)
[error]         at net.codingwell.scalaguice.InjectorExtensions$ScalaInjector$.instance$extension(InjectorExtensions.scala:27)
...
[error]         at scala.Function0.apply$mcV$sp(Function0.scala:39)
[error]         at scala.Function0.apply$mcV$sp$(Function0.scala:39)
[error]         at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
[error]         at scala.App.$anonfun$main$1(App.scala:73)
[error]         at scala.App.$anonfun$main$1$adapted(App.scala:73)
[error]         at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:553)
[error]         at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:551)
[error]         at scala.collection.AbstractIterable.foreach(Iterable.scala:921)
[error]         at scala.App.main(App.scala:73)
[error]         at scala.App.main$(App.scala:71)
...
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
[error]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.base/java.lang.reflect.Method.invoke(Method.java:568)
[error] stack trace is suppressed; run 'last migration-manager / Compile / bgRun' for the full output
[error] Nonzero exit code: 1
[error] (migration-manager / Compile / run) Nonzero exit code: 1
[error] Total time: 5 s, completed Mar. 7, 2022, 1:01:56 a.m.

We can see from the logs that it considers Int to be an Object.

This can be worked around by using Integer instead of Int.

Issue seems to be related to or have the same root cause as #56.