public class GenericClass {
public static voidmain(String args[]) throws SecurityException, NoSuchMethodException {
//通过方法,可以获取方法上的参数的泛型类型
MethodapplyMethod = GenericClass.class.getMethod("applyVector",Vector.class);
//泛型的参数类型(如果只有一个参数,那么就取第一个)
Type[] types =applyMethod.getGenericParameterTypes();
ParameterizedTypepType = (ParameterizedType)types[0];
//获取方法参数泛型类型,那么就为Date
System.err.println(pType.getActualTypeArguments()[0]);
//获取方法参数类型,那么就为Vector
System.err.println(pType.getRawType());
}
public static voidapplyVector(Vector<Date> v1){
}
}
本文介绍了一个使用Java反射API来获取方法上泛型参数类型的示例。通过具体实例展示了如何利用反射机制获取指定方法的参数类型及其泛型信息。
2079

被折叠的 条评论
为什么被折叠?



