通过反射获得泛型的实际参数类型

最近学到一个知识点,通过反射获得泛型的实际参数类型,记录一下

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.List;

public class Test implements Serializable {
    public static void main(String[] args) throws NoSuchMethodException {
        Method method = Test.class.getMethod("getGenericType", List.class);
        Type[] types = method.getGenericParameterTypes();
        System.out.println(types[0].getTypeName());
    }

    public void getGenericType(List<BigDecimal> dateList) {
    }
}

输出结果为

java.util.List<java.math.BigDecimal>

可以看得到了泛型的实际参数类型

你可能感兴趣的:(技术文章)