Skip to content

Commit 3302152

Browse files
committed
Continue to expand the Module API exposed
1 parent e3b586b commit 3302152

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package org.jlleitschuh.guice
33
import com.google.inject.Binder
44
import com.google.inject.Key
55
import com.google.inject.TypeLiteral
6-
import com.google.inject.binder.AnnotatedBindingBuilder
76
import org.jlleitschuh.guice.binder.AnnotatedBindingBuilderScope
87
import org.jlleitschuh.guice.binder.LinkedBindingBuilderScope
98
import kotlin.reflect.KClass
109

11-
class BinderScope
10+
open class BinderScope
1211
internal constructor(private val binder: Binder) : Binder by binder {
1312

1413
/**
@@ -25,7 +24,7 @@ internal constructor(private val binder: Binder) : Binder by binder {
2524
/**
2625
* See the EDSL examples at [Binder].
2726
*/
28-
override fun <T : Any> bind(key: Key<T>) =
27+
override fun <T : Any> bind(key: Key<T>): LinkedBindingBuilderScope<T> =
2928
LinkedBindingBuilderScope(binder.bind(key))
3029

3130
/**
@@ -40,4 +39,8 @@ internal constructor(private val binder: Binder) : Binder by binder {
4039
override fun <T : Any> bind(typeLiteral: TypeLiteral<T>): AnnotatedBindingBuilderScope<T> =
4140
AnnotatedBindingBuilderScope(binder.bind(typeLiteral))
4241

42+
43+
override fun newPrivateBinder(): PrivateBinderScope =
44+
PrivateBinderScope(binder.newPrivateBinder())
45+
4346
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package org.jlleitschuh.guice
22

33
import com.google.inject.Module
4+
import com.google.inject.PrivateModule
45

56
/**
67
* Creates a [Module] with the [BinderScope] being configured when [Module.configure]
78
* is called by Guice.
89
*/
910
fun module(configure: BinderScope.() -> Unit) =
10-
Module { binder -> BinderScope(binder).configure() }
11+
Module { binder -> BinderScope(binder).configure() }
12+
13+
/**
14+
* Creates a [PrivateModule] with the [PrivateBinderScope] being configured when [PrivateModule.configure]
15+
* is called by Guice.
16+
*/
17+
fun privateModule(configure: PrivateBinderScope.() -> Unit) =
18+
// The PrivateModule data type has some reflection checks to get the right type passed in. Easier to just use it.
19+
object : PrivateModule() {
20+
override fun configure() =
21+
configure.invoke(PrivateBinderScope(binder()))
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.jlleitschuh.guice
2+
3+
import com.google.inject.Key
4+
import com.google.inject.PrivateBinder
5+
import com.google.inject.TypeLiteral
6+
import com.google.inject.binder.AnnotatedElementBuilder
7+
8+
class PrivateBinderScope
9+
internal constructor(private val privateBinder: PrivateBinder): BinderScope(privateBinder), PrivateBinder {
10+
11+
override fun skipSources(vararg classesToSkip: Class<*>): PrivateBinderScope {
12+
return PrivateBinderScope(privateBinder.skipSources(*classesToSkip))
13+
}
14+
15+
override fun withSource(source: Any): PrivateBinder {
16+
return PrivateBinderScope(privateBinder.withSource(source))
17+
}
18+
19+
override fun expose(key: Key<*>) {
20+
privateBinder.expose(key)
21+
}
22+
23+
override fun expose(type: Class<*>): AnnotatedElementBuilder {
24+
return privateBinder.expose(type)
25+
}
26+
27+
override fun expose(type: TypeLiteral<*>): AnnotatedElementBuilder {
28+
return privateBinder.expose(type)
29+
}
30+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ class AnnotatedBindingBuilderScope<T : Any>(
1212
/**
1313
* See the EDSL examples at [com.google.inject.Binder].
1414
*/
15-
fun annotatedWith(annotationType: KClass<out Annotation>): LinkedBindingBuilder<T> =
15+
fun annotatedWith(annotationType: KClass<out Annotation>): LinkedBindingBuilderScope<T> =
1616
annotatedWith(annotationType.java)
1717

1818
/**
1919
* See the EDSL examples at [com.google.inject.Binder].
2020
*/
21-
override fun annotatedWith(annotationType: Class<out Annotation>): LinkedBindingBuilder<T> =
21+
override fun annotatedWith(annotationType: Class<out Annotation>): LinkedBindingBuilderScope<T> =
2222
LinkedBindingBuilderScope(annotatedBindingBuilder.annotatedWith(annotationType))
2323

2424
/**
2525
* See the EDSL examples at [com.google.inject.Binder].
2626
*/
27-
override fun annotatedWith(annotation: Annotation): LinkedBindingBuilder<T> =
27+
override fun annotatedWith(annotation: Annotation): LinkedBindingBuilderScope<T> =
2828
LinkedBindingBuilderScope(annotatedBindingBuilder.annotatedWith(annotation))
2929

3030
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ open class LinkedBindingBuilderScope<T : Any>(
1818
* See the EDSL examples at [com.google.inject.Binder].
1919
*/
2020
fun to(implementation: KClass<out T>): ScopedBindingBuilderScope =
21-
ScopedBindingBuilderScope(to(implementation.java))
21+
to(implementation.java)
2222

2323
/**
2424
* See the EDSL examples at [com.google.inject.Binder].

0 commit comments

Comments
 (0)