【发布时间】: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