1 package net.pterodactylus.sone.test
3 import com.google.inject.Injector
4 import com.google.inject.Module
5 import javax.inject.Provider
6 import kotlin.reflect.KClass
8 fun <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider(Provider<T> { instance }) }
9 fun <T : Any> KClass<T>.isProvidedBy(provider: com.google.inject.Provider<T>) = Module { it.bind(this.java).toProvider(provider) }
10 fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module { it.bind(this.java).toProvider(provider.java) }
11 inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider(Provider<T> { mock() }) }
13 inline fun <reified T : Any> Injector.getInstance() = getInstance(T::class.java)!!
15 fun <T : Any> supply(javaClass: Class<T>): Source<T> = object : Source<T> {
16 override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) }
17 override fun byInstance(instance: T) = Module { it.bind(javaClass).toProvider(Provider<T> { instance }) }
18 override fun byProvider(provider: com.google.inject.Provider<T>) = Module { it.bind(javaClass).toProvider(provider) }
19 override fun byProvider(provider: Class<Provider<T>>) = Module { it.bind(javaClass).toProvider(provider) }
22 interface Source<T : Any> {
23 fun fromInstance(instance: T): Module
24 fun byInstance(instance: T): Module
25 fun byProvider(provider: com.google.inject.Provider<T>): Module
26 fun byProvider(provider: Class<Provider<T>>): Module