我怎样才能做到这一点呢?
public class GenericClass<T>
{
public Type getMyType()
{
//How do I return the type of T?
}
}
到目前为止,我所尝试的一切总是返回Object类型,而不是使用的特定类型。
我怎样才能做到这一点呢?
public class GenericClass<T>
{
public Type getMyType()
{
//How do I return the type of T?
}
}
到目前为止,我所尝试的一切总是返回Object类型,而不是使用的特定类型。
当前回答
以下是我的诀窍:
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(Main.<String> getClazz());
}
static <T> Class getClazz(T... param) {
return param.getClass().getComponentType();
}
}
其他回答
这是我的解决方案:
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
public class GenericClass<T extends String> {
public static void main(String[] args) {
for (TypeVariable typeParam : GenericClass.class.getTypeParameters()) {
System.out.println(typeParam.getName());
for (Type bound : typeParam.getBounds()) {
System.out.println(bound);
}
}
}
}
使用番石榴。
import com.google.common.reflect.TypeToken;
import java.lang.reflect.Type;
public abstract class GenericClass<T> {
private final TypeToken<T> typeToken = new TypeToken<T>(getClass()) { };
private final Type type = typeToken.getType(); // or getRawType() to return Class<? super T>
public Type getType() {
return type;
}
public static void main(String[] args) {
GenericClass<String> example = new GenericClass<String>() { };
System.out.println(example.getType()); // => class java.lang.String
}
}
不久前,我在这里发布了一些完整的示例,包括抽象类和子类。
注意:这需要实例化GenericClass的一个子类,这样它才能正确地绑定类型参数。否则它只会返回类型为T。
这是受到Pablo和CoolMind的回答的启发。 我偶尔也会使用kayz1的答案中的技巧(在许多其他答案中也有表达),我相信这是一种完成OP要求的体面而可靠的方法。
我选择首先将其定义为一个接口(类似于PJWeisberg),因为我有可以从该功能中受益的现有类型,特别是异构泛型联合类型:
public interface IGenericType<T>
{
Class<T> getGenericTypeParameterType();
}
我在一个通用匿名接口实现中的简单实现如下所示:
//Passed into the generic value generator function: toStore
//This value name is a field in the enclosing class.
//IUnionTypeValue<T> is a generic interface that extends IGenericType<T>
value = new IUnionTypeValue<T>() {
...
private T storedValue = toStore;
...
@SuppressWarnings("unchecked")
@Override
public Class<T> getGenericTypeParameterType()
{
return (Class<T>) storedValue.getClass();
}
}
我想这也可以通过用类定义对象作为源来实现,这只是一个单独的用例。 我认为关键在于,正如许多其他答案所述,以某种方式,您需要在运行时获得类型信息,以便在运行时可用;对象本身保持其类型,但是擦除(也如其他人所说,使用适当的引用)会导致任何封闭/容器类型丢失该类型信息。
Java泛型大多是在编译时,这意味着类型信息在运行时丢失。
class GenericCls<T>
{
T t;
}
会被编译成什么样子
class GenericCls
{
Object o;
}
要在运行时获得类型信息,必须将其作为ctor的参数添加。
class GenericCls<T>
{
private Class<T> type;
public GenericCls(Class<T> cls)
{
type= cls;
}
Class<T> getType(){return type;}
}
例子:
GenericCls<?> instance = new GenericCls<String>(String.class);
assert instance.getType() == String.class;
我认为还有另一种优雅的解决方案。
你要做的是(安全地)将泛型类型参数的类型从concerete类“传递”给父类。
如果您允许自己将类类型视为类上的“元数据”,则建议使用Java方法在运行时编码元数据:注释。
首先按照下面的代码定义一个自定义注释:
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EntityAnnotation {
Class entityClass();
}
然后必须将注释添加到子类。
@EntityAnnotation(entityClass = PassedGenericType.class)
public class Subclass<PassedGenericType> {...}
然后你可以使用下面的代码来获取基类中的类类型:
import org.springframework.core.annotation.AnnotationUtils;
.
.
.
private Class getGenericParameterType() {
final Class aClass = this.getClass();
EntityAnnotation ne =
AnnotationUtils.findAnnotation(aClass, EntityAnnotation.class);
return ne.entityClass();
}
这种方法的一些局限性是:
在两个地方指定泛型类型(PassedGenericType),而不是一个非dry类型。 只有当您可以修改具体的子类时,这才有可能。