🔀 Merge changes from other next branch
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / test / Guice.kt
index 2fc4a96..c8f2417 100644 (file)
@@ -1,11 +1,10 @@
 package net.pterodactylus.sone.test
 
-import com.google.inject.Injector
-import com.google.inject.Module
+import com.google.inject.*
 import com.google.inject.name.*
 import org.mockito.*
 import javax.inject.Provider
-import kotlin.reflect.KClass
+import kotlin.reflect.*
 
 fun <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider(Provider<T> { instance }) }
 fun <T : Any> KClass<T>.withNameIsProvidedBy(instance: T, name: String) = Module { it.bind(this.java).annotatedWith(Names.named(name)).toProvider(Provider<T> { instance }) }
@@ -14,7 +13,9 @@ fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module
 inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider(Provider<T> { mock() }) }
 inline fun <reified T : Any> KClass<T>.isProvidedByDeepMock() = Module { it.bind(this.java).toProvider(Provider<T> { deepMock() }) }
 
-inline fun <reified T : Any> Injector.getInstance() = getInstance(T::class.java)!!
+inline fun <reified T : Any> Injector.getInstance(annotation: Annotation? = null): T = annotation
+               ?.let { getInstance(Key.get(object : TypeLiteral<T>() {}, it)) }
+               ?: getInstance(Key.get(object : TypeLiteral<T>() {}))
 
 fun <T : Any> supply(javaClass: Class<T>): Source<T> = object : Source<T> {
        override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) }