Skip to content

Commit c35a799

Browse files
Chang-EricDagger Team
authored andcommitted
Another fix for missing rootComponentNames from older version of Dagger.
Fixes #4898. RELNOTES=n/a PiperOrigin-RevId: 802766550
1 parent 6258b89 commit c35a799

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

hilt-compiler/main/java/dagger/hilt/processor/internal/root/AggregatedRootMetadata.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,24 @@ private static AggregatedRootMetadata create(XTypeElement element, XProcessingEn
9797
rootElement,
9898
env.requireTypeElement(annotation.getAsString("originatingRoot")),
9999
annotation.getAsType("rootAnnotation").getTypeElement(),
100-
parseClassName(
100+
parseRootComponentClassName(
101101
annotation.getAsString("rootComponentPackage"),
102102
annotation.getAsStringList("rootComponentSimpleNames")),
103103
allowSharingComponent);
104104
}
105105

106-
private static ClassName parseClassName(String pkg, List<String> simpleNames) {
106+
private static ClassName parseRootComponentClassName(
107+
String rootComponentPackage, List<String> rootComponentSimpleNames) {
108+
// If rootComponentPackage isn't there, that means that this is likely coming from an older
109+
// Dagger version, so assume the root component is the SingletonComponent for backwards
110+
// compatibility.
111+
if (rootComponentPackage.isEmpty()) {
112+
return ClassNames.SINGLETON_COMPONENT;
113+
}
107114
return ClassName.get(
108-
pkg, simpleNames.get(0), simpleNames.subList(1, simpleNames.size()).toArray(new String[0]));
115+
rootComponentPackage,
116+
rootComponentSimpleNames.get(0),
117+
rootComponentSimpleNames.subList(1, rootComponentSimpleNames.size())
118+
.toArray(new String[0]));
109119
}
110120
}

java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin/root/Aggregator.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,14 @@ private constructor(private val logger: Logger, private val asmApiVersion: Int)
167167
originatingRootClass,
168168
)
169169
val rootComponentName =
170-
// TODO(erichang): This is a bit of a hedge since the rootComponentPackage attribute
171-
// has a default value. So either the visitor visits it even if unset because it has
172-
// a default and it is always initialized and we only need the empty check or it
173-
// doesn't visit it despite the default value and we only need the initialized
174-
// check. The ASM documentation wasn't very clear though and since this cross
175-
// version setup is a bit difficult to test, just checking both for now to get this
176-
// fixed quickly.
177-
if (::rootComponentPackage.isInitialized && !rootComponentPackage.isEmpty()) {
170+
// If rootComponentPackage isn't there, that means that this is likely coming from
171+
// an older Dagger version, so assume the root component is the SingletonComponent
172+
// for backwards compatibility.
173+
// Also, even though these have default values, if they are unset then the visitor
174+
// still does not visit them so we only have to check if it is initialized.
175+
if (::rootComponentPackage.isInitialized) {
178176
parseClassName(rootComponentPackage, rootComponentSimpleNames)
179177
} else {
180-
// If rootComponentPackage isn't there, that means that this is likely coming from
181-
// an older Dagger version, so assume the root component is the SingletonComponent
182-
// for backwards compatibility.
183178
SINGLETON_COMPONENT
184179
}
185180

0 commit comments

Comments
 (0)