程宇寒 2018-11-08 09:27 采纳率: 100%
浏览 4011
已采纳

java反射,自动调用方法,方法的返回值类型是不一定的,怎么得到方法的返回值类型的原来的类型!

java反射动态获取方法的返回值类型?
图片说明
图片说明
代码如下:

 package javase;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class Person {
    static List<String> list;

    public void eat() {
        System.out.println("我是eat()函数");
    }

    public String sayHello(String name) {
        return "hello,我是" + name + "!";
    }

    public String jump(String name, int age) {
        return "hello,我是" + name + ",我今年" + age + "岁了!";
    }

    public List<String> returnList() {
        // List<String> list = new ArrayList<String>();
        list = new ArrayList<String>();
        list.add("令狐冲");
        list.add("韦小宝");
        list.add("张无忌");
        list.add("杨过");
        list.add("乔峰");
        return list;
    }

    public static void main(String[] args) throws Exception {
        Class cla = Class.forName("javase.Person");
        Constructor constructor = cla.getConstructor();
        Object obj = constructor.newInstance();
        Method[] declaredMethods = cla.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method.getName() + " / " + method);
            Class[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length > 0) {
                System.out.print(method.getName() + "方法的参数类型:");
            } else {
                System.out.print(method.getName() + "方法没有参数");
            }
            for (Class parameterType : parameterTypes) {
                System.out.print(parameterType.getName() + "\t");
            }
            System.out.println("\n--------------------------------------------------");
        }
        System.out.println("******************************");
        // 使用反射,动态调用对象的函数
        Method method = cla.getMethod("eat");
        Object returnValue = method.invoke(obj);
        System.out.println(returnValue);
        // 动态调用对象的函数
        Method method2 = cla.getMethod("sayHello", String.class);
        Object returnValue2 = method2.invoke(obj, "令狐冲");
        System.out.println(returnValue2);
        // 动态调用对象的函数
        Method method3 = cla.getMethod("jump", String.class, int.class);
        Object returnValue3 = method3.invoke(obj, "令狐冲", 16);
        System.out.println(returnValue3);
        // 动态调用对象的函数
        Method method4 = cla.getMethod("returnList");
        Type returnType = method4.getGenericReturnType();
        System.out.println(method4.getName() + "方法的返回值类型=" + returnType);
        /*
         * returnList方法返回值类型是List<String>,所以returnValue4变量原本的类型是List<String>
         * 还有一个问题值得我们思考,每个方法的返回值类型都可能不一样,我们在利用反射调用完invoke()方法后,得到的返回值类
         * 型都是Object,怎么转换回每个方法它自己的原本的返回值类型呢?
         */
        Object returnValue4 = method4.invoke(obj);
        System.out.println("方法的返回值类型:" + method4.getReturnType().cast(method4.invoke(obj)));
        System.out.println(returnValue4 + " / " + returnValue4.getClass());
        for (String str : list) {
            System.out.println("利用反射自动调用完returnList方法后集合元素" + str);
        }
        List<String> returnValue5 = (List<String>) method4.invoke(obj);
        for (String string : returnValue5) {
            System.out.println(string);
        }

    }

}

一言以蔽之,我就是想做到如下的代码这样:

 Method method4 = cla.getMethod("returnList");
 //我想下面的Object变成动态数据类型(即returnList方法的返回值类型) 
 Object returnValue4 = method4.invoke(obj);
 //有没有可能变成下面这样
  method4.getReturnType() returnValue4 = (method4.getReturnType())method4.invoke(obj);
    //到时候就可以拿到每个方法的具体的返回值,方法返回Map,我就可以拿到Map,方法返回int,我就拿到int,方法返回String,我就拿到String,方法返回List<Student>,我就可以拿到List<Student>

运行结果截图:
图片说明
图片说明

  • 写回答

1条回答 默认 最新

  • hx_sunshine 2022-08-05 16:19
    关注

    同样遇到了这个问题 楼主解决了吗 求告知

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 3月21日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog