public class getVectorClass {
/*
* 获取Vector中的泛型的类型。应用与java框架中
*/
public static void main(String[] args) throws SecurityException, NoSuchMethodException{
Class cls = getVectorClass.class;
Method method = cls.getMethod("apply", Vector.class);
Type[] type= method.getGenericParameterTypes();
ParameterizedType t = (ParameterizedType)type[0];
System.out.println(t.getActualTypeArguments()[0]);//获得泛型class java.util.Date
System.out.println(t.getRawType());//获得原始类型。class java.util.Vector
}
public void apply(Vector<Date> t){
}
}
获取类上面泛型的类
public class BaseService<T> {
/**
* 实体类class对象
*/
protected Class<T> clazz;
/**
* 实体类名称
*/
protected String className;
public BaseService() {
Type type = getClass().getGenericSuperclass();
Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
this.clazz = (Class<T>) trueType;
this.className = clazz.getSimpleName();
}
}