com.itangcent.intellij.extend.guice.Injectors.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guice-action Show documentation
Show all versions of guice-action Show documentation
Help for developing plugins for JetBrains products.
KotlinAnAction:provide ActionContext(support inject guice) for actionPerformed
The newest version!
package com.itangcent.intellij.extend.guice
import com.google.inject.ConfigurationException
import com.google.inject.Injector
import com.google.inject.ProvisionException
import com.google.inject.Singleton
import com.google.inject.binder.LinkedBindingBuilder
import com.google.inject.binder.ScopedBindingBuilder
import kotlin.reflect.KClass
/**
* Returns the appropriate instance for the given injection type; equivalent to {@code
* getProvider(type).get()}. When feasible, avoid using this method, in favor of having Guice
* inject your dependencies ahead of time.
*
* @throws ConfigurationException if this injector cannot find or create the provider.
* @throws ProvisionException if there was a runtime failure while providing an instance.
*/
fun Injector.instance(kClass: KClass): T {
return this.getInstance(kClass.java)
}
/**
* See the EDSL examples at [com.google.inject.Binder].
*
* @see com.google.inject.binder.ScopedBindingBuilder#in
*/
fun ScopedBindingBuilder.at(scopeAnnotation: KClass) {
return this.`in`(scopeAnnotation.java)
}
/**
* wrap [`in`(Singleton::class.java)]
*
* @see com.google.inject.binder.ScopedBindingBuilder#in
*/
fun ScopedBindingBuilder.singleton() {
return this.`in`(Singleton::class.java)
}
/** See the EDSL examples at [com.google.inject.Binder]. */
fun LinkedBindingBuilder.with(implementation: KClass): ScopedBindingBuilder {
return this.to(implementation.java)
}
/** See the EDSL examples at [com.google.inject.Binder]. */
@Suppress("UNCHECKED_CAST")
fun LinkedBindingBuilder.withUnsafe(implementation: KClass<*>): ScopedBindingBuilder {
return this.to(implementation.java as Class)
}
/**
* #todo:provider暂时无法完成PostConstruct
*/
inline fun LinkedBindingBuilder.withProvider(noinline provider: () -> TE): ScopedBindingBuilder {
return this.toProvider(com.google.inject.Provider { provider() })
}