mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-04 09:45:19 +08:00
Added new function to call CurrentClass in ReflectionFactory
This commit is contained in:
@@ -389,10 +389,18 @@ inline fun Class<*>.constructor(initiate: ConstructorFinder.() -> Unit): Constru
|
||||
inline fun <reified T : Any> T.current(initiate: CurrentClass.() -> Unit): T
|
||||
```
|
||||
|
||||
```kotlin
|
||||
inline fun <reified T : Any> T.current(): CurrentClass
|
||||
```
|
||||
|
||||
**变更记录**
|
||||
|
||||
`v1.0.70` `新增`
|
||||
|
||||
`v1.0.93` `新增`
|
||||
|
||||
新增不使用 `current { ... }` 调用域直接使用 `current()` 得到实例的类操作对象
|
||||
|
||||
**功能描述**
|
||||
|
||||
> 获得当前实例的类操作对象。
|
||||
|
@@ -563,6 +563,53 @@ instance.current {
|
||||
}
|
||||
```
|
||||
|
||||
如果你不喜欢使用一个大括号的调用域来创建当前实例的命名空间,你可以直接使用 `current()` 方法。
|
||||
|
||||
> 示例如下
|
||||
|
||||
```kotlin
|
||||
// 假设这就是这个 Class 的实例,这个 Class 是不能被直接得到的
|
||||
val instance = Test()
|
||||
// 执行 doTask 方法
|
||||
instance
|
||||
.current()
|
||||
.method {
|
||||
name = "doTask"
|
||||
param(StringType)
|
||||
}.call("task_name")
|
||||
// 执行 stop 方法
|
||||
instance
|
||||
.current()
|
||||
.method {
|
||||
name = "stop"
|
||||
emptyParam()
|
||||
}.call()
|
||||
// 得到 name
|
||||
val name = instance.current().method { name = "getName" }.string()
|
||||
```
|
||||
|
||||
同样地,它们之间可以连续调用,但<u>**不允许内联调用**</u>。
|
||||
|
||||
> 示例如下
|
||||
|
||||
```kotlin
|
||||
// 假设这就是这个 Class 的实例
|
||||
val instance = Test()
|
||||
// 假设这个 Class 是不能被直接得到的
|
||||
instance.current {
|
||||
method {
|
||||
name = "doTask"
|
||||
param(StringType)
|
||||
}.call("task_name")
|
||||
}.current()
|
||||
.method {
|
||||
name = "stop"
|
||||
emptyParam()
|
||||
}.call()
|
||||
// ❗注意,因为 current() 返回的是 CurrentClass 自身对象,所以不能像下面这样调用
|
||||
instance.current().current()
|
||||
```
|
||||
|
||||
问题又来了,我想使用反射的方式创建如下的实例并调用其中的方法,该怎么做呢?
|
||||
|
||||
> 示例如下
|
||||
|
@@ -147,10 +147,20 @@ inline fun Class<*>.constructor(initiate: ConstructorFinder.() -> Unit = { empty
|
||||
* @return [T]
|
||||
*/
|
||||
inline fun <reified T : Any> T.current(initiate: CurrentClass.() -> Unit): T {
|
||||
if (javaClass.name == CurrentClass::class.java.name) error("Cannot create itself within CurrentClass itself")
|
||||
CurrentClass(javaClass, self = this).apply(initiate)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当前实例的类操作对象
|
||||
* @return [CurrentClass]
|
||||
*/
|
||||
inline fun <reified T : Any> T.current(): CurrentClass {
|
||||
if (javaClass.name == CurrentClass::class.java.name) error("Cannot create itself within CurrentClass itself")
|
||||
return CurrentClass(javaClass, self = this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过构造方法创建新实例 - 任意类型 [Any]
|
||||
* @param param 方法参数
|
||||
|
Reference in New Issue
Block a user