- public class GenericClass {
- public static void main(String args[]) throws SecurityException, NoSuchMethodException {
- //通过方法,可以获取方法上的参数的泛型类型
- Method applyMethod = GenericClass.class.getMethod("applyVector", Vector.class);
- //泛型的参数类型(如果只有一个参数,那么就取第一个)
- Type[] types = applyMethod.getGenericParameterTypes();
- ParameterizedType pType = (ParameterizedType)types[0];
- //获取方法参数泛型类型,那么就为Date
- System.err.println(pType.getActualTypeArguments()[0]);
- //获取方法参数类型,那么就为Vector
- System.err.println(pType.getRawType());
- }
- public static void applyVector(Vector<Date> v1){
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1602201614535.html
来源: http://www.codesnippet.cn/detail/1602201614535.html