I have some questions when I reflect genericType.First of all,I have a class declaration like this:
public class JoyPageWebDTO<T> {
private int recordCount;
private int pageSize;
private int pageIndex;
private List<T> records;
}
And T is:
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;
}
The result of JoyPageWebDTO after request like this: [http://ift.tt/2a3lHeT]
The question is how can I get the T(AuditSpuWebDTO
) from JoyPageWebDTO
? I want's get the value of AuditSpuWebDTO
.I wrote some code as following to deal this,however,failed!
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
}
}
}
}
When I reflect about the generic type:T from List,the type of the result is TypeVariable
,how can I get the Class<?>
from TypeVariable
? The todo
which I mark is the key crucial problem I have.
Aucun commentaire:
Enregistrer un commentaire