调用函数将引发IllegalArgumentException预期为0,传递空args时得到1

时间:2019-05-15 09:24:25

标签: kotlin reflection

我尝试使用反射来调用android.app.ActivityThread.currentActivityThread() 功能

val currentActivityThread = invoke_static_method("android.app.ActivityThread",
                "currentActivityThread",
                arrayOf<Class<*>>(),
                arrayOf<Any>())

fun invoke_static_method(className: String,
                         methodName: String,
                         pareTypes: Array<Class<*>>,
                         pareValues: Array<Any>): Any?{
    try {
        val objClass: Class<*> = Class.forName(className)
        val method = objClass.getMethod(methodName,*pareTypes)
        Log.d("dump",method.toString()+pareValues.size)
        return method.invoke(null,pareValues)
    }catch (e: Exception){
        Log.d("dump",e.toString())
        e.printStackTrace()
    }
    return null
}

从日志中,我很清楚这种方法

D/dump: public static android.app.ActivityThread android.app.ActivityThread.currentActivityThread()0

D/dump: java.lang.IllegalArgumentException: Wrong number of arguments; expected 0, got 1

由于pareValues已正确初始化,我不知道为什么调用会抛出IllegalArgumentException

感谢您的帮助

0 个答案:

没有答案