Default
Specifies the default AutoBindIntoSet.bindGenericAs and AutoBindIntoMap.bindGenericAs for the annotated type.
This is useful if you know that the generic is always unique or if it's always shared. For example, an Interceptor<T>
should probably be bound as ExactType because you want to get all interceptors for a given type where as Resource<T>
should probably be bound as Wildcard since you're likely interested in getting all resources because there are no duplicates.
You can only add this to generic types.
Example when using wildcard:
@BindGenericAs.Default(BindGenericAs.Wildcard)
interface Resource<T> {
fun produce(): T
fun close()
}
@AutoBindIntoSet // Will bind this as Resource<*> instead of Resource<SomeThing>
class SomeResource @Inject constructor() : Resource<SomeThing> {
...
override fun close() { ... }
}
class ResourceCloser @Inject constructor(
val resources: Set<@JvmSuppressWildcards Resource<*>>
) {
fun closeAllResources() {
resources.forEach { it.close() }
}
}
Content copied to clipboard
Example when using exact type:
@BindGenericAs.Default(BindGenericAs.Type)
interface Interceptor<T> {
fun intercept(value: T): T
}
@AutoBindIntoSet // Will bind this as Interceptor<SomeThing>
class SomeInterceptor @Inject constructor() : Interceptor<SomeThing> {
...
}
class SomeOperation @Inject constructor(
val interceptors: Set<@JvmSuppressWildcards Interceptor<SomeThing>>
) {
fun performOperation(value: SomeThing): SomeThing {
return interceptors.fold(value) { v, interceptor -> interceptor.intercept(v) }
}
}
Content copied to clipboard
Since
1.0.0