我正在尝试使用Kotlin反射调用一个函数,但出现错误:
java.lang.IllegalArgumentException:Callable期望有4个参数,但提供了3个。
这是进行反射调用的代码:
annotation.listeners.forEach { listener: KClass<*> ->
listener.functions.forEach { function: KFunction<*> ->
if (function.name == "before") {
function.call(annotation.action, request, response)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了类型listener,function只是为了使问题更易读。
这就是被调用的方法:
fun before(action: String, request: RestRequest, response: RestResponse)
Run Code Online (Sandbox Code Playgroud)
为了再次检查我的类型是否正确,我这样做:
if (function.name == "before") {
println(annotation.action::class)
println(request::class)
println(response::class)
}
Run Code Online (Sandbox Code Playgroud)
打印(此before功能所需的正确类型):
Run Code Online (Sandbox Code Playgroud)class kotlin.String class com.mycompany.RestRequest class com.mycompany.RestResponse
第四个参数应该是什么?
| 归档时间: |
|
| 查看次数: |
744 次 |
| 最近记录: |