Skip to content

Commit 6cb6626

Browse files
committed
Add better support for creating Guice Modules
1 parent d13b6f8 commit 6cb6626

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ local.properties
8080

8181
## File-based project format:
8282
*.iws
83+
*.iml
8384

8485
## Plugin-specific files:
8586

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.jlleitschuh.guice
2+
3+
import com.google.inject.Binder
4+
import kotlin.reflect.KClass
5+
6+
class BinderScope
7+
internal constructor(private val binder: Binder) : Binder by binder {
8+
9+
/**
10+
* See the EDSL examples at [Binder].
11+
*/
12+
fun <T : Any> bind(klass: KClass<T>) =
13+
binder.bind(klass::class.java)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.jlleitschuh.guice
2+
3+
import com.google.inject.Binder
4+
import com.google.inject.Module
5+
6+
fun module(configure: BinderScope.() -> Unit) =
7+
Module { binder -> BinderScope(binder).configure() }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.jlleitschuh.guice
2+
3+
import com.google.inject.AbstractModule
4+
5+
class ModuleTest {
6+
interface Interface
7+
8+
class Implementation : Interface
9+
10+
val simpleModule = module {
11+
bind(key<Interface>()).to(key<Implementation>())
12+
}
13+
14+
val complicated = object : AbstractModule() {
15+
override fun configure() {
16+
bind(key<Interface>()).to(key<Implementation>())
17+
}
18+
19+
}
20+
}

0 commit comments

Comments
 (0)