8000 Merge branch 'pull91' into develop · codingwell/scala-guice@ba04338 · GitHub
[go: up one dir, main page]

Skip to content

Commit ba04338

Browse files
committed
Merge branch 'pull91' into develop
2 parents 6ad4695 + c723d6f commit ba04338

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/main/scala/net/codingwell/scalaguice/InjectorExtensions.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package net.codingwell.scalaguice
1717

18-
import com.google.inject.{Binding, Key, Injector}
18+
import com.google.inject.{Binding, Injector, Key, Provider}
1919
import java.lang.annotation.Annotation
2020
import KeyExtensions._
2121
import scala.reflect.ClassTag
@@ -32,5 +32,9 @@ object InjectorExtensions {
3232
def existingBinding[T: TypeTag](ann: Annotation): Option[Binding[T]] = existingBinding(typeLiteral[T].annotatedWith(ann))
3333
def existingBinding[T: TypeTag, Ann <: Annotation : ClassTag]: Option[Binding[T]] = existingBinding(typeLiteral[T].annotatedWith[Ann])
3434
def existingBinding[T](key: Key[T]): Option[Binding[T]] = Option(self.getExistingBinding(key))
35+
36+
def provider[T: TypeTag]: Provider[T] = self.getProvider(typeLiteral[T].toKey)
37+
def provider[T: TypeTag](ann: Annotation): Provider[T] = self.getProvider(typeLiteral[T].annotatedWith(ann))
38+
def provider[T: TypeTag, Ann <: Annotation : ClassTag]: Provider[T] = self.getProvider(typeLiteral[T].annotatedWith[Ann])
3539
}
3640
}

src/test/scala/net/codingwell/scalaguice/InjectorExtensionsSpec.scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class InjectorExtensionsSpec extends WordSpec with Matchers {
4848
inst.get should equal("String")
4949
}
5050

51-
"allow instance to be retreived using a type parameter and an annotation" in {
51+
"allow instance to be retrieved using a type parameter and an annotation" in {
5252
injector.instance[A](named("d"))
5353
}
5454

55-
"allow instance to be retreived using a type parameter and an annotation class" in {
55+
"allow instance to be retrieved using a type parameter and an annotation class" in {
5656
injector.instance[B, Named]
5757
}
5858

@@ -95,5 +95,22 @@ class InjectorExtensionsSpec extends WordSpec with Matchers {
9595
"allow missing bindings to be retrieved by key optionally" in {
9696
injector.existingBinding[A](keyMissingFn[A]) should not be defined
9797
}
98+
99+
"allow provider to be retrieved using a type parameter" in {
100+
injector.provider[A]
101+
}
102+
103+
"allow generic provider to be retrieved using a type parameter" in {
104+
val inst = injector.provider[Gen[String]]
105+
inst.get.get should equal("String")
106+
}
107+
108+
"allow provider to be retrieved using a type parameter and an annotation" in {
109+
injector.provider[A](named("d"))
110+
}
111+
112+
"allow provider to be retrieved using a type parameter and an annotation class" in {
113+
injector.provider[B, Named]
114+
}
98115
}
99116
}

0 commit comments

Comments
 (0)
0