//利用ParameterizedType获取java泛型的参数类型
public class Demo {public static void main(String[] args) {classTest();interfaceTest();}private static void classTest() {MySuperClass<Student, String> mySuperClass = new MySuperClass<Student, String>() {@Overridepublic void onSuccess(Student data) {}};//getClass().getGenericSuperclass()返回表示此 Class 所表示的实体的直接超类的 TypeParameterizedType type = (ParameterizedType) mySuperClass.getClass().getGenericSuperclass();sysoType(type);}private static void interfaceTest() {MyInterface<Student, String> myInterface = new MyInterface<Student, String>() {@Overridepublic void onSuccess(Student data) {}};ParameterizedType type = (ParameterizedType) myInterface.getClass().getGenericInterfaces()[0];sysoType(type);}private static void sysoType(ParameterizedType type) {/*com.bqt.generic.MySuperClass<com.bqt.generic.Student, java.lang.String>class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImplclass com.bqt.generic.Studentclass java.lang.Classclass java.lang.Stringclass java.lang.Classcom.bqt.generic.MyInterface<com.bqt.generic.Student, java.lang.String>class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImplclass com.bqt.generic.Studentclass java.lang.Classclass java.lang.Stringclass java.lang.Class*/System.out.println(type + "\n" + type.getClass());//返回表示此类型实际类型参数的 Type 对象的数组,泛型的参数可能有多个,我们需要哪个就取哪个Type[] targets = type.getActualTypeArguments();for (int i = 0; i < targets.length; i++) {System.out.println(targets[i] + "\n" + targets[i].getClass());}}}
public abstract class MySuperClass<T,V> {
public abstract void onSuccess(T data);}
public interface MyInterface<T,V> {void onSuccess(T data);}
public class Student {
}