docs: update example, reflection

This commit is contained in:
2023-10-06 02:20:59 +08:00
parent c801f2aeca
commit c4100247f8
4 changed files with 162 additions and 4 deletions

View File

@@ -78,6 +78,54 @@ For more functions, please refer to [classOf](../public/com/highcapable/yukihook
:::
### Lazy Loading
Suppose we want to get a `Class` that cannot be called directly, but we do not need this `Class` immediately.
At this time, you can use `lazyClass` to complete this function.
> The following example
```kotlin
// Lazy loading of this Class
// If you are currently in a PackageParam environment, then you do not need to consider ClassLoader
val instance by lazyClass("com.demo.Test")
// Customize the ClassLoader where the Class is located
val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader
val instance by lazyClass("com.demo.Test") { customClassLoader }
// Call this Class at the appropriate time
instance.method {
// ...
}
```
If the current `Class` does not exist, using the above method will throw an exception.
If you are not sure whether `Class` exists, you can refer to the following solution.
> The following example
```kotlin
// Lazy loading of this Class
// If you are currently in a PackageParam environment, then you do not need to consider ClassLoader
// If not available, the result will be null but no exception will be thrown
val instance by lazyClassOrNull("com.demo.Test")
// Customize the ClassLoader where the Class is located
val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader
// If not available, the result will be null but no exception will be thrown
val instance by lazyClassOrNull("com.demo.Test") { customClassLoader }
// Call this Class at the appropriate time
instance?.method {
// ...
}
```
::: tip
For more functions, please refer to [lazyClass](../public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory#lazyclass-method), [lazyClassOrNull](../public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory#lazyclassornull-method), [PackageParam → lazyClass](../public/com/highcapable/yukihookapi/hook/param/PackageParam#lazyclass-method), [PackageParam → lazyClassOrNull](../public/com/highcapable/yukihookapi/hook/param/PackageParam#lazyclassornull-method) methods.
:::
### Existential Judgment
Suppose we want to determine whether a `Class` exists.

View File

@@ -157,7 +157,7 @@ For example, I want to get `com.example.demo.TestClass`.
.method {
// Your code here.
}.hook {
// Your code here.
// Your code here.
}
```
@@ -170,10 +170,30 @@ If `com.example.demo` is the app you want to hook, then the writing method can b
.method {
// Your code here.
}.hook {
// Your code here.
// Your code here.
}
```
If this `Class` is not immediately available, you can use `lazyClass(...)` to define it.
> The following example
Define `TestClass`.
```kotlin
val TestClass by lazyClass("com.example.demo.TestClass")
```
Use it when appropriate.
```kotlin
TestClass.method {
// Your code here.
}.hook {
// Your code here.
}
```
::: tip
For more functions, please refer to [MemberHookCreator](../api/public/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator#memberhookcreator-class).
@@ -568,6 +588,18 @@ override fun onHook() = encase {
}
```
You can also abbreviate the `hook { ... }` method body when you only need a Hook callback event.
> The following example
```kotlin
ActivityClass.method {
// Your code here.
}.hook().after {
// Your code here.
}
```
## Xposed Module Status
Usually, the developer of the Xposed Module will choose to read the activation information of the current Xposed Module to better show the user the effective status of the current function.

View File

@@ -74,6 +74,52 @@ var instance = classOf<Test>(customClassLoader)
:::
### 延迟装载
假设我们要得到一个不能直接调用的 `Class`,但是我们也不是立刻就需要这个 `Class`
这个时候,你可以使用 `lazyClass` 来完成这个功能。
> 示例如下
```kotlin
// 延迟装载这个 Class
// 如果当前正处于 PackageParam 环境,那么你可以不需要考虑 ClassLoader
val instance by lazyClass("com.demo.Test")
// 自定义 Class 所在的 ClassLoader
val customClassLoader: ClassLoader? = ... // 假设这个就是你的 ClassLoader
val instance by lazyClass("com.demo.Test") { customClassLoader }
// 在适当的时候调用这个 Class
instance.method {
// ...
}
```
如果当前 `Class` 并不存在,使用上述方法会抛出异常,如果你不确定 `Class` 是否存在,可以参考下面的解决方案。
> 示例如下
```kotlin
// 延迟装载这个 Class
// 如果当前正处于 PackageParam 环境,那么你可以不需要考虑 ClassLoader
// 得不到时结果会为 null 但不会抛出异常
val instance by lazyClassOrNull("com.demo.Test")
// 自定义 Class 所在的 ClassLoader
val customClassLoader: ClassLoader? = ... // 假设这个就是你的 ClassLoader
// 得不到时结果会为 null 但不会抛出异常
val instance by lazyClassOrNull("com.demo.Test") { customClassLoader }
// 在适当的时候调用这个 Class
instance?.method {
// ...
}
```
::: tip
更多功能请参考 [lazyClass](../public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory#lazyclass-method)、[lazyClassOrNull](../public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory#lazyclassornull-method)、[PackageParam → lazyClass](../public/com/highcapable/yukihookapi/hook/param/PackageParam#lazyclass-method)、[PackageParam → lazyClassOrNull](../public/com/highcapable/yukihookapi/hook/param/PackageParam#lazyclassornull-method) 方法。
:::
### 存在判断
假设我们要判断一个 `Class` 是否存在,通常情况下,我们可以使用标准的反射 API 去查找这个 `Class` 通过异常来判断是否存在。

View File

@@ -157,7 +157,7 @@ loadApp(name = "com.android.browser") {
.method {
// Your code here.
}.hook {
// Your code here.
// Your code here.
}
```
@@ -170,10 +170,30 @@ loadApp(name = "com.android.browser") {
.method {
// Your code here.
}.hook {
// Your code here.
// Your code here.
}
```
若这个 `Class` 不是马上就能被得到的,你可以使用 `lazyClass(...)` 来定义它。
> 示例如下
定义 `TestClass`。
```kotlin
val TestClass by lazyClass("com.example.demo.TestClass")
```
在适当的时候使用它。
```kotlin
TestClass.method {
// Your code here.
}.hook {
// Your code here.
}
```
::: tip
更多功能请参考 [MemberHookCreator](../api/public/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator#memberhookcreator-class)。
@@ -568,6 +588,18 @@ override fun onHook() = encase {
}
```
你还可以在仅需要一个 Hook 回调事件的时候简写 `hook { ... }` 方法体。
> 示例如下
```kotlin
ActvityClass.method {
// Your code here.
}.hook().after {
// Your code here.
}
```
## Xposed 模块状态
通常情况下Xposed 模块的开发者都会去选择读取当前 Xposed 模块的激活信息以更好地向用户展示当前功能的生效状态。