zoukankan      html  css  js  c++  java
  • 反射获取泛型

    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://blog.csdn.net/weixin_39452731/article/details/82916840

     
  • 相关阅读:
    Swift3.0 数组(Array)
    Swift3.0 UICollectionView简单使用
    Swift3.0 字符串(string)
    Swift3.0 元组 (tuples)
    Swift3.0 UICollectionView 删除,拖动
    Swift3.0 控制流
    Swift3.0 UITextView写反馈界面
    HashMap JDK1.8实现原理
    Volatile的详解
    阻塞队列之LinkedBlockingQueue
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/13702945.html
Copyright © 2011-2022 走看看