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>.isInstance(instance: T) = Module { it.bind(this.java).toInstance(instance) }
9 fun <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider { instance } }
10 fun <T : Any> KClass<T>.isProvidedBy(provider: com.google.inject.Provider<T>) = Module { it.bind(this.java).toProvider(provider) }
11 fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module { it.bind(this.java).toProvider(provider.java) }
12 inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider { mock<T>() } }
14 inline fun <reified T : Any> Injector.getInstance() = getInstance(T::class.java)!!
16 fun <T : Any> supply(javaClass: Class<T>): Source<T> = object : Source<T> {
17 override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) }
18 override fun byInstance(instance: T) = Module { it.bind(javaClass).toProvider { instance } }
19 override fun byProvider(provider: com.google.inject.Provider<T>) = Module { it.bind(javaClass).toProvider(provider) }
20 override fun byProvider(provider: Class<Provider<T>>) = Module { it.bind(javaClass).toProvider(provider) }
23 interface Source<T : Any> {
24 fun fromInstance(instance: T): Module
25 fun byInstance(instance: T): Module
26 fun byProvider(provider: com.google.inject.Provider<T>): Module
27 fun byProvider(provider: Class<Provider<T>>): Module