java使用invoke反射调用成员函数时遇到的坑--> IllegalArgumentException: wrong number of arguments
发布日期:2021-10-07 06:13:10 浏览次数:1 分类:技术文章

本文共 2604 字,大约阅读时间需要 8 分钟。

参考博客:

问题背景:

使用invoke传递参数时,要考虑参数的个数:

public Object invoke(Object obj,            Object... args)/*Parameters:obj - the object the underlying method is invoked fromargs - the arguments used for the method callReturns:the result of dispatching the method represented by this object on obj with parameters args*/

以下两个成员函数,虽然一个是可变参数,一个是Object[]。但是在invoke函数中传参时当做一个Object来看待,如果想传递参数Object[] objects = {a, b};(见代码),需要加(Object)类型强制转换,

否则报错 java.lang.IllegalArgumentException: wrong number of arguments

public void addpp(Object... objects)public int addproxy(Object[] objects)

 以下的成员函数,参数个数为2个,两个int[],在invoke函数传参时可以传一个Object[2]不用加(Object)强制类型转换。

public int add(int[] a, int[] b)

 

代码概述:

有三个函数:

参数分别为可变参数、Object[]、两个int[],

主函数:

传递同样的两个数组,将两个数组转换为一个Object[],

使用invoke反射调用三种方法时,要对Object[]参数酌情加强制类型转换。

 

以下是测试代码:

package Ques;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Father {    public void addpp(Object... objects) {        System.out.println(objects.length);        //System.out.println("结果是 " + addproxy(objects));    }    public int add(int[] a, int[] b) {        return 0;    }    public int addproxy(Object[] objects) {        return add((int[]) objects[0], (int[]) objects[1]);    }    public static void main(String[] args) {        int[] a = {0, 1};        int[] b = {1, 2};        Object[] objects = {a, b};        Father son = new Father();        try {            Method m = Father.class.getDeclaredMethod("addpp", Object[].class);            Method m2 = Father.class.getDeclaredMethod("add", int[].class, int[].class);            Method m3 = Father.class.getDeclaredMethod("addproxy", Object[].class);            System.out.println("参数: m " + m.getParameterCount());//-->结果为1            System.out.println("参数: m2 " + m2.getParameterCount());//-->结果为2            System.out.println("参数: m3 " + m3.getParameterCount());//-->结果为1            m.invoke(son, (Object) objects);//输入的参数个数为1个            m2.invoke(son, objects);//输入的参数个数为2个            m3.invoke(son, (Object) objects);//输入的参数个数为1个        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        }        /*son.addpp(a,b);//-->结果为2        son.addpp(objects);//-->结果为2        son.addpp(a);//-->结果为1        son.addpp(new Object[2]);//-->结果为2        son.addpp(new int[2]);//-->结果为1        son.addpp(new String[2]);//-->结果为2        son.addpp((Object)objects);//-->结果为1        son.addpp((Object) new String[2]);//-->结果为1*/    }}

 

 

转载地址:https://blog.csdn.net/mengmengmengwang/article/details/108337627 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android开发笔记:Button的基本用法
下一篇:数据库锁——冒出的问题

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月05日 03时25分39秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

linux c : get curent tty info 2019-04-27
Leetcode 303. 区域和检索 - 数组不可变(DAY 25) ---- 动态规划学习期 2019-04-27
Leetcode 剑指 Offer 42. 连续子数组的最大和(DAY 25)---- 动态规划学习期 2019-04-27
Leetcode 121. 买卖股票的最佳时机(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 746. 使用最小花费爬楼梯(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 面试题 17.16. 按摩师(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 70. 爬楼梯(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 392. 判断子序列(DAY 26)---- 动态规划学习期 双百解法 2019-04-27
Leetcode 面试题 08.01. 三步问题(DAY 26) ---- 动态规划学习期 2019-04-27
Leetcode 877. 石子游戏(DAY 27) ---- 动态规划学习期 2019-04-27
Leetcode 714. 买卖股票的最佳时机含手续费(DAY 27) ---- 动态规划学习期 2019-04-27
Leetcode 96. 不同的二叉搜索树(DAY 28) ---- 动态规划学习期 (含题解) 2019-04-27
Leetcode 剑指 Offer 47. 礼物的最大价值(DAY 28) ---- 动态规划学习期 2019-04-27
Leetcode 120. 三角形最小路径和(DAY 28) ---- 动态规划学习期 2019-04-27
Leetcode 1227. 飞机座位分配概率(DAY 29) ---- 动态规划学习期 (成功留校) 2019-04-27
Leetcode 712. 两个字符串的最小ASCII删除和(DAY 30)---- 动态规划好难 学习期(懒狗复工) 2019-04-27
Leetcode 62. 不同路径(DAY 31) ---- 动态规划学习期 2019-04-27
Leetcode 983. 最低票价(DAY 31) ---- 动态规划学习期 2019-04-27
Python课后作业 2. 逆序排列(第三次作业) 2019-04-27
Android 自定义ViewGroup 实战篇 -> 实现FlowLayout 2019-04-27