首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

获取泛型转过来的种

2012-09-08 
获取泛型转过来的类public Class getGenericType(int index) {Type genType getClass().getGenericSuper

获取泛型转过来的类
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索引

热点排行