java 从 Class<T> 转换为 TypeReference<T>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13750754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:51:56  来源:igfitidea点击:

Convert from Class<T> to TypeReference<T>

javaclassgenericsHymanson

提问by Jason

Basically just the opposite of this question. I want to convert a Class<T> object to a TypeReference<T> object.

基本上刚好与这个问题相反。我想将 Class<T> 对象转换为 TypeReference<T> 对象。

I have tried:

我努力了:

foo(Class<T> valueType)
{
   TypeReference ref = new TypeReference<T>(){}; 
}

but that just returns a type reference of the classes's super class. I also tried:

但这只是返回类的超类的类型引用。我也试过:

foo(Class<T> valueType)
{
   TypeReference ref = new TypeReference<valueType>(){}; 
}

and

foo(Class<T> valueType)
{
   TypeReference ref = new TypeReference<valueType.getRawClass()>(){}; 
}

But the second two don't compile. How do I do this?

但后两个不编译。我该怎么做呢?

回答by SLaks

You can override getType()to return your type:

您可以覆盖getType()以返回您的类型:

new TypeReference<T>(){
    @Override
    public Type getType() {
        return valueType;
    }
}; 

回答by StaxMan

As others have pointed out, you may well be solving wrong problem. What you may want to unify towards is actually JavaType, because that is what both of your inputs get converted to, and that is what Hymanson internally uses for everything. And then you can have convenience methods to get to JavaType.

正如其他人指出的那样,您很可能正在解决错误的问题。实际上JavaType,您可能想要统一的是,因为这就是您的两个输入都转换为的内容,而这正是 Hymanson 内部用于所有内容的内容。然后你可以有便捷的方法来访问JavaType.

回答by Guido Simone

I want to convert a Class object to a TypeReference object.

我想将 Class 对象转换为 TypeReference 对象。

It may be possible to do this, but I cannot think of any realistic problem that it solves. Consider this info from the TypeReference javadoc

这样做是可能的,但我想不出它可以解决任何现实问题。考虑来自TypeReference javadoc 的这个信息

This class is used to pass full generics type information, and avoid problems with type erasure (that basically removes most usable type references from runtime Class objects).

此类用于传递完整的泛型类型信息,并避免类型擦除问题(这基本上从运行时 Class 对象中删除了大多数可用的类型引用)。

My interpretation of this is the only time you would use TypeReference instead of Class is when you need more type information that a Class object can provide. So even if you can figure out how to do this conversion - what have you gained? There is no way to put back erased type information from a Class into a TypeReference.

我对此的解释是,当您需要 Class 对象可以提供的更多类型信息时,您将使用 TypeReference 而不是 Class。所以即使你能弄清楚如何进行这种转换——你得到了什么?无法将已擦除的类型信息从 Class 放回 TypeReference。

Any time I have a situation where I need a TypeReference instead of Class, I create a concrete type reference instance and pass that object at run time. Something like this (may not compile, but you get the idea)

每当遇到需要 TypeReference 而不是 Class 的情况时,我都会创建一个具体的类型引用实例并在运行时传递该对象。像这样的东西(可能无法编译,但你明白了)

foo(TypeReference<T> refArg)
{
   TypeReference<T> ref = refArg; 
}

// later on
object.foo(new TypeReference<List<Set<MyObject>>(){});