工作中瞄到的,在此收集了
- public class RawDao<T> {
- protected Class<T> clazz;
- @SuppressWarnings("unchecked")
- public RawDao() {
- @SuppressWarnings("rawtypes")
- Class clazz = getClass();
- while (clazz != Object.class) {
- Type t = clazz.getGenericSuperclass();
- if (t instanceof ParameterizedType) {
- Type[] args = ((ParameterizedType) t).getActualTypeArguments();
- if (args[0] instanceof Class) {
- this.clazz = (Class<T>) args[0];
- break;
- }
- }
- clazz = clazz.getSuperclass();
- }
- }