Skip to content

Commit 4fbc045

Browse files
bcorsoDagger Team
authored andcommitted
Fix an issue where subcomponent factory method fails when defined with type parameters.
This CL updates `BindingGraph.factoryMethodRequirements()` to explicitly resolve the factory method's parameters against the parent component's concrete type using `asMemberOf(parentType)` to properly handle cases where a component might inherit from a generic interface. For example, ``` @component interface Parent extends SubcomponentProvider<Child, ChildModule> {} interface SubcomponentProvider<C, M> { C createSubcomponent(M module); } ``` Previously, the compiler failed to resolve the parameter `M` to the concrete `ChildModule` type which leads to the following error: ``` java.lang.IllegalArgumentException at plugin.apt.turbine//com.google.common.base.Preconditions.checkArgument(Preconditions.java:136) at plugin.apt.turbine//dagger.internal.codegen.binding.ComponentRequirement.forModule(ComponentRequirement.java:190) at plugin.apt.turbine//dagger.internal.codegen.binding.BindingGraph.lambda$factoryMethodRequirements$0(BindingGraph.java:450) ``` RELNOTES=N/A PiperOrigin-RevId: 972676784
1 parent b7edb67 commit 4fbc045

3 files changed

Lines changed: 138 additions & 2 deletions

File tree

dagger-compiler/main/java/dagger/internal/codegen/binding/BindingGraph.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
2828

2929
import androidx.room3.compiler.processing.XExecutableElement;
30+
import androidx.room3.compiler.processing.XExecutableType;
3031
import androidx.room3.compiler.processing.XMethodElement;
32+
import androidx.room3.compiler.processing.XType;
3133
import androidx.room3.compiler.processing.XTypeElement;
3234
import com.google.auto.value.AutoValue;
3335
import com.google.auto.value.extension.memoized.Memoized;
@@ -409,8 +411,10 @@ public final Optional<XMethodElement> factoryMethod() {
409411
*/
410412
// TODO(dpb): Consider disallowing modules if none of their bindings are used.
411413
public final ImmutableSet<ComponentRequirement> factoryMethodRequirements() {
412-
return factoryMethod().get().getParameters().stream()
413-
.map(parameter -> ComponentRequirement.forModule(parameter.getType()))
414+
XType parentType = componentPath().parentComponent().xprocessing().getType();
415+
XExecutableType resolvedFactoryMethod = factoryMethod().get().asMemberOf(parentType);
416+
return resolvedFactoryMethod.getParameterTypes().stream()
417+
.map(ComponentRequirement::forModule)
414418
.collect(toImmutableSet());
415419
}
416420

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2026 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.functional.kotlinsrc.subcomponent
18+
19+
import com.google.common.truth.Truth.assertThat
20+
import dagger.Component
21+
import dagger.Module
22+
import dagger.Provides
23+
import dagger.Subcomponent
24+
import org.junit.Test
25+
import org.junit.runner.RunWith
26+
import org.junit.runners.JUnit4
27+
28+
/** Tests for subcomponent factory methods defined on generic supertypes. */
29+
@RunWith(JUnit4::class)
30+
class GenericSubcomponentFactoryMethodTest {
31+
@Component(modules = [ParentModule::class])
32+
internal interface Parent : SubcomponentProvider<Child, ChildModule> {}
33+
34+
@Module
35+
internal class ParentModule {
36+
@Provides fun provideInt(): Int = 42
37+
}
38+
39+
@Subcomponent(modules = [ChildModule::class])
40+
internal interface Child {
41+
fun string(): String
42+
}
43+
44+
@Module
45+
internal class ChildModule(val s: String) {
46+
@Provides fun provideString(i: Int): String = s + i
47+
}
48+
49+
interface SubcomponentProvider<C, M> {
50+
fun createSubcomponent(module: M): C
51+
}
52+
53+
@Test
54+
fun factoryMethod_genericSupertype() {
55+
val parent: Parent = DaggerGenericSubcomponentFactoryMethodTest_Parent.create()
56+
val child: Child = parent.createSubcomponent(ChildModule("hello "))
57+
assertThat(child.string()).isEqualTo("hello 42")
58+
}
59+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2026 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.functional.subcomponent;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import dagger.Component;
22+
import dagger.Module;
23+
import dagger.Provides;
24+
import dagger.Subcomponent;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
/** Tests for subcomponent factory methods defined on generic supertypes. */
30+
@RunWith(JUnit4.class)
31+
public final class GenericSubcomponentFactoryMethodTest {
32+
33+
@Component(modules = ParentModule.class)
34+
interface Parent extends SubcomponentProvider<Child, ChildModule> {}
35+
36+
@Module
37+
static class ParentModule {
38+
@Provides
39+
int provideInt() {
40+
return 42;
41+
}
42+
}
43+
44+
@Subcomponent(modules = ChildModule.class)
45+
interface Child {
46+
String string();
47+
}
48+
49+
@Module
50+
static class ChildModule {
51+
final String s;
52+
53+
ChildModule(String s) {
54+
this.s = s;
55+
}
56+
57+
@Provides
58+
String provideString(int i) {
59+
return s + i;
60+
}
61+
}
62+
63+
public interface SubcomponentProvider<C, M> {
64+
C createSubcomponent(M module);
65+
}
66+
67+
@Test
68+
public void factoryMethod_genericSupertype() {
69+
Parent parent = DaggerGenericSubcomponentFactoryMethodTest_Parent.create();
70+
Child child = parent.createSubcomponent(new ChildModule("hello "));
71+
assertThat(child.string()).isEqualTo("hello 42");
72+
}
73+
}

0 commit comments

Comments
 (0)