如何获得Kotlin中实际泛型参数的实际类型参数? [英] How to get actual type arguments of a reified generic parameter in Kotlin?

查看:1971
本文介绍了如何获得Kotlin中实际泛型参数的实际类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用指定类型参数,可以编写一个内联函数在运行时通过反射类型参数:

  inline fun< reified T:Any> f(){
val clazz = T :: class
// ...
}

但是当 f用本身是泛型类的参数调用时,似乎没有办法通过 T :: class

  f< List< Integer>>( )// T :: class只是kotlin.collections.List 

有没有办法让实际通过反射类型化泛化泛型的参数?

解决方案

由于 type erasure ,实际泛型参数不能通过 T :: class 通用类的标记获得。不同类型的对象必须具有相同的类标记,这就是为什么它不能包含实际的泛型参数。



但有一个叫做超级类型令牌,它可以在编译时已知类型的情况下给出实际的类型参数(对于Kotlin中的泛化泛型而言,这是因为内联)。

诀窍是编译器保留了从泛型类派生的非泛型类的实际类型参数(它的所有实例都会有相同的参数,很好的解释这里)。它们可以通过 Class <*> 实例的 clazz.genericSuperClass.actualTypeArguments 访问。



给出所有这些,你可以这样编写一个util类:

  abstract class TypeReference< T> :Comparable< TypeReference< T>> {
val type:Type =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments [0]

override fun compareTo(other:TypeReference< T>)= 0
}

Jackson TypeReference ,它使用相同的方法。杰克逊Kotlin模块在使用泛化泛型时使用它

然后,在具有泛化泛型的内联函数中, TypeReference 需要被分类(可以使用类型



示例: p>

  inline fun< reified T:Any> printGeneric(){
val type = object:TypeReference< T>(){} .type
if(type是ParameterizedType)
type.actualTypeArguments.forEach {println(it.typeName)}
}

printGenerics< HashMap< Int,List< String> >>>()


  java.lang.Integer 
java.util.List<? extends java.lang.String>



Using reified type parameters, one can write an inline function which works with the type parameter through reflection at runtime:

inline fun <reified T: Any> f() {
    val clazz = T::class
    // ...
}

But when f is called with a parameter which is itself a generic class, there seems to be no way to obtain its actual type arguments through T::class:

f<List<Integer>>() // T::class is just kotlin.collections.List

Is there a way to get actual type arguments of a reified generic through reflection?

解决方案

Due to type erasure, actual generic arguments cannot be obtained through T::class token of a generic class. Different objects of a class must have the same class token, that's why it cannot contain actual generic arguments.

But there is a techinque called super type tokens which can give actual type arguments in case when the type is known at compile time (it is true for reified generics in Kotlin because of inlining).

The trick is that the compiler retains actual type arguments for a non-generic class derived from a generic class (all its instances will have the same arguments, good explanation here). They are accessible through clazz.genericSuperClass.actualTypeArguments of a Class<*> instance.

Given all that, you can write a util class like this:

abstract class TypeReference<T> : Comparable<TypeReference<T>> {
    val type: Type = 
        (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0]

    override fun compareTo(other: TypeReference<T>) = 0
}

Explained in Jackson TypeReference which uses the same approach. Jackson Kotlin module uses it on reified generics.

After that, in an inline function with a reified generic, TypeReference needs to be subclassed (an object expression will go), and then its type can be used.

Example:

inline fun <reified T: Any> printGenerics() {
    val type = object : TypeReference<T>() {}.type
    if (type is ParameterizedType)
        type.actualTypeArguments.forEach { println(it.typeName) }
}

printGenerics<HashMap<Int, List<String>>>():

java.lang.Integer
java.util.List<? extends java.lang.String>

这篇关于如何获得Kotlin中实际泛型参数的实际类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆