【问题标题】:Kotlin KCallable illegalArgumentExceptionKotlin KCallable 非法ArgumentException
【发布时间】:2019-01-20 15:22:23
【问题描述】:

我有以下 Kotlin 函数:

fun invokeSync(typedArguments : List<Any?>): Any?{
    var returnedValue : Any?    
    try {
        returnedValue = callable.call(this, typedArguments);
    } catch (e:Exception) {
        logInvocationError(e, typedArguments);
        throw IllegalArgumentException(e);
    }
}

不管这个列表中有多少参数,我总是会收到一个IllegalArgumentException 说“Callable 需要 3 个参数,但提供了 1 个”。

该函数是一个简单的 isGreater 函数,带有 2 个 Int 类型的参数。 我检查了参数列表,其中有 2 个 Int 类型的参数。

这里是上下文中的函数:

open class TypedJavaScriptFunction(name: String) : SelfRegisteringJavascriptFunction(MessageFormat.format(JS_NAME_CONVENTION, name)) {

    val callable = getCallable(this::class)

    override fun function(arguments: Array<Any?>): Any? {
        try {
            val typedArguments = getTypedArguments(arguments)
            val annotations = callable.annotations

            for (a in annotations) {
                if (a is BrowserFunction) {
                    if (a.value == Policy.ASYNC) {
                        invokeAsync(typedArguments);
                        return null
                    } else {
                        return invokeSync(typedArguments)
                    }
                }
            }
        } catch (e: IllegalArgumentException) {
            // this Exception is only for signaling the error; it has already
            // been logged before
            JavaScriptAPI.showError(browser, "Internal Error (" + callable.name + ")");
        }
        return null
    }

    fun getTypedArguments(arguments: Array<Any?>): List<Any?> {
        var typedArguments = mutableListOf<Any?>()
        val argTypes = callable.valueParameters
        if (arguments.size != argTypes.size) {
            LOG.error(getName()
                    + ": given arguments don't match signature. Given: "
                    + arguments.size + ", expected: " + argTypes.size);
            throw IllegalArgumentException()
        }

        for (i in 0 until arguments.size) {
            typedArguments.add(TypeRefinery.refine(arguments[i], argTypes[i].type.classifier as KClass<Any>))
        }

        return typedArguments
    }

    // ...

    fun invokeSync(typedArguments: List<Any?>): Any? {
        var returnedValue: Any?
        try {
            returnedValue = callable.call(this, typedArguments);
        } catch (e: Exception) {
            logInvocationError(e, typedArguments);
            throw IllegalArgumentException(e);
        }

        // ...
    }
}

有没有人可以帮助我并告诉我出了什么问题或者可以给我一个提示?

【问题讨论】:

  • 您能说明一下在这种情况下您是如何定义callable 的吗?
  • .. 以及如何定义this(周边类)?
  • @Lucky Ozzy 请将其简化为重现您的问题的最小示例。这样更容易发现问题并为您提供帮助。

标签: kotlin illegalargumentexception


【解决方案1】:

由于call 采用vararg,因此您需要使用扩展运算符*toTypedArray() 像这样传递List

returnedValue = callable.call(this, *typedArguments.toTypedArray());

第一个参数是你调用函数的实例,另外两个参数来自展开的 List,条件是 List 正好有两个元素。

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 2010-10-17
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2018-02-17
    相关资源
    最近更新 更多