首页 > 其他 > 详细

反射获取泛型

时间:2020-09-21 00:06:12      阅读:49      评论:0      收藏:0      [点我收藏+]
package com.sinosoft;

import com.sinosoft.lis.bq.PEdorAppBL;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

//使用反射获取泛型信息
public class Test {

public String test01(Map<String, PEdorAppBL> map,Double dou){
return "yyy";

}
public Map<String, PEdorAppBL> test02(){
return null;

}

public static void main(String[] args) throws NoSuchMethodException {
System.out.println("方法1");
Method method = Test.class.getMethod("test01", Map.class, Double.class);
Type[] genericParameterTypes = method.getGenericParameterTypes();
for (Type genericParameterType:genericParameterTypes) {
System.out.println("=========="+genericParameterType);
if(genericParameterType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for(Type actualTypeArgument:actualTypeArguments){
System.out.println(actualTypeArgument);
}

}
}
System.out.println("方法2");
Method method2 = Test.class.getMethod("test02", null);
Type genericReturnType = method2.getGenericReturnType();
if(genericReturnType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for(Type actualTypeArgument:actualTypeArguments){
System.out.println(actualTypeArgument);
}

}
}
}



运行结果:

方法1
==========java.util.Map<java.lang.String, com.sinosoft.lis.bq.PEdorAppBL>
class java.lang.String
class com.sinosoft.lis.bq.PEdorAppBL
==========class java.lang.Double
方法2
class java.lang.String
class com.sinosoft.lis.bq.PEdorAppBL

 

反射获取泛型

原文:https://www.cnblogs.com/dongyaotou/p/13702945.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!