Skip to content

Commit

Permalink
Fix an issue where two @DefineComponent classes with the same simple …
Browse files Browse the repository at this point in the history
…name can create build conflicts by appending a unique suffix.

RELNOTES=Fix issue with @DefineComponent classes with same name
PiperOrigin-RevId: 356363930
  • Loading branch information
Chang-Eric authored and Dagger Team committed Feb 8, 2021
1 parent 53ceb91 commit 785838e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Expand Up @@ -163,15 +163,25 @@ private DefineComponentMetadata getUncached(
? Optional.empty()
: Optional.of(get(parent, childPath));

ClassName componentClassName = ClassName.get(component);

ProcessorErrors.checkState(
parentComponent.isPresent()
|| ClassName.get(component).equals(ClassNames.SINGLETON_COMPONENT),
|| componentClassName.equals(ClassNames.SINGLETON_COMPONENT),
component,
"@DefineComponent %s is missing a parent declaration.\n"
+ "Please declare the parent, for example: @DefineComponent(parent ="
+ " SingletonComponent.class)",
component);

ProcessorErrors.checkState(
componentClassName.equals(ClassNames.SINGLETON_COMPONENT)
|| !componentClassName.simpleName().equals(ClassNames.SINGLETON_COMPONENT.simpleName()),
component,
"Cannot have a component with the same simple name as the reserved %s: %s",
ClassNames.SINGLETON_COMPONENT.simpleName(),
componentClassName);

return new AutoValue_DefineComponentMetadatas_DefineComponentMetadata(
component, scopes, parentComponent);
}
Expand Down
35 changes: 34 additions & 1 deletion java/dagger/hilt/processor/internal/root/RootGenerator.java
Expand Up @@ -16,6 +16,7 @@

package dagger.hilt.processor.internal.root;

import static com.google.common.base.Preconditions.checkState;
import static dagger.hilt.processor.internal.Processors.toClassNames;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static javax.lang.model.element.Modifier.ABSTRACT;
Expand All @@ -40,6 +41,8 @@
import dagger.hilt.processor.internal.ComponentTree;
import dagger.hilt.processor.internal.Processors;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Modifier;
Expand All @@ -58,6 +61,8 @@ static void generate(RootMetadata metadata, ProcessingEnvironment env) throws IO
private final RootMetadata metadata;
private final ProcessingEnvironment env;
private final Root root;
private final Map<String, Integer> simpleComponentNamesToDedupeSuffix = new HashMap<>();
private final Map<ComponentDescriptor, ClassName> componentNameMap = new HashMap<>();

private RootGenerator(RootMetadata metadata, ProcessingEnvironment env) {
this.metadata = metadata;
Expand Down Expand Up @@ -214,6 +219,34 @@ private ClassName getComponentsWrapperClassName() {
}

private ClassName getComponentClassName(ComponentDescriptor componentDescriptor) {
return ComponentNames.generatedComponent(root.classname(), componentDescriptor.component());
if (componentNameMap.containsKey(componentDescriptor)) {
return componentNameMap.get(componentDescriptor);
}

// Disallow any component names with the same name as our SingletonComponent because we treat
// that component specially and things may break.
checkState(
componentDescriptor.component().equals(ClassNames.SINGLETON_COMPONENT)
|| !componentDescriptor.component().simpleName().equals(
ClassNames.SINGLETON_COMPONENT.simpleName()),
"Cannot have a component with the same simple name as the reserved %s: %s",
ClassNames.SINGLETON_COMPONENT.simpleName(),
componentDescriptor.component());

ClassName generatedComponent = ComponentNames.generatedComponent(
root.classname(), componentDescriptor.component());

Integer suffix = simpleComponentNamesToDedupeSuffix.get(generatedComponent.simpleName());
if (suffix != null) {
// If an entry exists, use the suffix in the map and the replace it with the value incremented
generatedComponent = Processors.append(generatedComponent, String.valueOf(suffix));
simpleComponentNamesToDedupeSuffix.put(generatedComponent.simpleName(), suffix + 1);
} else {
// Otherwise, just add an entry for any possible future duplicates
simpleComponentNamesToDedupeSuffix.put(generatedComponent.simpleName(), 2);
}

componentNameMap.put(componentDescriptor, generatedComponent);
return generatedComponent;
}
}

0 comments on commit 785838e

Please sign in to comment.