File tree 4 files changed +34
-0
lines changed
main/kotlin/org/jlleitschuh/guice
test/kotlin/org/jlleitschuh/guice
4 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,8 @@ This is the equivalent in Kotlin:
53
53
fun main (vararg args : String ) {
54
54
val myModule = module {
55
55
bind(SomeService ::class ).to(SomeServiceImpl ::class )
56
+ // Or, even simpler with reified generics
57
+ bind<SomeService >().to<SomeServiceImpl >()
56
58
}
57
59
val injector = Guice .createInjector(myModule)
58
60
}
Original file line number Diff line number Diff line change @@ -15,6 +15,9 @@ internal constructor(private val binder: Binder) : Binder by binder {
15
15
*/
16
16
fun binder () = this
17
17
18
+ inline fun <reified T : Any > bind (): AnnotatedBindingBuilderScope <T > =
19
+ bind(typeLiteral<T >())
20
+
18
21
/* *
19
22
* See the EDSL examples at [Binder].
20
23
*/
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package org.jlleitschuh.guice.binder
3
3
import com.google.inject.Key
4
4
import com.google.inject.TypeLiteral
5
5
import com.google.inject.binder.LinkedBindingBuilder
6
+ import org.jlleitschuh.guice.typeLiteral
6
7
import kotlin.reflect.KClass
7
8
8
9
open class LinkedBindingBuilderScope <T : Any >(
@@ -14,6 +15,12 @@ open class LinkedBindingBuilderScope<T : Any>(
14
15
* a mistake to make.
15
16
*/
16
17
18
+ /* *
19
+ * See the EDSL examples at [com.google.inject.Binder].
20
+ */
21
+ inline fun <reified T2 : T > to () =
22
+ to(typeLiteral<T2 >())
23
+
17
24
/* *
18
25
* See the EDSL examples at [com.google.inject.Binder].
19
26
*/
@@ -37,4 +44,16 @@ open class LinkedBindingBuilderScope<T : Any>(
37
44
*/
38
45
override fun to (targetKey : Key <out T >): ScopedBindingBuilderScope =
39
46
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 )
40
59
}
Original file line number Diff line number Diff line change @@ -21,6 +21,16 @@ class ModuleTest {
21
21
assertTrue(theInterface is Implementation )
22
22
}
23
23
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
+
24
34
@Test
25
35
fun `module not using key` () {
26
36
val simpleModule = module {
You can’t perform that action at this time.
0 commit comments