首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-10-31 
43 通过反射获得泛型的实际类型参数??public static void applyVector(VectorDate v1){}Method methGen

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

?

?

public static void applyVector(Vector<Date> v1){}Method meth=GenericTest.class.getMethod("applyVector", Vector.class);Type[] types=meth.getGenericParameterTypes();ParameterizedType pys=(ParameterizedType) types[0];//ParameterizedType类型是Tpye类型其中之一的儿子System.out.println(pys.getRawType());//得到她的原始类型//class java.util.VectorSystem.out.println(pys.getActualTypeArguments()[0]);//得到实际的类型参数,注意他是个数组,因为他可能有多个,例如HashMap<K,V>//class java.util.Vector

?

?

Class GenericalReflection { private Vector<Date> dates = new Vector<Date>(); public void setDates(Vector<Date> dates) { this.dates = dates; } public static void main(String[] args) { Method methodApply = GenericalReflection.class.getDeclaredMethod("applyGeneric", Vector.class); ParameterizedType pType = (ParameterizedType) (methodApply .getGenericParameterTypes())[0]; System.out.println("setDates(" + ((Class) pType.getRawType()).getName() + "<" + ((Class) (pType.getActualTypeArguments()[0])).getName() + ">)" ); }}

?

?

?????? }

??????}

?? public class ArticleDaoImpl extendsDaoBaseImpl<Article> implements ArticleDao {

?????? }

?

?

?

?

?

?

?

热点排行