您的位置:首页 > 其它

通过反射获取泛型参数类型

2013-01-25 16:59 447 查看
public class ReflectDemo2 {

Map<String,Integer> map = new HashMap<String, Integer>();

List<String> list = new ArrayList<String>();

/**
* @param args
* @throws NoSuchFieldException
* @throws SecurityException
*/
public static void main(String[] args) throws SecurityException, NoSuchFieldException {
// TODO Auto-generated method stub
Class clazz = ReflectDemo2.class;

Field f = clazz.getDeclaredField("list");

Class a = f.getType();

System.out.println(a.getName());

Type t = f.getGenericType();

ParameterizedType pt = (ParameterizedType)t;

Type[] ptas = pt.getActualTypeArguments();

for(Type pta : ptas){
System.out.println(pta);
}

}

}
java.util.List

java.lang.Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  泛型