Repository: JetBrains/kotlin
File: core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt (Kotlin)

Code before:
                }
                parameter.isVararg -> {}
                else -> {
                    throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
                }
            }

            if (parameter.kind == KParameter.Kind.VALUE) {
                valueParameterIndex++
            }
        }

        if (!anyOptional) {
            return call(*arguments)
        }

        val caller = defaultCaller ?: throw KotlinReflectionInternalError("This callable does not support a default call: $descriptor")

        @Suppress("UNCHECKED_CAST")
        return reflectionCall {
            // +1 is argument for DefaultConstructorMarker or MethodHandle
            val mergedArgs = arrayOfNulls<Any?>(arguments.size + masks.size + 1)
            System.arraycopy(arguments, 0, mergedArgs, 0, arguments.size)
            for (i in masks.indices) {
                mergedArgs[i + arguments.size] = masks[i]
            }

            caller.call(mergedArgs) as R
        }
    }

    private fun callAnnotationConstructor(args: Map<KParameter, Any?>): R {
        val arguments = parameters.map { parameter ->
            when {
                args.containsKey(parameter) -> {
                    args[parameter] ?: throw IllegalArgumentException("Annotation argument value cannot be null ($parameter)")
                }
                parameter.isOptional -> null
                parameter.isVararg -> defaultEmptyArray(parameter.type)
                else -> throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
            }
        }

        val caller = defaultCaller ?: throw KotlinReflectionInternalError("This callable does not support a default call: $descriptor")

        @Suppress("UNCHECKED_CAST")
        return reflectionCall {
            caller.call(arguments.toTypedArray()) as R
        }
    }


Review comment (suggestion):
Since it is not possible to copy from an `IntArray` to an `Array<Any?>` using `System.arraycopy`, a `for` loop is used.

Code after:

        var valueParameterIndex = 0
        var anyOptional = false

        for (parameter in parameters) {
            when {
                args.containsKey(parameter) -> {
                    arguments[parameter.index] = args[parameter]
                }
                parameter.isOptional -> {
                    val maskIndex = parameterSize + (valueParameterIndex / Integer.SIZE)
                    arguments[maskIndex] = (arguments[maskIndex] as Int) or (1 shl (valueParameterIndex % Integer.SIZE))
                    anyOptional = true
                }
                parameter.isVararg -> {}
                else -> {
                    throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
                }
            }

            if (parameter.kind == KParameter.Kind.VALUE) {
                valueParameterIndex++
            }
        }

        if (!anyOptional) {
            @Suppress("UNCHECKED_CAST")
            return reflectionCall {
                caller.call(arguments.copyOf(parameterSize)) as R
            }
        }

        val caller = defaultCaller ?: throw KotlinReflectionInternalError("This callable does not support a default call: $descriptor")

        @Suppress("UNCHECKED_CAST")
        return reflectionCall {
            caller.call(arguments) as R
        }
    }

    private fun callAnnotationConstructor(args: Map<KParameter, Any?>): R {
        val arguments = parameters.map { parameter ->
            when {
                args.containsKey(parameter) -> {
                    args[parameter] ?: throw IllegalArgumentException("Annotation argument value cannot be null ($parameter)")
                }
                parameter.isOptional -> null
                parameter.isVararg -> defaultEmptyArray(parameter.type)
                else -> throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
            }
        }