Support interpretation of toArray for collections

This commit is contained in:
Ivan Kylchik
2021-06-10 20:20:27 +03:00
committed by TeamCityServer
parent b10fdb919f
commit 78475a5d9c
2 changed files with 12 additions and 1 deletions
@@ -80,7 +80,7 @@ internal class CommonProxy private constructor(override val state: Common, overr
method.name == "toString" && method.parameterTypes.isEmpty() -> commonProxy.toString()
else -> {
val irFunction = commonProxy.state.getIrFunction(method)
?: throw AssertionError("Cannot find method $method in ${commonProxy.state}")
?: return@newProxyInstance commonProxy.fallbackIfMethodNotFound(method)
val valueArguments = mutableListOf<Variable>()
valueArguments += Variable(irFunction.getDispatchReceiver()!!, commonProxy.state)
valueArguments += irFunction.valueParameters
@@ -90,5 +90,15 @@ internal class CommonProxy private constructor(override val state: Common, overr
}
}
}
private fun CommonProxy.fallbackIfMethodNotFound(method: java.lang.reflect.Method): Any {
return when {
method.name == "toArray" && method.parameterTypes.isEmpty() -> {
val wrapper = this.state.superWrapperClass
if (wrapper == null) arrayOf() else (wrapper as Collection<*>).toTypedArray()
}
else -> throw AssertionError("Cannot find method $method in ${this.state}")
}
}
}
}
@@ -24,6 +24,7 @@ fun test(list: MyArrayList<Int>): String {
val otherList = arrayListOf(4, 5, 6)
list.addAll(otherList)
list.addAll(emptyList<Int>())
return "Counter " + list.addCounter + "; size " + list.size
}