如何在没有参数的情况下获得泛型类的类? [英] How to get class of generic type when there is no parameter of it?
问题描述
我刚刚了解到这个美观的语法
Collections。< String> emptyList()
清空列表
,其元素类型应该是 String
。 Java的源代码如下所示:
public static final List EMPTY_LIST = new EmptyList< Object>();
:
public static final< T>列表与LT; T> emptyList(){
return(List< T>)EMPTY_LIST;
$ / code>
现在,如果我以通用类型没有出现的方式编写方法在参数列表中,有什么办法可以访问实际的类,它变成了 T
?
I'我说,到目前为止,我的方法来编码同样的事情会是
private< T> T get(String key,Class< T> clazz){
//在这里我可以用clazz做任何我想做的事情,例如:
return clazz.cast(value);
$ / code>
如果我删除了 clazz
-parameter我无法执行 cast()
。显然我可以做
pre $ return $(T)value;
但是这给了我通常的警告类型安全性:未检查从Object转换为Ť
。好的, @SuppressWarnings(unchecked)
在这里有帮助,但实际上我想用方法的预期返回类型做一些事情。如果我添加一个局部变量
T retValue;
我必须用 我可以这样做,例如 因为Java(或者至少是我的Java 6)的确做到了这一点,所以我没有提供任何关于 请注意,我列出的例子非常简单,没有多大意义。我想在这里做更复杂的事情,但那不在范围之内。 在运行时输入你需要把它作为一个字段,或者为特定的类型组合创建一个类型的子类。 打印 I just learned about this fine looking syntax to get an empty Now if I code a method in that way where the generic type does not appear in the parameter list, is there any way how I can access the actual class that becomes I'm saying, up to now my approach to code the same thing would have been If I removed the but that gives me the usual warning I'd have to initialise it with something, I could do, e.g. but if the cast fails I end up with no information about Since Java (or at least my Java 6) does not have the generic info any more during runtime, I currently can't think of a way to do this. Is there a way? Or do I have to stick with my "old" approach here? Please note that the example I lined out is very simple and doesn't make much sense. I want to do more complicated stuff here, but that's out of the scope. If you want the generic type at runtime you need to either have it as a field or create a sub-class of a type for a specific combination of types. e.g. prints
这篇关于如何在没有参数的情况下获得泛型类的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! null $ c来初始化它$ c>没有帮助。当我将它赋值为
$ p $ @SuppressWarnings(unchecked)
T retValue =(T)value;
retValue.getClass()。getName()
T
的信息。 在运行期间不再有通用信息,我目前无法想到这样做的方法。有没有办法?或者我必须坚持我的老方法?
$ b $ p例如
列表< String> list = new ArrayList< String>(){}; //创建通用的子类型
final类型=(Class)((ParameterizedType)list.getClass()
.getGenericSuperclass())。getActualTypeArguments()[0];
System.out.println(type);
class java.lang.String
Collections.<String>emptyList()
List
with elements which are supposedly of type String
. Java's source looks like this:public static final List EMPTY_LIST = new EmptyList<Object>();
:
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
T
?private <T> T get(String key, Class<T> clazz) {
// here I can do whatever I want with clazz, e.g.:
return clazz.cast(value);
}
clazz
-parameter I wouldn't be able to do the cast()
. Obviously I could do return (T) value;
Type safety: Unchecked cast from Object to T
. Ok, @SuppressWarnings("unchecked")
helps here, but actually I want to do something with the intended return type of the method. If I add a local variableT retValue;
null
doesn't help. After I assign it like@SuppressWarnings("unchecked")
T retValue = (T) value;
retValue.getClass().getName()
T
again.List<String> list = new ArrayList<String>() {}; // creates a generic sub-type
final Class type = (Class) ((ParameterizedType) list.getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
System.out.println(type);
class java.lang.String