From 9b7a767e7145db725ce6f11145732d3a3397a3e9 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Tue, 20 Sep 2022 11:58:00 +0800 Subject: [PATCH] Added GenericClass and generic function in CurrentClass and ReflectionFactory --- .../yukihookapi/hook/bean/CurrentClass.md | 64 +++++++++++++++++++ .../yukihookapi/hook/bean/GenericClass.md | 31 +++++++++ .../hook/factory/ReflectionFactory.md | 32 ++++++++++ .../yukihookapi/hook/bean/CurrentClass.kt | 35 ++++++++++ .../yukihookapi/hook/bean/GenericClass.kt | 44 +++++++++++++ .../hook/factory/ReflectionFactory.kt | 26 ++++++-- 6 files changed, 228 insertions(+), 4 deletions(-) create mode 100644 docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/GenericClass.md create mode 100644 yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/GenericClass.kt diff --git a/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/CurrentClass.md b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/CurrentClass.md index d744cba5..42baa9ce 100644 --- a/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/CurrentClass.md +++ b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/CurrentClass.md @@ -48,6 +48,38 @@ val simpleName: String > 获得当前 `classSet` 的 `Class.getSimpleName`。 +## generic - method + +```kotlin:no-line-numbers +fun generic(): GenericClass? +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 获得当前实例中的泛型父类。 + +如果当前实例不存在泛型将返回 `null`。 + +## generic - method + +```kotlin:no-line-numbers +inline fun generic(initiate: GenericClass.() -> Unit): GenericClass? +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 获得当前实例中的泛型父类。 + +如果当前实例不存在泛型将返回 `null`。 + ## superClass - method ```kotlin:no-line-numbers @@ -136,6 +168,38 @@ val simpleName: String > 获得当前 `classSet` 中父类的 `Class.getSimpleName`。 +### generic - method + +```kotlin:no-line-numbers +fun generic(): GenericClass? +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 获得当前实例父类中的泛型父类。 + +如果当前实例不存在泛型将返回 `null`。 + +### generic - method + +```kotlin:no-line-numbers +inline fun generic(initiate: GenericClass.() -> Unit): GenericClass? +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 获得当前实例父类中的泛型父类。 + +如果当前实例不存在泛型将返回 `null`。 + ### field - method ```kotlin:no-line-numbers diff --git a/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/GenericClass.md b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/GenericClass.md new file mode 100644 index 00000000..975dbfe2 --- /dev/null +++ b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/bean/GenericClass.md @@ -0,0 +1,31 @@ +--- +pageClass: code-page +--- + +# GenericClass - class + +```kotlin:no-line-numbers +class GenericClass internal constructor(private val type: ParameterizedType) +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 当前 `Class` 的泛型父类操作对象。 + +## argument - method + +```kotlin:no-line-numbers +fun argument(index: Int): Class<*> +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 获得泛型参数数组下标的 `Class` 实例。 \ No newline at end of file diff --git a/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.md b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.md index bd898658..3ec9fed0 100644 --- a/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.md +++ b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.md @@ -542,6 +542,38 @@ inline fun Class<*>.constructor(initiate: ConstructorConditions): ConstructorFin `v1.0.2` `移除` +## Class.generic - ext-method + +```kotlin:no-line-numbers +fun Class<*>.generic(): GenericClass? +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 获得当前 `Class` 的泛型父类。 + +如果当前实例不存在泛型将返回 `null`。 + +## Class.generic - ext-method + +```kotlin:no-line-numbers +inline fun Class<*>.generic(initiate: GenericClass.() -> Unit): GenericClass? +``` + +**变更记录** + +`v1.1.0` `新增` + +**功能描述** + +> 获得当前 `Class` 的泛型父类。 + +如果当前实例不存在泛型将返回 `null`。 + ## Any.current - ext-method ```kotlin:no-line-numbers diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/CurrentClass.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/CurrentClass.kt index 7b557dd9..b96a41ca 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/CurrentClass.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/CurrentClass.kt @@ -34,6 +34,7 @@ import com.highcapable.yukihookapi.hook.core.finder.members.MethodFinder import com.highcapable.yukihookapi.hook.core.finder.type.factory.FieldConditions import com.highcapable.yukihookapi.hook.core.finder.type.factory.MethodConditions import com.highcapable.yukihookapi.hook.factory.field +import com.highcapable.yukihookapi.hook.factory.generic import com.highcapable.yukihookapi.hook.factory.method /** @@ -59,6 +60,23 @@ class CurrentClass @PublishedApi internal constructor(@PublishedApi internal val */ val simpleName get() = classSet.simpleName ?: instance.javaClass.simpleName ?: "" + /** + * 获得当前实例中的泛型父类 + * + * 如果当前实例不存在泛型将返回 null + * @return [GenericClass] or null + */ + fun generic() = classSet.generic() + + /** + * 获得当前实例中的泛型父类 + * + * 如果当前实例不存在泛型将返回 null + * @param initiate 实例方法体 + * @return [GenericClass] or null + */ + inline fun generic(initiate: GenericClass.() -> Unit) = classSet.generic(initiate) + /** * 调用父类实例 * @return [SuperClass] @@ -99,6 +117,23 @@ class CurrentClass @PublishedApi internal constructor(@PublishedApi internal val */ val simpleName get() = superClassSet.simpleName ?: "" + /** + * 获得当前实例父类中的泛型父类 + * + * 如果当前实例不存在泛型将返回 null + * @return [GenericClass] or null + */ + fun generic() = superClassSet.generic() + + /** + * 获得当前实例父类中的泛型父类 + * + * 如果当前实例不存在泛型将返回 null + * @param initiate 实例方法体 + * @return [GenericClass] or null + */ + inline fun generic(initiate: GenericClass.() -> Unit) = superClassSet.generic(initiate) + /** * 调用父类实例中的变量 * @param initiate 查找方法体 diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/GenericClass.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/GenericClass.kt new file mode 100644 index 00000000..ed40c287 --- /dev/null +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/bean/GenericClass.kt @@ -0,0 +1,44 @@ +/* + * YukiHookAPI - An efficient Kotlin version of the Xposed Hook API. + * Copyright (C) 2019-2022 HighCapable + * https://github.com/fankes/YukiHookAPI + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * This file is Created by fankes on 2022/9/20. + */ +package com.highcapable.yukihookapi.hook.bean + +import java.lang.reflect.ParameterizedType + +/** + * 当前 [Class] 的泛型父类操作对象 + * @param type 类型声明实例 + */ +class GenericClass internal constructor(private val type: ParameterizedType) { + + /** + * 获得泛型参数数组下标的 [Class] 实例 + * @param index 数组下标 - 默认 0 + * @return [Class] + */ + fun argument(index: Int = 0) = type.actualTypeArguments[index] as Class<*> +} \ No newline at end of file diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt index b62ed91b..b182ee7a 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/factory/ReflectionFactory.kt @@ -31,6 +31,7 @@ package com.highcapable.yukihookapi.hook.factory import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.hook.bean.CurrentClass +import com.highcapable.yukihookapi.hook.bean.GenericClass import com.highcapable.yukihookapi.hook.core.finder.base.rules.ModifierRules import com.highcapable.yukihookapi.hook.core.finder.members.ConstructorFinder import com.highcapable.yukihookapi.hook.core.finder.members.FieldFinder @@ -42,10 +43,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.factory.MethodCondition import com.highcapable.yukihookapi.hook.core.finder.type.factory.ModifierConditions import com.highcapable.yukihookapi.hook.xposed.bridge.status.YukiHookModuleStatus import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics -import java.lang.reflect.Constructor -import java.lang.reflect.Field -import java.lang.reflect.Member -import java.lang.reflect.Method +import java.lang.reflect.* /** * 定义一个 [Class] 中的 [Member] 类型 @@ -170,6 +168,26 @@ inline fun Class<*>.method(initiate: MethodConditions) = MethodFinder(classSet = */ inline fun Class<*>.constructor(initiate: ConstructorConditions = { emptyParam() }) = ConstructorFinder(classSet = this).apply(initiate).build() +/** + * 获得当前 [Class] 的泛型父类 + * + * 如果当前实例不存在泛型将返回 null + * @return [GenericClass] or null + */ +fun Class<*>.generic(): GenericClass? { + checkingInternal() + return genericSuperclass?.let { (it as? ParameterizedType?)?.let { e -> GenericClass(e) } } +} + +/** + * 获得当前 [Class] 的泛型父类 + * + * 如果当前实例不存在泛型将返回 null + * @param initiate 实例方法体 + * @return [GenericClass] or null + */ +inline fun Class<*>.generic(initiate: GenericClass.() -> Unit) = generic()?.apply(initiate) + /** * 获得当前实例的类操作对象 * @param ignored 是否开启忽略错误警告功能 - 默认否