Skip to content

Commit 3407b0f

Browse files
authored
Merge pull request #9152 from tamasvajk/kotlin-fix-parcelize-reflection-1
Kotlin: Fix extraction of reflective call generated by Parcelize
2 parents f918b2e + ef08554 commit 3407b0f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,23 @@ open class KotlinFileExtractor(
13521352
return fn
13531353
}
13541354

1355+
private fun findTopLevelPropertyOrWarn(propertyFilter: String, type: String, warnAgainstElement: IrElement): IrProperty? {
1356+
1357+
val prop = pluginContext.referenceProperties(FqName(propertyFilter))
1358+
.firstOrNull { it.owner.parentClassOrNull?.fqNameWhenAvailable?.asString() == type }
1359+
?.owner
1360+
1361+
if (prop != null) {
1362+
if (prop.parentClassOrNull != null) {
1363+
extractExternalClassLater(prop.parentAsClass)
1364+
}
1365+
} else {
1366+
logger.errorElement("Couldn't find JVM intrinsic property $propertyFilter in $type", warnAgainstElement)
1367+
}
1368+
1369+
return prop
1370+
}
1371+
13551372
val javaLangString by lazy {
13561373
val result = pluginContext.referenceClass(FqName("java.lang.String"))?.owner
13571374
result?.let { extractExternalClassLater(it) }
@@ -1865,6 +1882,27 @@ open class KotlinFileExtractor(
18651882
}
18661883
}
18671884
}
1885+
isBuiltinCall(c, "<get-java>", "kotlin.jvm") -> {
1886+
// Special case for KClass<*>.java, which is used in the Parcelize plugin. In normal cases, this is already rewritten to the property referenced below:
1887+
findTopLevelPropertyOrWarn("kotlin.jvm.java", "kotlin.jvm.JvmClassMappingKt", c)?.let { javaProp ->
1888+
val getter = javaProp.getter
1889+
if (getter == null) {
1890+
logger.error("Couldn't find getter of `kotlin.jvm.JvmClassMappingKt::java`")
1891+
return
1892+
}
1893+
1894+
val ext = c.extensionReceiver
1895+
if (ext == null) {
1896+
logger.errorElement("No extension receiver found for `KClass::java` call", c)
1897+
return
1898+
}
1899+
1900+
val argType = (ext.type as? IrSimpleType)?.arguments?.firstOrNull()?.typeOrNull
1901+
val typeArguments = if (argType == null) listOf() else listOf(argType)
1902+
1903+
extractRawMethodAccess(getter, c, callable, parent, idx, enclosingStmt, listOf(), null, ext, typeArguments)
1904+
}
1905+
}
18681906
isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "iterator") && c.origin == IrStatementOrigin.FOR_LOOP_ITERATOR -> {
18691907
findTopLevelFunctionOrWarn("kotlin.jvm.internal.iterator", "kotlin.jvm.internal.ArrayIteratorKt", c)?.let { iteratorFn ->
18701908
extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf(c.dispatchReceiver), null, null, listOf((c.dispatchReceiver!!.type as IrSimpleType).arguments.first().typeOrNull!!))

0 commit comments

Comments
 (0)