Skip to content

Commit d3c3f3b

Browse files
committed
Kotlin: Fix class lookup for nested Android synthetic classes
1 parent 9780dff commit d3c3f3b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,30 @@ open class KotlinUsesExtractor(
308308
c.hasEqualFqName(FqName("java.lang.Object")))
309309
return c
310310
return globalExtensionState.syntheticToRealClassMap.getOrPut(c) {
311-
val result = c.fqNameWhenAvailable?.let {
312-
pluginContext.referenceClass(it)?.owner
311+
val qualifiedName = c.fqNameWhenAvailable
312+
if (qualifiedName == null) {
313+
logger.warn("Failed to replace synthetic class ${c.name} because it has no fully qualified name")
314+
return@getOrPut null
313315
}
314-
if (result == null) {
315-
logger.warn("Failed to replace synthetic class ${c.name}")
316-
} else {
316+
317+
val result = pluginContext.referenceClass(qualifiedName)?.owner
318+
if (result != null) {
317319
logger.info("Replaced synthetic class ${c.name} with its real equivalent")
320+
return@getOrPut result
318321
}
319-
result
322+
323+
// The above doesn't work for (some) generated nested classes, such as R$id, which should be R.id
324+
val fqn = qualifiedName.asString()
325+
if (fqn.indexOf('$') >= 0) {
326+
val nested = pluginContext.referenceClass(FqName(fqn.replace('$', '.')))?.owner
327+
if (nested != null) {
328+
logger.info("Replaced synthetic nested class ${c.name} with its real equivalent")
329+
return@getOrPut nested
330+
}
331+
}
332+
333+
logger.warn("Failed to replace synthetic class ${c.name}")
334+
return@getOrPut null
320335
} ?: c
321336
}
322337

0 commit comments

Comments
 (0)