//任务6:利用反射获取方法的泛型参数、泛型异常、泛型返回值
public <T> void applyList(List<T> list) throws Exception{
/**
* 思路:
* 1.获取方法
* 2.获取方法的泛型参数
*/
Method method = GenericTest.class.getMethod("applyList", List.class);
//知识点:参数化的类型,ParameterizedType
/*
* 下面是错误的
* ParameterizedType[] pt= (ParameterizedType[]) method.getGenericParameterTypes();
*/
Type[] types=method.getGenericParameterTypes();
//获取types中的元素,转成ParameterizedType这种类型
ParameterizedType pt=(ParameterizedType) types[0];
System.out.println(pt.getRawType()+";"+pt.getActualTypeArguments()[0]);
}
public void testApplyList() throws Exception{
applyList(new ArrayList<Integer>());
}
二 泛型DAO
public class GenericDao<E> {
public void add(E x){
}
public E findById(int id){
return null;
}
public void delete(E obj){
}
public void delete(int id){
}
public void update(E obj){
}
public static <E> void update2(E obj){
}
public E findByUserName(String name){
return null;
}
public Set<E> findByConditions(String where){
return null;
}
}