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

xiaoxiao2025-11-19  7

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

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>

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

转载请注明原文地址: https://www.6miu.com/read-5039899.html

最新回复(0)