Skip to content

Commit 785838e

Browse files
Chang-EricDagger Team
authored andcommitted
Fix an issue where two @DefineComponent classes with the same simple name can create build conflicts by appending a unique suffix.
RELNOTES=Fix issue with @DefineComponent classes with same name PiperOrigin-RevId: 356363930
1 parent 53ceb91 commit 785838e

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

java/dagger/hilt/processor/internal/definecomponent/DefineComponentMetadatas.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,25 @@ private DefineComponentMetadata getUncached(
163163
? Optional.empty()
164164
: Optional.of(get(parent, childPath));
165165

166+
ClassName componentClassName = ClassName.get(component);
167+
166168
ProcessorErrors.checkState(
167169
parentComponent.isPresent()
168-
|| ClassName.get(component).equals(ClassNames.SINGLETON_COMPONENT),
170+
|| componentClassName.equals(ClassNames.SINGLETON_COMPONENT),
169171
component,
170172
"@DefineComponent %s is missing a parent declaration.\n"
171173
+ "Please declare the parent, for example: @DefineComponent(parent ="
172174
+ " SingletonComponent.class)",
173175
component);
174176

177+
ProcessorErrors.checkState(
178+
componentClassName.equals(ClassNames.SINGLETON_COMPONENT)
179+
|| !componentClassName.simpleName().equals(ClassNames.SINGLETON_COMPONENT.simpleName()),
180+
component,
181+
"Cannot have a component with the same simple name as the reserved %s: %s",
182+
ClassNames.SINGLETON_COMPONENT.simpleName(),
183+
componentClassName);
184+
175185
return new AutoValue_DefineComponentMetadatas_DefineComponentMetadata(
176186
component, scopes, parentComponent);
177187
}

java/dagger/hilt/processor/internal/root/RootGenerator.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package dagger.hilt.processor.internal.root;
1818

19+
import static com.google.common.base.Preconditions.checkState;
1920
import static dagger.hilt.processor.internal.Processors.toClassNames;
2021
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
2122
import static javax.lang.model.element.Modifier.ABSTRACT;
@@ -40,6 +41,8 @@
4041
import dagger.hilt.processor.internal.ComponentTree;
4142
import dagger.hilt.processor.internal.Processors;
4243
import java.io.IOException;
44+
import java.util.HashMap;
45+
import java.util.Map;
4346
import java.util.Optional;
4447
import javax.annotation.processing.ProcessingEnvironment;
4548
import javax.lang.model.element.Modifier;
@@ -58,6 +61,8 @@ static void generate(RootMetadata metadata, ProcessingEnvironment env) throws IO
5861
private final RootMetadata metadata;
5962
private final ProcessingEnvironment env;
6063
private final Root root;
64+
private final Map<String, Integer> simpleComponentNamesToDedupeSuffix = new HashMap<>();
65+
private final Map<ComponentDescriptor, ClassName> componentNameMap = new HashMap<>();
6166

6267
private RootGenerator(RootMetadata metadata, ProcessingEnvironment env) {
6368
this.metadata = metadata;
@@ -214,6 +219,34 @@ private ClassName getComponentsWrapperClassName() {
214219
}
215220

216221
private ClassName getComponentClassName(ComponentDescriptor componentDescriptor) {
217-
return ComponentNames.generatedComponent(root.classname(), componentDescriptor.component());
222+
if (componentNameMap.containsKey(componentDescriptor)) {
223+
return componentNameMap.get(componentDescriptor);
224+
}
225+
226+
// Disallow any component names with the same name as our SingletonComponent because we treat
227+
// that component specially and things may break.
228+
checkState(
229+
componentDescriptor.component().equals(ClassNames.SINGLETON_COMPONENT)
230+
|| !componentDescriptor.component().simpleName().equals(
231+
ClassNames.SINGLETON_COMPONENT.simpleName()),
232+
"Cannot have a component with the same simple name as the reserved %s: %s",
233+
ClassNames.SINGLETON_COMPONENT.simpleName(),
234+
componentDescriptor.component());
235+
236+
ClassName generatedComponent = ComponentNames.generatedComponent(
237+
root.classname(), componentDescriptor.component());
238+
239+
Integer suffix = simpleComponentNamesToDedupeSuffix.get(generatedComponent.simpleName());
240+
if (suffix != null) {
241+
// If an entry exists, use the suffix in the map and the replace it with the value incremented
242+
generatedComponent = Processors.append(generatedComponent, String.valueOf(suffix));
243+
simpleComponentNamesToDedupeSuffix.put(generatedComponent.simpleName(), suffix + 1);
244+
} else {
245+
// Otherwise, just add an entry for any possible future duplicates
246+
simpleComponentNamesToDedupeSuffix.put(generatedComponent.simpleName(), 2);
247+
}
248+
249+
componentNameMap.put(componentDescriptor, generatedComponent);
250+
return generatedComponent;
218251
}
219252
}

0 commit comments

Comments
 (0)