Added instanceOrNull functions in HookParam

This commit is contained in:
2023-01-31 21:30:27 +08:00
parent cf2473b92c
commit 5bd1b3b79e
3 changed files with 98 additions and 4 deletions

View File

@@ -94,6 +94,28 @@ val instance: Any
:::
如果你不确定当前实例的对象是否为 `null`,你可以使用 `instanceOrNull`。
## instanceOrNull <span class="symbol">- field</span>
```kotlin:no-line-numbers
val instanceOrNull: Any?
```
**Change Records**
`v1.1.7` `added`
**Function Illustrate**
> 获取当前 Hook 实例的对象。
::: danger
如果你当前 Hook 的对象是一个静态,那么它将不存在实例的对象。
:::
## instanceClass <span class="symbol">- field</span>
```kotlin:no-line-numbers
@@ -313,6 +335,24 @@ inline fun <reified T> instance(): T
instance<Activity>().finish()
```
## instanceOrNull <span class="symbol">- method</span>
```kotlin:no-line-numbers
inline fun <reified T> instanceOrNull(): T?
```
**Function Illustrate**
`v1.1.7` `added`
**Function Illustrate**
> 获取当前 Hook 实例的对象 `T`。
**Function Example**
用法请参考 [instance](#instance-method) 方法。
## args <span class="symbol">- method</span>
```kotlin:no-line-numbers

View File

@@ -86,6 +86,28 @@ val instance: Any
:::
如果你不确定当前实例的对象是否为 `null`,你可以使用 `instanceOrNull`。
## instanceOrNull <span class="symbol">- field</span>
```kotlin:no-line-numbers
val instanceOrNull: Any?
```
**变更记录**
`v1.1.7` `新增`
**功能描述**
> 获取当前 Hook 实例的对象。
::: danger
如果你当前 Hook 的对象是一个静态,那么它将不存在实例的对象。
:::
## instanceClass <span class="symbol">- field</span>
```kotlin:no-line-numbers
@@ -305,6 +327,24 @@ inline fun <reified T> instance(): T
instance<Activity>().finish()
```
## instanceOrNull <span class="symbol">- method</span>
```kotlin:no-line-numbers
inline fun <reified T> instanceOrNull(): T?
```
**变更记录**
`v1.1.7` `新增`
**功能描述**
> 获取当前 Hook 实例的对象 `T`。
**功能示例**
用法请参考 [instance](#instance-method) 方法。
## args <span class="symbol">- method</span>
```kotlin:no-line-numbers

View File

@@ -87,16 +87,24 @@ class HookParam internal constructor(
*/
val args get() = param?.args ?: error("Current hooked Member args is null")
//val instanceOrNull?
/**
* 获取当前 Hook 实例的对象
*
* - ❗如果你当前 Hook 的对象是一个静态 - 那么它将不存在实例的对象
*
* - 如果你不确定当前实例的对象是否为 null - 你可以使用 [instanceOrNull]
* @return [Any]
* @throws IllegalStateException 如果对象为空
*/
val instance get() = param?.instance ?: error("HookParam instance got null! Is this a static member?")
/**
* 获取当前 Hook 实例的对象
*
* - ❗如果你当前 Hook 的对象是一个静态 - 那么它将不存在实例的对象
* @return [Any]
* @throws IllegalStateException 如果对象为空
* @return [Any] or null
*/
val instance get() = param?.instance ?: error("HookParam instance got null! Is this a static member?")
val instanceOrNull get() = param?.instance
/**
* 获取当前 Hook 实例的类对象
@@ -186,6 +194,12 @@ class HookParam internal constructor(
*/
inline fun <reified T> instance() = instance as? T? ?: error("HookParam instance cannot cast to ${classOf<T>().name}")
/**
* 获取当前 Hook 实例的对象 [T]
* @return [T] or null
*/
inline fun <reified T> instanceOrNull() = instanceOrNull as? T?
/**
* 获取当前 Hook 对象的 [method] or [constructor] 的参数数组下标实例化类
* @return [ArgsIndexCondition]