I have this Dagger module. I want to understand the generated code so I can verify that my Dagger configuration is optimal.
@Module
public class TypefaceModule {
@Provides @Singleton @Named("Roboto Light")
static Typeface provideRobotoLight(AssetManager assets) {
return Typeface.createFromAsset(assets, "fonts/Roboto-Light.ttf");
}
}
Here's the generated code (Dagger 2.14.1):
public final class TypefaceModule_ProvideRobotoLightFactory implements Factory<Typeface> {
private final Provider<AssetManager> assetsProvider;
public TypefaceModule_ProvideRobotoLightFactory(Provider<AssetManager> assetsProvider) {
this.assetsProvider = assetsProvider;
}
@Override
public Typeface get() {
return Preconditions.checkNotNull(
TypefaceModule.provideRobotoLight(assetsProvider.get()),
"Cannot return null from a non-@Nullable @Provides method");
}
public static TypefaceModule_ProvideRobotoLightFactory create(
Provider<AssetManager> assetsProvider) {
return new TypefaceModule_ProvideRobotoLightFactory(assetsProvider);
}
public static Typeface proxyProvideRobotoLight(AssetManager assets) {
return Preconditions.checkNotNull(
TypefaceModule.provideRobotoLight(assets),
"Cannot return null from a non-@Nullable @Provides method");
}
}
There are two functions which do almost the same thing: the instance method get()
, and the static method proxyProvideRobotoLight()
.
Why has Dagger generated two versions of this code, which both call the module's provide()
method statically? Can't one call the other?
(Incidentally, I do realise that I no longer need to bundle fonts in my app assets. That's not the question here.)
.get
and.proxyProvide
) it is called without using module instance, otherwiseget
would use references passed and stored via constructor (including module instance) andproxyProvide
would expect all required details to be passed as arguments. Suppose there is still room for improvement, i.e. detect and reuse static method in generated code. – Gyn