Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotatedWith(KClass<out Annotation>) functions #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Thu Sep 12 16:31:13 CDT 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package dev.misfitlabs.kotlinguice4.binder

import com.google.inject.binder.AnnotatedBindingBuilder
import kotlin.reflect.KClass

/**
* An extension of [AnnotatedBindingBuilder] that enhances the binding DSL to allow binding using
Expand All @@ -40,6 +41,12 @@ abstract class KotlinAnnotatedBindingBuilder<T>(private val self: AnnotatedBindi
return this
}

/** Binds with the annotation specified by the class. */
fun annotatedWith(annotationClass: KClass<out Annotation>): KotlinLinkedBindingBuilder<T> {
delegate.annotatedWith(annotationClass.java)
return this
}

/** Binds with the specified annotation. */
override fun annotatedWith(annotation: Annotation): KotlinLinkedBindingBuilder<T> {
delegate.annotatedWith(annotation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package dev.misfitlabs.kotlinguice4.binder

import com.google.inject.binder.AnnotatedElementBuilder
import kotlin.reflect.KClass

/**
* An extension of [AnnotatedElementBuilder] that enhances the binding DSL to allow binding using
Expand All @@ -34,4 +35,9 @@ class KotlinAnnotatedElementBuilder(val delegate: AnnotatedElementBuilder) :
inline fun <reified TAnn : Annotation> annotatedWith() {
delegate.annotatedWith(TAnn::class.java)
}

/** Binds with the annotation specified by the class. */
fun annotatedWith(annotation: KClass<out Annotation>) {
delegate.annotatedWith(annotation.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ object KotlinBinderSpec : Spek({
a.get() shouldEqual "Impl of A"
}

it("binds with an annotation using a type argument") {
val injector = Guice.createInjector(object : KotlinModule() {
override fun configure() {
kotlinBinder.bind<A>().to<B>()
kotlinBinder.bind<A>().annotatedWith(Annotated::class).to<AImpl>()
}
})

val a = injector.getInstance(Key.get(A::class.java, Annotated::class.java))

a.get() shouldEqual "Impl of A"
}

it("binds to a provider using a type parameter") {
val injector = Guice.createInjector(object : KotlinModule() {
override fun configure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ object KotlinPrivateBinderSpec : Spek({
a.get() shouldEqual "Impl of A"
}

it("binds with an annotation using a class") {
val injector = Guice.createInjector(object : KotlinPrivateModule() {
override fun configure() {
kotlinBinder.bind<A>().to<B>()
kotlinBinder.bind<A>().annotatedWith(Annotated::class).to<AImpl>()

kotlinBinder.expose<A>().annotatedWith(Annotated::class)
}
})

val a = injector.getInstance(annotatedKey<A, Annotated>())

a.get() shouldEqual "Impl of A"
}

it("binds to a provider using a type parameter") {
val injector = Guice.createInjector(object : KotlinPrivateModule() {
override fun configure() {
Expand Down