Added current function in FieldFinder.Instance

This commit is contained in:
2022-08-12 01:54:55 +08:00
parent dcc428270f
commit 223ad3cbb5
3 changed files with 71 additions and 1 deletions

View File

@@ -548,6 +548,24 @@ inner class Instance internal constructor(private val instance: Any?, private va
请直接使用 `any` 方法得到 `Field` 自身的实例化对象
##### current [method]
```kotlin
fun current(): CurrentClass?
```
```kotlin
inline fun current(initiate: CurrentClass.() -> Unit): Any?
```
**变更记录**
`v1.0.93` `新增`
**功能描述**
> 获得当前 `Field` 自身 `self` 实例的类操作对象。
##### cast [method]
```kotlin

View File

@@ -42,6 +42,8 @@ public class Test extends BaseTest {
private static TAG = "Test";
private BaseTest baseInstance;
private String a;
private boolean a;
@@ -612,6 +614,39 @@ instance.current {
instance.current().current()
```
针对 `Field` 实例,还有一个便捷的方法,可以直接获取 `Field` 所在实例的对象。
> 示例如下
```kotlin
// 假设这就是这个 Class 的实例
val instance = Test()
// 假设这个 Class 是不能被直接得到的
instance.current {
// <方案1>
field {
name = "baseInstance"
}.current {
method {
name = "doBaseTask"
param(StringType)
}.call("task_name")
}
// <方案2>
field {
name = "baseInstance"
}.current()
?.method {
name = "doBaseTask"
param(StringType)
}?.call("task_name")
}
```
上述 `current` 方法相当于帮你调用了 `CurrentClass` 中的 `field { ... }.any()?.current()` 方法。
!> 若不存在 `CurrentClass` 调用域,你需要使用 `field { ... }.get(instance).current()` 来进行调用。
问题又来了,我想使用反射的方式创建如下的实例并调用其中的方法,该怎么做呢?
> 示例如下

View File

@@ -30,6 +30,7 @@
package com.highcapable.yukihookapi.hook.core.finder
import com.highcapable.yukihookapi.annotation.YukiPrivateApi
import com.highcapable.yukihookapi.hook.bean.CurrentClass
import com.highcapable.yukihookapi.hook.bean.VariousClass
import com.highcapable.yukihookapi.hook.core.YukiMemberHookCreater
import com.highcapable.yukihookapi.hook.core.finder.base.BaseFinder
@@ -37,6 +38,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.ModifierRules
import com.highcapable.yukihookapi.hook.core.finder.type.NameConditions
import com.highcapable.yukihookapi.hook.core.reflex.tools.ReflectionTool
import com.highcapable.yukihookapi.hook.factory.FieldCondition
import com.highcapable.yukihookapi.hook.factory.current
import com.highcapable.yukihookapi.hook.factory.hasExtends
import com.highcapable.yukihookapi.hook.log.yLoggerW
import com.highcapable.yukihookapi.hook.utils.await
@@ -466,7 +468,22 @@ class FieldFinder @PublishedApi internal constructor(
* - 若要直接获取不确定的实例对象 - 请调用 [any] 方法
* @return [Any] or null
*/
private val self get() = field?.get(instance)
@PublishedApi
internal val self
get() = field?.get(instance)
/**
* 获得当前 [Field] 自身 [self] 实例的类操作对象
* @return [CurrentClass] or null
*/
fun current() = self?.current()
/**
* 获得当前 [Field] 自身 [self] 实例的类操作对象
* @param initiate 方法体
* @return [Any] or null
*/
inline fun current(initiate: CurrentClass.() -> Unit) = self?.current(initiate)
/**
* 得到当前 [Field] 实例