读书人

获取泛型转过来的种

发布时间: 2012-09-08 10:48:07 作者: rapoo

获取泛型转过来的类
public Class getGenericType(int index) {
Type genType = getClass().getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
throw new RuntimeException("Index outof bounds");
}
if (!(params[index] instanceof Class)) {
return Object.class;
}
return (Class) params[index];
}

例如:Class<T,E>;T为0索引

读书人网 >编程

热点推荐