com.cedarsoft.guava.GuiceExtensions.kt Maven / Gradle / Ivy
package com.cedarsoft.guava
import com.google.inject.Binder
import com.google.inject.Guice
import com.google.inject.Injector
import com.google.inject.Module
import com.google.inject.Singleton
import com.google.inject.binder.AnnotatedBindingBuilder
import com.google.inject.binder.LinkedBindingBuilder
import com.google.inject.binder.ScopedBindingBuilder
import java.util.ArrayList
import javax.inject.Inject
import javax.inject.Provider
/**
* Contains extension methods for Guice
* @author Johannes Schneider ([email protected])
*/
open class GuiceInjectorBuilder() {
private val collected = ArrayList()
fun module(config: Binder.() -> Any?): Module {
return Module {
binder ->
binder.config()
}
}
fun Module.plus() {
collected.add(this)
}
companion object {
fun injector(config: GuiceInjectorBuilder.() -> Any?): Injector {
val collector = GuiceInjectorBuilder()
collector.config()
return Guice.createInjector(collector.collected)
}
}
}
fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
inline fun Binder.bind() = bind(T::class.java)!!
inline fun AnnotatedBindingBuilder.to() = to(T::class.java)!!
inline fun AnnotatedBindingBuilder.toSingleton() = to(T::class.java)!!.asSingleton()
inline fun Injector.getInstance() = getInstance(T::class.java)!!
inline fun Injector.getProvider() = getProvider(T::class.java)!!
inline fun Binder.getProvider(): com.google.inject.Provider = getProvider(T::class.java)
inline fun > LinkedBindingBuilder.toProvider() = toProvider(S::class.java)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy