首页 > 其他分享 >通过反射获取泛型

通过反射获取泛型

时间:2023-03-26 15:56:11浏览次数:32  
标签:反射 java System 获取 泛型 import ParameterizedType Type out

package edu.wtbu;

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

//通过反射获取泛型
public class Demo01 {
public static void main(String[] args) throws NoSuchMethodException {
Method method = Demo01.class.getMethod("test1", Map.class, List.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);
}
}
}

Method method1 = Demo01.class.getMethod("test2", null);
Type genericReturnType = method1.getGenericReturnType();
if (genericReturnType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
public void test1(Map<String,User> map, List<User> list){
System.out.println("test1");
}
public Map<String,User> test2(){
System.out.println("test2");
return null;
}
}

标签:反射,java,System,获取,泛型,import,ParameterizedType,Type,out
From: https://www.cnblogs.com/123456dh/p/17258814.html

相关文章

  • iOS应用内获取WiFi SSID
    https://www.jianshu.com/p/0135e7db5a69 iOS如何在应用中连接WIFI1、打开HotspotConfiguration权限。2、在info.plist中添加"Requiredbackgroundmodes",在item0赋......
  • Spring中获取IOC容器中的Bean实例工具封装
    在spring中获取Bean的方式有很多们,这里使用实现ApplicationContextAware接口的方式封装一个可以快速获取Bean实例的方式,具体实现实例: 实现ApplicationContextAware接口......
  • 前端获取视频缩略图方式
    代码示例:/***获取缩略图*@paramurl视频地址*@paramcurrentTime缩略图取第几秒的图片*@paramwidth截取的图片宽*@paramheight截取的图片高*@ret......
  • Go06-文件操作+单元测试+goroutine+channel+反射
    Go06-文件操作+单元测试+goroutine+channel+反射1.打开和关闭文件funcmain(){ //1打开文件。 //file可以称为file对象、file指针、file文件句柄。 file,err:=......
  • 获取标签的自定义属性
    <template><div><!--注意命名data-格式--><h1:data-myName="name":data-age="age"@click="btn">测试</h1></div></template><script>exportdefaul......
  • Java 泛型
    泛型1.泛型引入不能对加入到集合ArrayList中的数据类型进行约束(不安全)遍历的时候,需要进行类型转换,如果集合中的数据量较大,对效率有影响packagegeneric_;public......
  • Java获取系统时间的4种方法
    Java获取系统时间的4种方法1publicDateTime(){2//1.设置时间格式3/*4*yyyy-MM-dd:年-月-日2023-03-255......
  • python字典的值的路径集合,获取所有值的key路径,字典的键路径,
    #回溯算法获取字典所有值的键的集合defget_dict_path(data):ret=[]path=[]deftraceback(path,data):iflen(path)>0andnotisinstance(data,......
  • EL表达式获取数据及细节
    EL表达式没有空指针异常、索引越界异常;EL表达式没有字符串的拼接;<%@pageimport="www.hw.demo.Student"%><%@pageimport="java.util.ArrayList"%><%@pageimpor......
  • c# 获取系统dpi
    c#获取系统dpi正在搜索:c#获取系统dpi正在为你生成答案…您可以使用以下代码获取当前屏幕的DPI:GraphicscurrentGraphics=Graphics.FromHwnd(newWindowInterop......