Kotlin KCallable 非法参数异常
Kotlin KCallable illegalArgumentException
我有以下 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 expects 3 arguments, but 1 were provided"。
该函数是一个简单的 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);
}
// ...
}
}
有没有人可以帮助我并告诉我哪里出了问题或可以给我提示?
因为 call
takes a vararg
you need to use the spread operator *
and toTypedArray()
像这样传入 List
:
returnedValue = callable.call(this, *typedArguments.toTypedArray());
第一个参数是您调用函数的实例,另外两个参数来自展开的 List,条件是 List 恰好有两个元素。
我有以下 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 expects 3 arguments, but 1 were provided"。
该函数是一个简单的 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);
}
// ...
}
}
有没有人可以帮助我并告诉我哪里出了问题或可以给我提示?
因为 call
takes a vararg
you need to use the spread operator *
and toTypedArray()
像这样传入 List
:
returnedValue = callable.call(this, *typedArguments.toTypedArray());
第一个参数是您调用函数的实例,另外两个参数来自展开的 List,条件是 List 恰好有两个元素。