From 6f98e769cf49dcb9af64d9974c259d9b52679e62 Mon Sep 17 00:00:00 2001 From: Fankesyooni Date: Fri, 11 Feb 2022 22:52:50 +0800 Subject: [PATCH] ... --- .../hook/factory/ReflectionFactory.kt | 69 +++++++++---------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt index e3e95194..94e2f1f2 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt @@ -68,36 +68,38 @@ val HookClass.normalClass get() = instance /** * 查找方法是否存在 * @param name 名称 - * @param clazz params + * @param paramType params * @return [Boolean] 是否存在 */ -fun Class<*>.hasMethod(name: String, vararg clazz: Class<*>): Boolean = +fun Class<*>.hasMethod(name: String, vararg paramType: Class<*>): Boolean = try { - getDeclaredMethod(name, *clazz) + getDeclaredMethod(name, *paramType) true } catch (_: Throwable) { false } /** - * 查找静态 [Field] 的实例 + * 查找并得到静态 [Field] 的实例 * @param name 名称 - * @return [Any] 实例对象 + * @return [T] 实例对象 or null * @throws NoSuchFieldError */ -fun Class<*>.findStaticField(name: String): Any? = getDeclaredField(name).apply { isAccessible = true }[null] +inline fun Class<*>.obtainStaticFieldAny(name: String): T? = + getDeclaredField(name).apply { isAccessible = true }[null] as? T? /** - * 查找 [Field] 的实例 + * 查找并得到 [Field] 的实例 * @param any 对象 * @param name 名称 - * @return [Any] 实例对象 + * @return [T] 实例对象 or null * @throws NoSuchFieldError */ -fun Class<*>.findField(any: Any?, name: String): Any? = getDeclaredField(name).apply { isAccessible = true }[any] +inline fun Class<*>.obtainFieldAny(any: Any?, name: String): T? = + getDeclaredField(name).apply { isAccessible = true }[any] as? T? /** - * 修改静态 [Field] 实例内容 + * 修改静态 [Field] 的实例内容 * @param name 名称 * @param value 值 * @throws NoSuchFieldError @@ -110,7 +112,7 @@ fun Class<*>.modifyStaticField(name: String, value: Any?) { } /** - * 修改 [Field] 实例内容 + * 修改 [Field] 的实例内容 * @param any 对象 * @param name 名称 * @param value 值 @@ -124,48 +126,39 @@ fun Class<*>.modifyField(any: Any?, name: String, value: Any?) { } /** - * 查找目标变量 - * @param name 方法名 - * @return [Field] - * @throws NoSuchFieldError 如果找不到变量会报错 - */ -fun Class<*>.findField(name: String): Field = - getDeclaredField(name).apply { isAccessible = true } - -/** - * 得到方法 + * 查找并得到方法 * @param name 方法名称 - * @param clazz params - * @return [Method] + * @param paramType params + * @return [Method] or null * @throws NoSuchMethodError */ -fun Class<*>.findMethod(name: String, vararg clazz: Class<*>): Method? = - getDeclaredMethod(name, *clazz).apply { isAccessible = true } +fun Class<*>.obtainMethod(name: String, vararg paramType: Class<*>): Method? = + getDeclaredMethod(name, *paramType).apply { isAccessible = true } /** - * 得到构造类 - * @param parameterTypes params - * @return [Constructor] + * 查找并得到构造类 + * @param paramType params + * @return [Constructor] or null * @throws NoSuchMethodError */ -fun Class<*>.findConstructor(vararg parameterTypes: Class<*>?): Constructor? = - getDeclaredConstructor(*parameterTypes).apply { isAccessible = true } +fun Class<*>.obtainConstructor(vararg paramType: Class<*>): Constructor? = + getDeclaredConstructor(*paramType).apply { isAccessible = true } /** - * 执行方法 - 静态 - * @param anys 方法参数 + * 执行静态方法 + * @param param 方法参数 * @return [T] * @throws IllegalStateException 如果 [T] 类型错误 */ -inline fun Method.invokeStatic(vararg anys: Any) = - invoke(null, anys) as? T? ?: error("Method ReturnType cannot cast to ${T::class.java}") +inline fun Method.invokeStatic(vararg param: Any?) = + invoke(null, param) as? T? ?: error("Method ReturnType cannot cast to ${T::class.java}") /** - * 执行方法 - 非静态 + * 执行方法 * @param any 目标对象 - * @param anys 方法参数 + * @param param 方法参数 * @return [T] * @throws IllegalStateException 如果 [T] 类型错误 */ -inline fun Method.invokeAny(any: Any?, vararg anys: Any) = - invoke(any, anys) as? T? ?: error("Method ReturnType cannot cast to ${T::class.java}") +inline fun Method.invokeAny(any: Any?, vararg param: Any?) = + invoke(any, param) as? T? ?: error("Method ReturnType cannot cast to ${T::class.java}")