diff --git a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java index c5bb4038bfc..4486a32e09b 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypeSpecs.java @@ -105,7 +105,7 @@ private static enum Kind { private boolean isAbstract; private boolean isFunctional; private VisibilityModifier visibility = null; - private XElement originatingElement; + private final List originatingElements = new ArrayList<>(); private final Set alwaysQualifyNames = new LinkedHashSet<>(); private final List javadocs = new ArrayList<>(); private XTypeName superclass; @@ -171,10 +171,10 @@ public Builder addJavadoc(String format, Object... args) { return this; } - /** Sets the originating element of the type. */ + /** Adds an originating element of the type. */ @CanIgnoreReturnValue public Builder addOriginatingElement(XElement originatingElement) { - this.originatingElement = originatingElement; + originatingElements.add(originatingElement); return this; } @@ -420,7 +420,7 @@ public XTypeSpec build() { throw new AssertionError(); } - if (originatingElement != null) { + for (XElement originatingElement : originatingElements) { builder.addOriginatingElement(originatingElement); } diff --git a/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java b/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java index 6c488179286..1d42d814c33 100644 --- a/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java +++ b/javatests/artifacts/dagger/build-tests/src/test/java/buildtests/IncrementalProcessingTest.java @@ -275,9 +275,7 @@ public void testIncrementalProcessingWithGeneratedModule() throws IOException { + "'generated.Generated_AnnotatedClassModule' could not be resolved."); } - // This is a regression test for https://github.com/google/dagger/issues/4181 when multiple - // @ContributesAndroidInjector modules are implicitly included (see - // https://github.com/google/dagger/pull/5360). + // This is a regression test for https://github.com/google/dagger/issues/5363. @Test public void testIncrementalProcessingWithGeneratedAndroidInjectorModules() throws IOException { File projectDir = tmpFolder.getRoot(); @@ -369,12 +367,10 @@ public void testIncrementalProcessingWithGeneratedAndroidInjectorModules() throw // Leave both injector modules unchanged and regenerate only the component's source file. appModule.addSrcFile( "MyComponent.kt", componentContent.replace("dependency()", "dependency2()")); - BuildResult incrementalBuild = buildAndFail(projectDir); - assertThat(incrementalBuild.task(":app:kspKotlin").getOutcome()).isEqualTo(TaskOutcome.FAILED); - assertThat(incrementalBuild.getOutput()) - .contains( - "ComponentProcessingStep was unable to process 'app.MyComponent' because " - + "'app.ActivityModule_Activity' could not be resolved."); + BuildResult incrementalBuild = build(projectDir); + assertThat(incrementalBuild.task(":app:kspKotlin").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); + assertThat(getAllKspGeneratedFileNames(appModule.getDir())) + .containsExactlyElementsIn(initialGeneratedFiles); } private static BuildResult build(File projectDir) { diff --git a/javatests/dagger/internal/codegen/XTypeSpecsTest.java b/javatests/dagger/internal/codegen/XTypeSpecsTest.java new file mode 100644 index 00000000000..7418938bfec --- /dev/null +++ b/javatests/dagger/internal/codegen/XTypeSpecsTest.java @@ -0,0 +1,57 @@ +/* + * 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.internal.codegen; + +import static androidx.room3.compiler.codegen.compat.XConverters.toJavaPoet; +import static com.google.common.truth.Truth.assertThat; +import static dagger.testing.compile.CompilerTests.javaSource; + +import androidx.room3.compiler.codegen.XTypeSpec; +import androidx.room3.compiler.processing.XTypeElement; +import dagger.internal.codegen.xprocessing.XTypeSpecs; +import dagger.testing.compile.CompilerTests; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link XTypeSpecs}. */ +@RunWith(JUnit4.class) +public final class XTypeSpecsTest { + @Test + public void noOriginatingElements() { + assertThat(toJavaPoet(XTypeSpecs.classBuilder("Generated").build()).originatingElements) + .isEmpty(); + } + + @Test + public void addOriginatingElement_preservesAllElements() { + CompilerTests.invocationCompiler( + javaSource("test.First", "package test;", "class First {}"), + javaSource("test.Second", "package test;", "class Second {}")) + .compile(invocation -> { + XTypeElement first = invocation.getProcessingEnv().requireTypeElement("test.First"); + XTypeElement second = invocation.getProcessingEnv().requireTypeElement("test.Second"); + XTypeSpec type = + XTypeSpecs.classBuilder("Generated") + .addOriginatingElement(first) + .addOriginatingElement(second) + .build(); + + assertThat(toJavaPoet(type).originatingElements).hasSize(2); + }); + } +}