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 a01aa745..380b9d73 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 @@ -136,6 +136,36 @@ fun Class<*>.method(initiate: MethodFinder.() -> Unit) = MethodFinder(classSet = */ fun Class<*>.constructor(initiate: ConstructorFinder.() -> Unit) = ConstructorFinder(classSet = this).apply(initiate).build() +/** + * 调用当前实例中的变量 + * @param initiate 查找方法体 + * @return [FieldFinder.Result.Instance] + */ +fun Any.field(initiate: FieldFinder.() -> Unit) = javaClass.field(initiate).get(this) + +/** + * 调用当前实例中的方法 + * @param initiate 查找方法体 + * @return [MethodFinder.Result.Instance] + */ +fun Any.method(initiate: MethodFinder.() -> Unit) = javaClass.method(initiate).get(this) + +/** + * 通过构造方法创建新实例 - 指定类型 [T] + * @param param 方法参数 + * @param initiate 查找方法体 + * @return [T] or null + */ +fun Class<*>.construct(vararg param: Any?, initiate: ConstructorFinder.() -> Unit = {}) = constructor(initiate).get().newInstance(*param) + +/** + * 通过构造方法创建新实例 - 任意类型 [Any] + * @param param 方法参数 + * @param initiate 查找方法体 + * @return [Any] or null + */ +fun Class<*>.constructAny(vararg param: Any?, initiate: ConstructorFinder.() -> Unit = {}) = construct(*param, initiate) + /** * 遍历当前类中的所有方法 * @param callback 回调 - ([Int] 下标,[Method] 实例) diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/ReflectionTool.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/ReflectionTool.kt index e6b7c572..85344722 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/ReflectionTool.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/ReflectionTool.kt @@ -306,9 +306,9 @@ internal object ReflectionTool { ): Constructor<*> { paramTypes?.takeIf { it.isNotEmpty() } ?.forEachIndexed { p, it -> if (it == UndefinedType) error("Constructor match paramType[$p] class is not found") } - if (orderIndex == null && matchIndex == null && paramCount < 0 && paramTypes == null && modifiers == null) - error("You must set a condition when finding a Constructor") - val hashCode = ("[$orderIndex][$matchIndex][$paramCount][${paramTypes.typeOfString()}][$modifiers][$classSet]").hashCode() + val paramCountR = + if (orderIndex == null && matchIndex == null && paramCount < 0 && paramTypes == null && modifiers == null) 0 else paramCount + val hashCode = ("[$orderIndex][$matchIndex][$paramCountR][${paramTypes.typeOfString()}][$modifiers][$classSet]").hashCode() return MemberCacheStore.findConstructor(hashCode) ?: let { var constructor: Constructor<*>? = null classSet?.declaredConstructors?.apply { @@ -316,7 +316,7 @@ internal object ReflectionTool { var paramCountIndex = -1 var modifyIndex = -1 val paramCountLastIndex = - if (paramCount >= 0 && matchIndex != null) filter { paramCount == it.parameterTypes.size }.lastIndex else -1 + if (paramCountR >= 0 && matchIndex != null) filter { paramCountR == it.parameterTypes.size }.lastIndex else -1 val paramTypeLastIndex = if (paramTypes != null && matchIndex != null) filter { arrayContentsEq(paramTypes, it.parameterTypes) }.lastIndex else -1 val modifyLastIndex = if (modifiers != null && matchIndex != null) filter { modifiers.contains(it) }.lastIndex else -1 @@ -324,7 +324,7 @@ internal object ReflectionTool { var isMatched = false var conditions = true if (paramCount >= 0) - conditions = (it.parameterTypes.size == paramCount).let { + conditions = (it.parameterTypes.size == paramCountR).let { if (it) paramCountIndex++ isMatched = true it && (matchIndex == null || @@ -381,7 +381,7 @@ internal object ReflectionTool { } }] " + "paramCount:[${ - paramCount.takeIf { it >= 0 || it == -2 } + paramCountR.takeIf { it >= 0 || it == -2 } ?.toString()?.replace(oldValue = "-2", newValue = "last") ?: "unspecified" }] " + "paramTypes:[${paramTypes.typeOfString()}] " +