Skip to content

Commit 56dbee5

Browse files
committed
Start adding reified generic overloads
1 parent 032ea46 commit 56dbee5

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ This is the equivalent in Kotlin:
5353
fun main(vararg args: String) {
5454
val myModule = module {
5555
bind(SomeService::class).to(SomeServiceImpl::class)
56+
// Or, even simpler with reified generics
57+
bind<SomeService>().to<SomeServiceImpl>()
5658
}
5759
val injector = Guice.createInjector(myModule)
5860
}

core/src/main/kotlin/org/jlleitschuh/guice/BinderScope.kt

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ internal constructor(private val binder: Binder) : Binder by binder {
1515
*/
1616
fun binder() = this
1717

18+
inline fun <reified T: Any> bind(): AnnotatedBindingBuilderScope<T> =
19+
bind(typeLiteral<T>())
20+
1821
/**
1922
* See the EDSL examples at [Binder].
2023
*/

core/src/main/kotlin/org/jlleitschuh/guice/binder/LinkedBindingBuilderScope.kt

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.jlleitschuh.guice.binder
33
import com.google.inject.Key
44
import com.google.inject.TypeLiteral
55
import com.google.inject.binder.LinkedBindingBuilder
6+
import org.jlleitschuh.guice.typeLiteral
67
import kotlin.reflect.KClass
78

89
open class LinkedBindingBuilderScope<T : Any>(
@@ -14,6 +15,12 @@ open class LinkedBindingBuilderScope<T : Any>(
1415
* a mistake to make.
1516
*/
1617

18+
/**
19+
* See the EDSL examples at [com.google.inject.Binder].
20+
*/
21+
inline fun <reified T2 : T> to() =
22+
to(typeLiteral<T2>())
23+
1724
/**
1825
* See the EDSL examples at [com.google.inject.Binder].
1926
*/
@@ -37,4 +44,16 @@ open class LinkedBindingBuilderScope<T : Any>(
3744
*/
3845
override fun to(targetKey: Key<out T>): ScopedBindingBuilderScope =
3946
ScopedBindingBuilderScope(linkedBindingBuilder.to(targetKey))
47+
48+
/**
49+
* See the EDSL examples at [com.google.inject.Binder].
50+
*/
51+
fun toProvider(providerType: KClass<out javax.inject.Provider<out T>>): ScopedBindingBuilderScope =
52+
ScopedBindingBuilderScope(linkedBindingBuilder.toProvider(providerType.java))
53+
54+
/**
55+
* See the EDSL examples at [com.google.inject.Binder].
56+
*/
57+
inline fun <reified P : javax.inject.Provider<out T>> toProvider() =
58+
toProvider(P::class)
4059
}

core/src/test/kotlin/org/jlleitschuh/guice/ModuleTest.kt

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class ModuleTest {
2121
assertTrue(theInterface is Implementation)
2222
}
2323

24+
@Test
25+
fun `simple module reified`() {
26+
val simpleModule = module {
27+
bind<Interface>().to<Implementation>()
28+
}
29+
val injector = Guice.createInjector(simpleModule)
30+
val theInterface = injector.getInstance(key<Interface>())
31+
assertTrue(theInterface is Implementation)
32+
}
33+
2434
@Test
2535
fun `module not using key`() {
2636
val simpleModule = module {

0 commit comments

Comments
 (0)