Merge pull request #9152 from tamasvajk/kotlin-fix-parcelize-reflection-1

Kotlin: Fix extraction of reflective call generated by Parcelize
This commit is contained in:
Tamás Vajk 2022-05-20 09:06:21 +02:00 коммит произвёл GitHub
Родитель f918b2e763 ef08554adb
Коммит 3407b0f055
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 38 добавлений и 0 удалений

Просмотреть файл

@ -1352,6 +1352,23 @@ open class KotlinFileExtractor(
return fn
}
private fun findTopLevelPropertyOrWarn(propertyFilter: String, type: String, warnAgainstElement: IrElement): IrProperty? {
val prop = pluginContext.referenceProperties(FqName(propertyFilter))
.firstOrNull { it.owner.parentClassOrNull?.fqNameWhenAvailable?.asString() == type }
?.owner
if (prop != null) {
if (prop.parentClassOrNull != null) {
extractExternalClassLater(prop.parentAsClass)
}
} else {
logger.errorElement("Couldn't find JVM intrinsic property $propertyFilter in $type", warnAgainstElement)
}
return prop
}
val javaLangString by lazy {
val result = pluginContext.referenceClass(FqName("java.lang.String"))?.owner
result?.let { extractExternalClassLater(it) }
@ -1865,6 +1882,27 @@ open class KotlinFileExtractor(
}
}
}
isBuiltinCall(c, "<get-java>", "kotlin.jvm") -> {
// Special case for KClass<*>.java, which is used in the Parcelize plugin. In normal cases, this is already rewritten to the property referenced below:
findTopLevelPropertyOrWarn("kotlin.jvm.java", "kotlin.jvm.JvmClassMappingKt", c)?.let { javaProp ->
val getter = javaProp.getter
if (getter == null) {
logger.error("Couldn't find getter of `kotlin.jvm.JvmClassMappingKt::java`")
return
}
val ext = c.extensionReceiver
if (ext == null) {
logger.errorElement("No extension receiver found for `KClass::java` call", c)
return
}
val argType = (ext.type as? IrSimpleType)?.arguments?.firstOrNull()?.typeOrNull
val typeArguments = if (argType == null) listOf() else listOf(argType)
extractRawMethodAccess(getter, c, callable, parent, idx, enclosingStmt, listOf(), null, ext, typeArguments)
}
}
isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "iterator") && c.origin == IrStatementOrigin.FOR_LOOP_ITERATOR -> {
findTopLevelFunctionOrWarn("kotlin.jvm.internal.iterator", "kotlin.jvm.internal.ArrayIteratorKt", c)?.let { iteratorFn ->
extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf(c.dispatchReceiver), null, null, listOf((c.dispatchReceiver!!.type as IrSimpleType).arguments.first().typeOrNull!!))