抽象父类获取子类的泛型 或接口泛型
jie通过getClass().getGenericSuperclass()或者子类的泛型
getClass().getGenericInterfaces();获取多个接口的泛型
GenericTypeResolver.resolveTypeArgument(GenericityService.class, GenericitySuper.class)
抽象父类
public abstract class GenericitySuper<T> {protected Class<T> getGenericity(){return (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];}protected List<Class<T>> getInterfaceGenericity(){List<Class<T>> list = new ArrayList<Class<T>>();Type[] types = getClass().getGenericInterfaces();for(Type type:types) {list.add((Class<T>)((ParameterizedType)type).getActualTypeArguments()[0]);}return list;}
}
接口
public interface GenericityInterface<T> {}
public interface Genericity2Interface<T> {}
子类
public class GenericityService extends GenericitySuper<Data> implements GenericityInterface<CardInfo>,Genericity2Interface<Page>{public static void main(String[] args) {GenericityService service = new GenericityService();System.out.println(service.getGenericity().getSimpleName());for(Class cls:service.getInterfaceGenericity()) {System.out.println(cls.getSimpleName());}}}