近日,需要用Annotation(注解)来实现对象的数据Copy。但是有些字段是List或Map,这时就到取到List或Map的范型参数。查找一下JDK类,果真能取出来,记录如下:
[b]1. 模型类[/b]
[b]2. 解析类[/b]
[b]结果是:[/b]
[b]1. 模型类[/b]
public class Model {
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
[b]2. 解析类[/b]
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class ModelParser {
public static void main(String[] args) throws Throwable {
Field field = Model.class.getDeclaredField("list");
Type type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
System.out.println(type);
System.out.println(type == String.class);
}
}
[b]结果是:[/b]
class java.lang.String
true