Discussion:
Guice Puzzler with TypeLiteral, Scala, and Generic Types 2 layers deep
Daniel Hinojosa
2017-02-10 04:07:44 UTC
Permalink
Hello, I am receiving the following stack trace when trying to run this
small Scala/Guice example, I am suspecting that it has to do with
Seq<Tuple2<String, String>> and somehow it is not mapping right. Hope
someone has a good solution.

1) No implementation for
scala.collection.Seq<scala.Tuple2<java.lang.String, java.lang.String>>
annotated with @com.google.inject.name.Named(value=countries) was bound.
while locating scala.collection.Seq<scala.Tuple2<java.lang.String,
java.lang.String>> annotated with
@com.google.inject.name.Named(value=countries)
for the 2nd parameter of MyComponent.<init>(MyComponent.scala:3)
at MyModule.configure(MyModule.scala:18)

2) No implementation for
scala.collection.Seq<scala.Tuple2<java.lang.String, java.lang.String>>
annotated with @com.google.inject.name.Named(value=states) was bound.
while locating scala.collection.Seq<scala.Tuple2<java.lang.String,
java.lang.String>> annotated with
@com.google.inject.name.Named(value=states)
for the 1st parameter of MyComponent.<init>(MyComponent.scala:3)
at MyModule.configure(MyModule.scala:18)

The following is the code, I have three classes, one is the Runner (the
main class), My Component where items will be injected, and MyModule which
is the Guice Module

*MyComponent.scala*

import javax.inject.{Inject, Named}

class MyComponent @Inject() (@Named("states") states: Seq[(String, String)],
@Named("countries") countries: Seq[(String, String)]) {
def stateSize: Int = states.size
def countrySize: Int = countries.size
}



*MyModule.scala*

import com.google.inject.name.Names.named
import com.google.inject.{AbstractModule, Provider, TypeLiteral}

import scala.collection.immutable.Seq

class MyModule extends AbstractModule {
override def configure(): Unit = {
bind(new TypeLiteral[Seq[(String, String)]]() {})
.annotatedWith(named("states"))
.toInstance(Seq("NY" -> "New York", "NJ" -> "New Jersey"))

bind(new TypeLiteral[Seq[(String, String)]]() {})
.annotatedWith(named("countries"))
.toProvider(new Provider[Seq[(String, String)]] {
override def get() = Seq(("US", "United States"), ("MX", "Mexico"))
})

bind(classOf[MyComponent])
}
}


*Runner.scala*


import com.google.inject.{Guice, Injector}


object Runner extends App {
val injector:Injector = Guice.createInjector(new MyModule())

val instance: MyComponent = injector.getInstance(classOf[MyComponent])

println(instance)
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/6af19829-d632-42ff-b534-2a3df8ac63bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thomas Suckow
2017-02-10 15:22:20 UTC
Permalink
My guess is the mixing of Scala.collection.seq and
scala.collection.immutable.seq
Post by Daniel Hinojosa
Hello, I am receiving the following stack trace when trying to run this
small Scala/Guice example, I am suspecting that it has to do with
Seq<Tuple2<String, String>> and somehow it is not mapping right. Hope
someone has a good solution.
1) No implementation for
scala.collection.Seq<scala.Tuple2<java.lang.String, java.lang.String>>
while locating scala.collection.Seq<scala.Tuple2<java.lang.String,
java.lang.String>> annotated with
@com.google.inject.name.Named(value=countries)
for the 2nd parameter of MyComponent.<init>(MyComponent.scala:3)
at MyModule.configure(MyModule.scala:18)
2) No implementation for
scala.collection.Seq<scala.Tuple2<java.lang.String, java.lang.String>>
while locating scala.collection.Seq<scala.Tuple2<java.lang.String,
java.lang.String>> annotated with
@com.google.inject.name.Named(value=states)
for the 1st parameter of MyComponent.<init>(MyComponent.scala:3)
at MyModule.configure(MyModule.scala:18)
The following is the code, I have three classes, one is the Runner (the
main class), My Component where items will be injected, and MyModule which
is the Guice Module
*MyComponent.scala*
import javax.inject.{Inject, Named}
@Named("countries") countries: Seq[(String, String)]) {
def stateSize: Int = states.size
def countrySize: Int = countries.size
}
*MyModule.scala*
import com.google.inject.name.Names.named
import com.google.inject.{AbstractModule, Provider, TypeLiteral}
import scala.collection.immutable.Seq
class MyModule extends AbstractModule {
override def configure(): Unit = {
bind(new TypeLiteral[Seq[(String, String)]]() {})
.annotatedWith(named("states"))
.toInstance(Seq("NY" -> "New York", "NJ" -> "New Jersey"))
bind(new TypeLiteral[Seq[(String, String)]]() {})
.annotatedWith(named("countries"))
.toProvider(new Provider[Seq[(String, String)]] {
override def get() = Seq(("US", "United States"), ("MX", "Mexico"))
})
bind(classOf[MyComponent])
}
}
*Runner.scala*
import com.google.inject.{Guice, Injector}
object Runner extends App {
val injector:Injector = Guice.createInjector(new MyModule())
val instance: MyComponent = injector.getInstance(classOf[MyComponent])
println(instance)
}
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/6af19829-d632-42ff-b534-2a3df8ac63bc%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/6af19829-d632-42ff-b534-2a3df8ac63bc%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAKQgQ_899mGL%3DDnUFz8cRp_9jnh4aU_UW21Fn_HydP0MQ-1Pyw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Daniel Hinojosa
2017-02-10 15:35:38 UTC
Permalink
I did solve it last night. Just woke up and to mention you are right. It was a type alias issue. It was the diff between Scala.Seq and scala.collection.Seq. A total pain. Ha. Thanks.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/81eddf81-c85a-425f-a24e-f3cfac896fae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...