![](/img/trans.png)
[英]Java weird compilation error with raw generic type parameter and Optionals
[英]Java create objects of generic type with unknown in compilation type parameter
我有通用类:
class Field<T> { }
还有一些其他类(我只在运行时知道它的类型)有很多get方法,例如:
class A{
public Date getDate();
public String getName();
public Integer getNumber();
}
我想为所有get方法创建类Field
实例, T
等于返回这些get
方法的类型。 对于此示例Field<Date>
, Field<String>
, Field<Integer>
。
有人可以帮忙吗?
您通常将反射用于您在运行时才知道的事物。 泛型信息在编译时被删除。 因此,虽然有些情况下你可以混合两者,但这并不常见。
我想为所有get方法创建类Field的实例,T等于返回这些get方法的类型。 对于此示例Field,Field,Field。
从字面上回答你的问题:
Field<Date> dateField = new Field<Date>();
Field<String> nameField = new Field<String>();
Field<Integer> numberField = new Field<Integer>();
class Field<T> {
T value;
Field(T value) {this.value = value;}
}
class A{
Date date;
public Field<Date> getDate() {
return new Field<Date>(date);
}
//etc....
}
将泛型视为编译器注释而不是实际的运行时类型检查。 编译后的代码将只具有静态强制转换,并且根本不执行任何泛型类型检查。 使用Sun称为“类型擦除”的内容,泛型功能只是添加到编译器以提供与旧JVM的运行时兼容性。 这里有一些很好的阅读:
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
在这种情况下,我不相信你可以创建泛型类型的Field,因为在编译时检查泛型,而你只能在运行时获得(通过反射)类A的声明方法的返回类型。
要获得吸气剂,请考虑使用内省而不是反射:它旨在用于此目的!
这是一个自动获取getter的代码。 当心,你也得到“getClass()”getter!
for(PropertyDescriptor descriptor :Introspector.getBeanInfo(A.class).getPropertyDescriptors()){
System.out.println("Getter method :" + descriptor.getReadMethod());
System.out.println("Return type : " + descriptor.getReadMethod().getReturnType());
Class<?> c=descriptor.getReadMethod().getReturnType();
}
}
输出:
Getter method :public final native java.lang.Class java.lang.Object.getClass()
Return type : class java.lang.Class
Getter method :public java.util.Date A.getDate()
Return type : class java.util.Date
Getter method :public java.lang.String A.getName()
Return type : class java.lang.String
Getter method :public java.lang.Integer A.getNumber()
Return type : class java.lang.Integer
然后,您可以轻松地为每个getter创建一个Field。 对于泛型类型,它在运行时没有意义,但仍然可以在代码生成器中使用它...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.