From 5e34d66f964e1b5f4ee992cc89cd202a70275e72 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sun, 1 Oct 2023 00:59:51 +0800 Subject: [PATCH] fix: make actualTypeArguments not force cast to Class type --- .../yukireflection/bean/GenericClass.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/bean/GenericClass.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/bean/GenericClass.kt index 44f8f09..122827c 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/bean/GenericClass.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/bean/GenericClass.kt @@ -40,18 +40,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