From c4100247f85549fec00e3021fd382fa5fc521207 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Fri, 6 Oct 2023 02:20:59 +0800 Subject: [PATCH] docs: update example, reflection --- .../src/en/api/special-features/reflection.md | 48 +++++++++++++++++++ docs-source/src/en/guide/example.md | 36 +++++++++++++- .../zh-cn/api/special-features/reflection.md | 46 ++++++++++++++++++ docs-source/src/zh-cn/guide/example.md | 36 +++++++++++++- 4 files changed, 162 insertions(+), 4 deletions(-) diff --git a/docs-source/src/en/api/special-features/reflection.md b/docs-source/src/en/api/special-features/reflection.md index 2ef9105d..2b7c44cc 100644 --- a/docs-source/src/en/api/special-features/reflection.md +++ b/docs-source/src/en/api/special-features/reflection.md @@ -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. diff --git a/docs-source/src/en/guide/example.md b/docs-source/src/en/guide/example.md index dbb761a3..f6fee358 100644 --- a/docs-source/src/en/guide/example.md +++ b/docs-source/src/en/guide/example.md @@ -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. diff --git a/docs-source/src/zh-cn/api/special-features/reflection.md b/docs-source/src/zh-cn/api/special-features/reflection.md index bd89a5d4..505ee5ad 100644 --- a/docs-source/src/zh-cn/api/special-features/reflection.md +++ b/docs-source/src/zh-cn/api/special-features/reflection.md @@ -74,6 +74,52 @@ var instance = classOf(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` 通过异常来判断是否存在。 diff --git a/docs-source/src/zh-cn/guide/example.md b/docs-source/src/zh-cn/guide/example.md index e5adc68e..50606fb9 100644 --- a/docs-source/src/zh-cn/guide/example.md +++ b/docs-source/src/zh-cn/guide/example.md @@ -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 模块的激活信息以更好地向用户展示当前功能的生效状态。