在基类中直接获取到其泛型参数的Class

今天看到一个方法,在基类中直接获取到其泛型参数的Class,如下例:
MyDao extends BaseDao<Book> {...}

通过如下方法,可以在BaseDao中通过反射的方法,得到Book.class:
Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

原文见:http://xuliangyong.iteye.com/blog/51083

猜你喜欢

转载自lvjinhua.iteye.com/blog/1487024