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 @@ -105,7 +105,7 @@ private static enum Kind {
private boolean isAbstract;
private boolean isFunctional;
private VisibilityModifier visibility = null;
private XElement originatingElement;
private final List<XElement> originatingElements = new ArrayList<>();
private final Set<String> alwaysQualifyNames = new LinkedHashSet<>();
private final List<XCodeBlock> javadocs = new ArrayList<>();
private XTypeName superclass;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -420,7 +420,7 @@ public XTypeSpec build() {
throw new AssertionError();
}

if (originatingElement != null) {
for (XElement originatingElement : originatingElements) {
builder.addOriginatingElement(originatingElement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
57 changes: 57 additions & 0 deletions javatests/dagger/internal/codegen/XTypeSpecsTest.java
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
Loading