Edit Since there were many downvotes and people who didn't understand what I'm asking for, I'll rephrase:
编辑因为有许多downvotes和那些不理解我要求的人,我会改写:
How do I find out at runtime what is the class that foo was generified from?
我如何在运行时发现foo被泛化的类是什么?
public boolean doesClassImplementList(Class<?> genericClass)
{
// help me fill this in
// this method should return true if genericClass implements List
// I can't do this because it doesn't compile:
return genericClass instanceof List;
}
4 个解决方案
#1
Class.isAssignableFrom
#2
Class<Integer>
is a type declaration, you can't assign it to a variable, as you are doing on your first line.
Class
Class<Integer>
is basically a way of referencing the Integer type, so you can pass it as a parameter. It does affect some methods (such as cast) but it is basically a way of providing a generic parameter to a method and associate it with a type.
Class
For example:
public <T> T someMethod(Object someObject, Class<T> castTo) {
return castTo.cast(someObject);
}
#4
You should be using Class.forName( "fully_qualified_name" ) to generate a Class object, or using the .getClass() method on an already instantiated object. Also, for int types, the class is defined in the Integer.TYPE property.
您应该使用Class.forName(“fully_qualified_name”)生成Class对象,或者在已实例化的对象上使用.getClass()方法。此外,对于int类型,该类在Integer.TYPE属性中定义。
I need a little more clarification of what you are attempting to do, match the same type? You can use a number of methods, what is the end goal?
我需要更多一点澄清你想要做什么,匹配相同的类型?你可以使用多种方法,最终目标是什么?
Ok, now that you've clarified... heres some help ... just pass in a class..
好的,既然你已经澄清了...继承人帮助......只是通过一堂课......
public boolean doesClassImplementList(Class genericClass)
{
// help me fill this in
// this method should return true if genericClass implements List
// I can't do this because it doesn't compile:
//this will give you a a list of class this class implements
Class[] classesImplementing = genericClass.getInterfaces();
//loop through this array and test for:
for(int i=0; i<classesImplementing.length; i++) {
if (classesImplementing[i].getName().equals("java.util.List")) {
return true;
}
}
return false;
}
#1
Class.isAssignableFrom
#2
Class<Integer>
is a type declaration, you can't assign it to a variable, as you are doing on your first line.
Class
Class<Integer>
is basically a way of referencing the Integer type, so you can pass it as a parameter. It does affect some methods (such as cast) but it is basically a way of providing a generic parameter to a method and associate it with a type.
Class
For example:
public <T> T someMethod(Object someObject, Class<T> castTo) {
return castTo.cast(someObject);
}
#3
you cannot in Java (type erasure)
你不能用Java(类型擦除)
#4
You should be using Class.forName( "fully_qualified_name" ) to generate a Class object, or using the .getClass() method on an already instantiated object. Also, for int types, the class is defined in the Integer.TYPE property.
您应该使用Class.forName(“fully_qualified_name”)生成Class对象,或者在已实例化的对象上使用.getClass()方法。此外,对于int类型,该类在Integer.TYPE属性中定义。
I need a little more clarification of what you are attempting to do, match the same type? You can use a number of methods, what is the end goal?
我需要更多一点澄清你想要做什么,匹配相同的类型?你可以使用多种方法,最终目标是什么?
Ok, now that you've clarified... heres some help ... just pass in a class..
好的,既然你已经澄清了...继承人帮助......只是通过一堂课......
public boolean doesClassImplementList(Class genericClass)
{
// help me fill this in
// this method should return true if genericClass implements List
// I can't do this because it doesn't compile:
//this will give you a a list of class this class implements
Class[] classesImplementing = genericClass.getInterfaces();
//loop through this array and test for:
for(int i=0; i<classesImplementing.length; i++) {
if (classesImplementing[i].getName().equals("java.util.List")) {
return true;
}
}
return false;
}