Skip to content

Commit

Permalink
Associate Dagger Android output with generated Component.
Browse files Browse the repository at this point in the history
fixes #4181

RELNOTES=n/a
PiperOrigin-RevId: 618273295
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Mar 22, 2024
1 parent 29d9a8e commit fc2363d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions java/dagger/internal/codegen/binding/ModuleDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public abstract class ModuleDescriptor {
/** The kind of the module. */
public abstract ModuleKind kind();

/** Whether the module is implicitly included rather than directly referenced in annotation. */
public abstract Boolean isImplicitlyIncluded();

/** Returns all of the bindings declared in this module. */
@Memoized
public ImmutableSet<BindingDeclaration> allBindingDeclarations() {
Expand Down Expand Up @@ -107,6 +110,7 @@ public static final class Factory implements ClearableCache {
private final OptionalBindingDeclaration.Factory optionalBindingDeclarationFactory;
private final DaggerSuperficialValidation superficialValidation;
private final Map<XTypeElement, ModuleDescriptor> cache = new HashMap<>();
private final Set<XTypeElement> implicitlyIncludedModules = new LinkedHashSet<>();

@Inject
Factory(
Expand Down Expand Up @@ -174,7 +178,8 @@ public ModuleDescriptor createUncached(XTypeElement moduleElement) {
subcomponentDeclarationFactory.forModule(moduleElement),
delegates.build(),
optionalDeclarations.build(),
ModuleKind.forAnnotatedElement(moduleElement).get());
ModuleKind.forAnnotatedElement(moduleElement).get(),
implicitlyIncludedModules.contains(moduleElement));
}

private void collectCompanionModuleBindings(
Expand Down Expand Up @@ -231,7 +236,10 @@ private Set<XTypeElement> collectIncludedModules(
.ifPresent(
moduleAnnotation -> {
includedModules.addAll(moduleAnnotation.includes());
includedModules.addAll(implicitlyIncludedModules(moduleElement));
ImmutableSet<XTypeElement> daggerAndroidModules =
implicitlyIncludedModules(moduleElement);
includedModules.addAll(daggerAndroidModules);
implicitlyIncludedModules.addAll(daggerAndroidModules);
});
return includedModules;
}
Expand Down
11 changes: 11 additions & 0 deletions java/dagger/internal/codegen/writing/ComponentImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static javax.lang.model.element.Modifier.STATIC;
import static javax.tools.Diagnostic.Kind.ERROR;

import androidx.room.compiler.processing.JavaPoetExtKt;
import androidx.room.compiler.processing.XExecutableParameterElement;
import androidx.room.compiler.processing.XMessager;
import androidx.room.compiler.processing.XMethodElement;
Expand Down Expand Up @@ -80,6 +81,7 @@
import dagger.internal.codegen.binding.ComponentRequirement;
import dagger.internal.codegen.binding.KeyVariableNamer;
import dagger.internal.codegen.binding.MethodSignature;
import dagger.internal.codegen.binding.ModuleDescriptor;
import dagger.internal.codegen.compileroption.CompilerOptions;
import dagger.internal.codegen.javapoet.CodeBlocks;
import dagger.internal.codegen.javapoet.TypeNames;
Expand Down Expand Up @@ -706,6 +708,15 @@ public void claimMethodName(CharSequence name) {
public TypeSpec generate() {
TypeSpec.Builder builder = classBuilder(name);

// Ksp requires explicitly associating input classes that are generated with the output class,
// otherwise, the cached generated classes won't be discoverable in an incremental build.
if (processingEnv.getBackend() == XProcessingEnv.Backend.KSP) {
graph.componentDescriptor().modules().stream()
.filter(ModuleDescriptor::isImplicitlyIncluded)
.forEach(
module -> JavaPoetExtKt.addOriginatingElement(builder, module.moduleElement()));
}

if (isComponentShard()) {
TypeSpecs.addSupertype(builder, graph.componentTypeElement());
addCreator();
Expand Down

0 comments on commit fc2363d

Please sign in to comment.