Guice Puzzler with TypeLiteral, Scala, and Generic Types 2 layers deep

394 views
Skip to first unread message

Daniel Hinojosa

unread,
Feb 9, 2017, 11:07:44 PM2/9/17
to google-guice
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)
}


Thomas Suckow

unread,
Feb 10, 2017, 10:22:33 AM2/10/17
to google-guice

My guess is the mixing of Scala.collection.seq and scala.collection.immutable.seq


--
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.

Daniel Hinojosa

unread,
Feb 10, 2017, 10:35:38 AM2/10/17
to google-guice
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.
Reply all
Reply to author
Forward
0 new messages