0
我有一些问题,当我反映所有的genericType.First,我有一个类的声明是这样的:我如何反映类<T>的列表<T>并获取T?
public class JoyPageWebDTO<T> {
private int recordCount;
private int pageSize;
private int pageIndex;
private List<T> records;
}
,T是:
public class AuditSpuWebDTO implements Serializable {
private String indications;
private String forbidden;
private int shopID;
private String shopName;
private String city;
private Date updateTime;
private int auditStatus;
private String auditContent;
}
JoyPageWebDTO像这样的请求后,其结果是: [http://i.stack.imgur.com/zeK1O.jpg][1]
问题是我怎么能从JoyPageWebDTO
得到T(AuditSpuWebDTO
)?我想得到AuditSpuWebDTO
的价值。我写了一些代码来处理这个问题,但是,失败了!
public static void reflect(Class<?> clazz) {
for (Field field : clazz.getDeclaredFields()) {
//get the static type of the field
Class<?> fieldType = field.getType();
//if it's String,
if (fieldType == String.class) {
// save/use field
}
//if it's String[],
else if (fieldType == String[].class) {
// save/use field
}
//if it's List or a subtype of List,
else if (List.class.isAssignableFrom(fieldType)) {
//get the type as generic
ParameterizedType fieldGenericType = (ParameterizedType) field.getGenericType();
//get it's first type parameter
Type type = fieldGenericType.getActualTypeArguments()[0];
Class<?> fieldTypeParameterClazz;
if (type instanceof ParameterizedType) {
fieldTypeParameterClazz = (Class<?>) ((ParameterizedType) type).getRawType();
} else if(type instanceof TypeVariable){
//todo how to get the class????
}else{
fieldTypeParameterClazz = (Class<?>) type;
}
//if the type parameter is String,
if (fieldTypeParameterClazz == String.class) {
// save/use field
}
}
}
}
当我反映了泛型类型:从T,因此对结果的类型为TypeVariable
,我怎样才能从TypeVariable
的Class<?>
?我标记的todo
是我的关键问题。
[运行时获取泛型类的可能的副本](http://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime) – Sam
快速回答:不可能。 – Sam