diff --git a/src/main/scala/net/codingwell/scalaguice/BindingExtensions.scala b/src/main/scala/net/codingwell/scalaguice/BindingExtensions.scala index df1a392..2e6aae3 100644 --- a/src/main/scala/net/codingwell/scalaguice/BindingExtensions.scala +++ b/src/main/scala/net/codingwell/scalaguice/BindingExtensions.scala @@ -15,7 +15,10 @@ */ package net.codingwell.scalaguice -import com.google.inject._ +import javax.inject.Provider + +import com.google.inject.Binder +import com.google.inject.name.Names /** * Extensions for Guice's binding DSL. @@ -70,6 +73,7 @@ object BindingExtensions { class ScalaAnnotatedConstantBindingBuilder(b: AnnotatedConstantBindingBuilder) { def annotatedWithType[TAnn <: JAnnotation : Manifest] = b annotatedWith cls[TAnn] + def annotatedWithName(name: String) = b annotatedWith Names.named(name) } implicit def enrichAnnotatedConstantBindingBuilder(b: AnnotatedConstantBindingBuilder) = { diff --git a/src/main/scala/net/codingwell/scalaguice/KeyExtensions.scala b/src/main/scala/net/codingwell/scalaguice/KeyExtensions.scala index 9fbdbe9..a4a681f 100644 --- a/src/main/scala/net/codingwell/scalaguice/KeyExtensions.scala +++ b/src/main/scala/net/codingwell/scalaguice/KeyExtensions.scala @@ -16,6 +16,7 @@ package net.codingwell.scalaguice import com.google.inject._ +import com.google.inject.name.Names object KeyExtensions { @@ -25,5 +26,6 @@ object KeyExtensions { def toKey: Key[T] = Key.get(t) def annotatedWith(annotation: JAnnotation): Key[T] = Key.get(t, annotation) def annotatedWith[TAnn <: JAnnotation : Manifest]: Key[T] = Key.get(t, cls[TAnn]) + def annotatedWithName(name: String) = annotatedWith(Names.named(name)) } } diff --git a/src/main/scala/net/codingwell/scalaguice/ScalaModule.scala b/src/main/scala/net/codingwell/scalaguice/ScalaModule.scala index 822ed74..e3242d2 100644 --- a/src/main/scala/net/codingwell/scalaguice/ScalaModule.scala +++ b/src/main/scala/net/codingwell/scalaguice/ScalaModule.scala @@ -15,14 +15,14 @@ */ package net.codingwell.scalaguice -import com.google.inject._ +import com.google.inject.{PrivateModule, PrivateBinder, Binder, Scope, AbstractModule} import binder._ import java.lang.annotation.Annotation import javax.inject.Provider /** * Allows binding via type parameters. Mix into AbstractModule - * (or subclass) to allow using a type parameter instead of + * (or subclass) to allow using a type parameter instead of * classOf[Foo] or new TypeLiteral[Bar[Foo]] {}. * * For example, instead of @@ -77,7 +77,7 @@ trait ScalaModule extends AbstractModule with InternalModule[Binder] { //Hack, no easy way to exclude the bind method that gets added to classes inheriting ScalaModule //So we experimentally figured out how many calls up is the source, so we use that //Commit 52c2e92f8f6131e4a9ea473f58be3e32cd172ce6 has better class exclusion - protected[this] def binderAccess = super.binder.withSource( (new Throwable).getStackTrace()(4) ) // should not need super + protected[this] def binderAccess = super.binder.withSource((new Throwable).getStackTrace()(4)) // should not need super } trait ScalaPrivateModule extends PrivateModule with InternalModule[PrivateBinder] { @@ -90,11 +90,11 @@ trait ScalaPrivateModule extends PrivateModule with InternalModule[PrivateBinder //Hack, no easy way to exclude the bind method that gets added to classes inheriting ScalaModule //So we experimentally figured out how many calls up is the source, so we use that //Commit 52c2e92f8f6131e4a9ea473f58be3e32cd172ce6 has better class exclusion - protected[this] def binderAccess = super.binder.withSource( (new Throwable).getStackTrace()(5) ) // should not need super + protected[this] def binderAccess = super.binder.withSource((new Throwable).getStackTrace()(5)) // should not need super protected[this] def expose[T: Manifest] = new ScalaAnnotatedElementBuilder[T] { - val myBinder = binderAccess - val self = myBinder.expose(typeLiteral[T]) + val myBinder = binderAccess + val self = myBinder.expose(typeLiteral[T]) } } @@ -105,18 +105,17 @@ object ScalaModule { def in[TAnn <: JAnnotation : Manifest]() = self in cls[TAnn] } - trait ScalaLinkedBindingBuilder[T] extends ScalaScopedBindingBuilder - with LinkedBindingBuilderProxy[T] { outer => + trait ScalaLinkedBindingBuilder[T] extends ScalaScopedBindingBuilder with LinkedBindingBuilderProxy[T] { outer => def to[TImpl <: T : Manifest] = new ScalaScopedBindingBuilder { val self = outer.self to typeLiteral[TImpl] } + def toProvider[TProvider <: Provider[_ <: T] : Manifest] = new ScalaScopedBindingBuilder { val self = outer.self toProvider typeLiteral[TProvider] } } - trait ScalaAnnotatedBindingBuilder[T] extends ScalaLinkedBindingBuilder[T] - with AnnotatedBindingBuilderProxy[T] { outer => + trait ScalaAnnotatedBindingBuilder[T] extends ScalaLinkedBindingBuilder[T] with AnnotatedBindingBuilderProxy[T] { outer => def annotatedWith[TAnn <: JAnnotation : Manifest] = new ScalaLinkedBindingBuilder[T] { val self = outer.self annotatedWith cls[TAnn] } diff --git a/src/main/scala/net/codingwell/scalaguice/binder/BindingProxies.scala b/src/main/scala/net/codingwell/scalaguice/binder/BindingProxies.scala index d40d424..5cc511a 100644 --- a/src/main/scala/net/codingwell/scalaguice/binder/BindingProxies.scala +++ b/src/main/scala/net/codingwell/scalaguice/binder/BindingProxies.scala @@ -21,6 +21,7 @@ import com.google.inject.binder._ import java.lang.annotation.{Annotation => JAnnotation} import java.lang.reflect.{Constructor => JConstructor} import net.codingwell.scalaguice.ScalaModule.{ScalaLinkedBindingBuilder, ScalaScopedBindingBuilder} +import com.google.inject.name.Names /** * Proxy for [[com.google.inject.binder.ScopedBindingBuilder]] @@ -66,6 +67,7 @@ trait AnnotatedBindingBuilderProxy[T] extends AnnotatedBindingBuilder[T] with Li def annotatedWith(annotation: JAnnotation) = newBuilder(self annotatedWith annotation) def annotatedWith(annotationType: Class[_ <: JAnnotation]) = newBuilder(self annotatedWith annotationType) + def annotatedWithName(name: String) = annotatedWith(Names.named(name)) private[this] def newBuilder(underlying: LinkedBindingBuilder[T]) = new ScalaLinkedBindingBuilder[T] { val self = underlying @@ -80,4 +82,5 @@ trait AnnotatedElementBuilderProxy[T] extends AnnotatedElementBuilder with Proxy def annotatedWith(annotation: JAnnotation) = self annotatedWith annotation def annotatedWith(annotationType: Class[_ <: JAnnotation]) = self annotatedWith annotationType + def annotatedWithName(name: String) = annotatedWith(Names.named(name)) } \ No newline at end of file diff --git a/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala b/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala index cc8880d..7422786 100644 --- a/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala +++ b/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala @@ -15,9 +15,8 @@ */ package net.codingwell.scalaguice -import com.google.inject.Inject -import com.google.inject.Provider import com.google.inject.TypeLiteral +import javax.inject.{Provider, Inject, Named} object Outer { trait A @@ -67,3 +66,5 @@ class FooProviderWithJavax extends javax.inject.Provider[Foo] { def foo() = "foo" } } + +case class TwoStrings @Inject()(@Named("first") first: String, @Named("second") second: String) diff --git a/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala b/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala index 6e871cb..29c3a47 100644 --- a/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala +++ b/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala @@ -111,6 +111,19 @@ class ScalaModuleSpec extends WordSpec with ShouldMatchers { val sources = messages.iterator.next.getSource assert( sources.contains("ScalaModuleSpec.scala") ) } + + "allow use annotatedWithName" in { + import net.codingwell.scalaguice.BindingExtensions._ + val module = new AbstractModule with ScalaModule { + def configure() = { + bind[String].annotatedWithName("first").toInstance("first") + bindConstant().annotatedWithName("second").to("second") + } + } + val twoStrings = Guice.createInjector(module).getInstance(classOf[TwoStrings]) + twoStrings.first should be ("first") + twoStrings.second should be ("second") + } } }