findMethod
按查询条件查找方法。
默认行为:先查当前类,找不到自动向上搜索父类。
val m = findMethod(clz) { name("doTask") }
val m = findMethod(clz) {
name("doTask")
paramCount(2)
params(String::class.java, Int::class.java)
}
// 强制只查当前类
val m = findMethod(clz, findSuper = false) { name("doTask") }Content copied to clipboard
Return
第一个匹配的 Method(已 setAccessible)
Parameters
clz
目标类
findSuper
null=智能搜索(默认), false=仅当前类, true=强制搜索继承链
query
查询条件块
Throws
fun findMethod(className: String, classLoader: ClassLoader = EzReflect.classLoader, findSuper: Boolean? = null, query: MethodQuery.() -> Unit): Method
按类名查找方法。
val m = findMethod("com.example.Target") { name("doTask") }Content copied to clipboard
fun String.findMethod(classLoader: ClassLoader = EzReflect.classLoader, findSuper: Boolean? = null, query: MethodQuery.() -> Unit): Method
从类名直接查找方法的便捷写法,等价于 loadClass(name).findMethod { ... }。 适合一次性查找场景;多次操作同一个类时推荐先显式 loadClass / loadClassFirst。
val m = "com.example.Target".findMethod { name("doTask") }Content copied to clipboard
从 Class 对象直接查找方法。
val m = clz.findMethod {
name("doTask")
filter { isPublic }
}Content copied to clipboard