Skip to content

Instantly share code, notes, and snippets.

@ZacSweers ZacSweers/Details.md Secret
Created Apr 25, 2019

Embed
What would you like to do?
Dagger Factory Generation

When constructor injecting class with Dagger, the Dagger compiler will generate <Type>_Factory.java classes. This gist demonstrates the generated Java code and Kotlin equivalent code used in the blog post for Kotlin build performance measurements.

package com.uber.model.core.generated.example;
import com.google.errorprone.annotations.RestrictedApi;
import com.uber.presidio.realtime.core.Data;
import com.uber.presidio.realtime.core.RealtimeClient;
import dagger.internal.Factory;
import javax.annotation.Generated;
import javax.inject.Provider;
@Generated(
value = "com.uber.realtime.android.servicebuilder.RealtimeJavaServiceBuilder",
comments = "This code was generated by the Uber Jenga Compiler for use with Dagger."
)
public final class SampleServiceClient_Factory<D extends Data>
implements Factory<SampleServiceClient<D>> {
private final Provider<RealtimeClient<D>> clientProvider;
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = Generated.class
)
public SampleServiceClient_Factory(Provider<RealtimeClient<D>> clientProvider) {
this.clientProvider = clientProvider;
}
@Override
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = Generated.class
)
public SampleServiceClient<D> get() {
return provideInstance(clientProvider);
}
@SuppressWarnings("CallingProviderGetDuringInit")
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = Generated.class
)
public static <D extends Data> SampleServiceClient<D> provideInstance(
Provider<RealtimeClient<D>> clientProvider) {
return new SampleServiceClient<D>(clientProvider.get());
}
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = Generated.class
)
public static <D extends Data> SampleServiceClient_Factory<D> create(
Provider<RealtimeClient<D>> clientProvider) {
return new SampleServiceClient_Factory<D>(clientProvider);
}
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = Generated.class
)
public static <D extends Data> SampleServiceClient<D> newSampleServiceClient(
RealtimeClient<D> client) {
return new SampleServiceClient<D>(client);
}
}
package com.uber.model.core.generated.example
import com.google.errorprone.annotations.RestrictedApi
import com.uber.presidio.realtime.core.Data
import com.uber.presidio.realtime.core.RealtimeClient
import dagger.internal.Factory
import javax.annotation.Generated
import javax.inject.Provider
import kotlin.jvm.JvmStatic
@Generated(
"com.uber.realtime.android.servicebuilder.kotlin.RealtimeKotlinServiceBuilder",
comments = "This code was generated by the Uber Jenga Compiler for use with Dagger."
)
class SampleServiceClient_Factory<D : Data>(private val clientProvider: Provider<RealtimeClient<D>>)
: Factory<SampleServiceClient<D>> {
override fun get(): SampleServiceClient<D> = provideInstance<D>(clientProvider)
companion object {
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = [Generated::class]
)
@JvmStatic
fun <D : Data> provideInstance(clientProvider: Provider<RealtimeClient<D>>):
SampleServiceClient<D> = SampleServiceClient<D>(clientProvider.get())
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = [Generated::class]
)
@JvmStatic
fun <D : Data> create(clientProvider: Provider<RealtimeClient<D>>):
SampleServiceClient_Factory<D> = SampleServiceClient_Factory<D>(clientProvider)
@RestrictedApi(
explanation = "This should only be accessed from Dagger code",
link = "",
whitelistAnnotations = [Generated::class]
)
@JvmStatic
fun <D : Data> newSampleServiceClient(client: RealtimeClient<D>): SampleServiceClient<D> =
SampleServiceClient<D>(client)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.