OBSOLETE. Anvil provides better functionality. Try it out
Automatic binder for Dagger 2
Automatically generated dagger module with @Binds
methods
implementation "ru.ztrap.tools:auto-binder-core:1.0.6"
kapt "ru.ztrap.tools:auto-binder-processor:1.0.6"
In order to allow compiler generate dagger module and @Binds
methods, define an auto-binder dagger module anywhere in
the same gradle module with @AutoBindTo
classes:
@Module(includes = [AutoBinder_SampleModule::class])
@AutoBinderModule
object SampleModule
interface Contract {
interface IPresenter
}
@AutoBindTo(Contract.IPresenter::class)
class Presenter : Contract.IPresenter
This will generate the following:
@Module
public abstract class AutoBinder_SampleModule {
private AutoBinder_SampleModule() {
}
@Binds
abstract Contract.IPresenter bindPresenterToContract$IPresenter(Presenter binding);
}
interface Interceptor {
fun intercept(pipeline: Pipeline)
}
@AutoBindTo(Interceptor::class, AutoBindTo.Type.INTO_SET)
class FirstInterceptor : Interceptor
@AutoBindTo(Interceptor::class, AutoBindTo.Type.INTO_SET)
class SecondInterceptor : Interceptor
This will generate the following:
@Module
public abstract class AutoBinder_SampleModule {
private AutoBinder_SampleModule() {
}
@Binds
@IntoSet
abstract Interceptor bindFirstInterceptorToInterceptor(FirstInterceptor binding);
@Binds
@IntoSet
abstract Interceptor bindSecondInterceptorToInterceptor(SecondInterceptor binding);
}
Default map keys (representations of dagger.multibindings
-keys)
Dagger name | Our name |
---|---|
ClassKey |
AutoClassKey |
IntKey |
AutoIntKey |
LongKey |
AutoLongKey |
StringKey |
AutoStringKey |
interface Interceptor {
fun intercept(pipeline: Pipeline)
}
@AutoStringKey("first")
@AutoBindTo(Interceptor::class, AutoBindTo.Type.INTO_MAP)
class FirstInterceptor : Interceptor
@AutoStringKey("second")
@AutoBindTo(Interceptor::class, AutoBindTo.Type.INTO_MAP)
class SecondInterceptor : Interceptor
This will generate the following:
@Module
public abstract class AutoBinder_SampleModule {
private AutoBinder_SampleModule() {
}
@Binds
@IntoMap
@StringKey("first")
abstract Interceptor bindFirstInterceptorToInterceptor(FirstInterceptor binding);
@Binds
@IntoMap
@StringKey("second")
abstract Interceptor bindSecondInterceptorToInterceptor(SecondInterceptor binding);
}
Annotation marked with dagger.Qualifier
annotation will be automatically copied to generated @Binds
method only if parameter translateQualifier
== true
in @AutoBindTo
interface Interceptor {
fun intercept(pipeline: Pipeline)
}
@Named("first")
@AutoBindTo(Interceptor::class, AutoBindTo.Type.INTO_SET)
class FirstInterceptor : Interceptor
@Named("second")
@AutoBindTo(Interceptor::class, AutoBindTo.Type.INTO_SET)
class SecondInterceptor : Interceptor
This will generate the following:
@Module
public abstract class AutoBinder_SampleModule {
private AutoBinder_SampleModule() {
}
@Binds
@IntoSet
@Named("first")
abstract Interceptor bindFirstInterceptorToInterceptor(FirstInterceptor binding);
@Binds
@IntoSet
@Named("second")
abstract Interceptor bindSecondInterceptorToInterceptor(SecondInterceptor binding);
}
- Peter Gulko
- ztrap.developer@gmail.com
- paypal.me/zTrap
Copyright 2019 zTrap.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.