Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

import dagger.BindsInstance;
import dagger.Component;

@Component
public interface GenericComponentBuilderInterface {
String value();

@Component.Builder
interface Builder extends GenericBuilder<GenericComponentBuilderInterface> {
Builder bindString(@BindsInstance String instance);
}

public interface GenericBuilder<Component> {
Component build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,14 @@ public void subcomponentFactoryProvision() {
assertThat(nested.two()).isEqualTo(2L);
}

@Test
public void genericComponentBuilderInterface() {
GenericComponentBuilderInterface component =
backend.builder(GenericComponentBuilderInterface.Builder.class).bindString("one").build();

assertThat(component.value()).isEqualTo("one");
}

@Test
public void componentBindingInstance() {
ComponentBindingInstance instance = backend.create(ComponentBindingInstance.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

final class ComponentBuilderInvocationHandler implements InvocationHandler {
static <B> B forComponentBuilder(Class<B> builderClass) {
Expand Down Expand Up @@ -87,7 +89,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return method.invoke(proxy, args);
}

Class<?> returnType = method.getReturnType();
Type returnType = resolveReturnType(method);
Type[] parameterTypes = method.getGenericParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();

Expand Down Expand Up @@ -166,4 +168,26 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl

throw new IllegalStateException(method.toString()); // TODO report unsupported method shape
}

private Type resolveReturnType(Method method) {
Type genericReturnType = method.getGenericReturnType();

if (genericReturnType != null) {
Class<?> declaringClass = method.getDeclaringClass();
TypeVariable<? extends Class<?>>[] typeParameters = declaringClass.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) {
if (typeParameters[i].equals(genericReturnType)) {
Type[] genericInterfaces = builderClass.getGenericInterfaces();
for (Type genericInterface : genericInterfaces) {
ParameterizedType implementationType = (ParameterizedType) genericInterface;
if (implementationType.getRawType().equals(declaringClass)) {
return implementationType.getActualTypeArguments()[i];
}
}
}
}
}

return method.getReturnType();
}
}