From c9e2720afcd4d4f3a8f75355ab46d660df13d6d3 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sun, 1 Oct 2023 00:59:55 +0800 Subject: [PATCH] fix: make actualTypeArguments not force cast to Class type --- .../yukihookapi/hook/bean/GenericClass.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/bean/GenericClass.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/bean/GenericClass.kt index 2756d491..de82d25b 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/bean/GenericClass.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/bean/GenericClass.kt @@ -39,18 +39,25 @@ class GenericClass internal constructor(private val type: ParameterizedType) { /** * 获得泛型参数数组下标的 [Class] 实例 + * + * - 在运行时局部变量的泛型会被擦除 - 获取不到时将会返回 null * @param index 数组下标 - 默认 0 - * @return [Class] + * @return [Class] or null */ - fun argument(index: Int = 0) = type.actualTypeArguments[index] as Class<*> + fun argument(index: Int = 0) = type.actualTypeArguments[index] as? Class<*>? /** * 获得泛型参数数组下标的 [Class] 实例 + * + * - 在运行时局部变量的泛型会被擦除 - 获取不到时将会返回 null * @param index 数组下标 - 默认 0 - * @return [Class]<[T]> + * @return [Class]<[T]> or null * @throws IllegalStateException 如果 [Class] 的类型不为 [T] */ @JvmName("argument_Generics") inline fun argument(index: Int = 0) = - type.actualTypeArguments[index] as? Class ?: error("Target Class type cannot cast to ${T::class.java}") + type.actualTypeArguments[index].let { args -> + if (args is Class<*>) args as? Class? ?: error("Target Class type cannot cast to ${T::class.java}") + else null + } } \ No newline at end of file