参数化

@Repository
public class RecommendContentDaoImpl extends MongoDbBaseDaoImpl<RecommendContent> {

public static void main(String[] args) {
Class<Object> genricType = getSuperClassGenricType(new RecommendContentDaoImpl().getClass(), 0);
System.out.println(genricType);
}

private static Class<Object> getSuperClassGenricType(final Class clazz, final int index) {
//返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。
Type genType = clazz.getGenericSuperclass();

if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
//返回表示此类型实际类型参数的 Type 对象的数组。
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

if (index >= params.length || index < 0) {
return Object.class;
}
if (!(params[index] instanceof Class)) {
return Object.class;
}

return (Class) params[index];
}

}

猜你喜欢

转载自www.cnblogs.com/gotodsp/p/8855687.html