Skip to content
Merged
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
Expand Up @@ -27,7 +27,9 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;

import androidx.room3.compiler.processing.XExecutableElement;
import androidx.room3.compiler.processing.XExecutableType;
import androidx.room3.compiler.processing.XMethodElement;
import androidx.room3.compiler.processing.XType;
import androidx.room3.compiler.processing.XTypeElement;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
Expand Down Expand Up @@ -409,8 +411,10 @@ public final Optional<XMethodElement> factoryMethod() {
*/
// TODO(dpb): Consider disallowing modules if none of their bindings are used.
public final ImmutableSet<ComponentRequirement> factoryMethodRequirements() {
return factoryMethod().get().getParameters().stream()
.map(parameter -> ComponentRequirement.forModule(parameter.getType()))
XType parentType = componentPath().parentComponent().xprocessing().getType();
XExecutableType resolvedFactoryMethod = factoryMethod().get().asMemberOf(parentType);
return resolvedFactoryMethod.getParameterTypes().stream()
.map(ComponentRequirement::forModule)
.collect(toImmutableSet());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2026 The Dagger Authors.
*
* 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.
*/

package dagger.functional.kotlinsrc.subcomponent

import com.google.common.truth.Truth.assertThat
import dagger.Component
import dagger.Module
import dagger.Provides
import dagger.Subcomponent
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

/** Tests for subcomponent factory methods defined on generic supertypes. */
@RunWith(JUnit4::class)
class GenericSubcomponentFactoryMethodTest {
@Component(modules = [ParentModule::class])
internal interface Parent : SubcomponentProvider<Child, ChildModule> {}

@Module
internal class ParentModule {
@Provides fun provideInt(): Int = 42
}

@Subcomponent(modules = [ChildModule::class])
internal interface Child {
fun string(): String
}

@Module
internal class ChildModule(val s: String) {
@Provides fun provideString(i: Int): String = s + i
}

interface SubcomponentProvider<C, M> {
fun createSubcomponent(module: M): C
}

@Test
fun factoryMethod_genericSupertype() {
val parent: Parent = DaggerGenericSubcomponentFactoryMethodTest_Parent.create()
val child: Child = parent.createSubcomponent(ChildModule("hello "))
assertThat(child.string()).isEqualTo("hello 42")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2026 The Dagger Authors.
*
* 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.
*/

package dagger.functional.subcomponent;

import static com.google.common.truth.Truth.assertThat;

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import dagger.Subcomponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for subcomponent factory methods defined on generic supertypes. */
@RunWith(JUnit4.class)
public final class GenericSubcomponentFactoryMethodTest {

@Component(modules = ParentModule.class)
interface Parent extends SubcomponentProvider<Child, ChildModule> {}

@Module
static class ParentModule {
@Provides
int provideInt() {
return 42;
}
}

@Subcomponent(modules = ChildModule.class)
interface Child {
String string();
}

@Module
static class ChildModule {
final String s;

ChildModule(String s) {
this.s = s;
}

@Provides
String provideString(int i) {
return s + i;
}
}

public interface SubcomponentProvider<C, M> {
C createSubcomponent(M module);
}

@Test
public void factoryMethod_genericSupertype() {
Parent parent = DaggerGenericSubcomponentFactoryMethodTest_Parent.create();
Child child = parent.createSubcomponent(new ChildModule("hello "));
assertThat(child.string()).isEqualTo("hello 42");
}
}
Loading