反射调用参数为数组的方法2019-02-152019-02-15

反射类

    public static Boolean callMethod(String json,Map map) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        String id_card = "蛇精病";
        String pop_other = "小妖精";
        String[] s = new String[2];
        s[0]=id_card;
        s[1]=pop_other;
        //根据全类名获取一个class对象
        Class<?> clz = Class.forName("com.gsww.dgms.rule.batch.utils.CallMethodUtil");
        //获取类对象
        Constructor<?> declaredConstructor = clz.getDeclaredConstructor();
        //创建此class所表示的类的实例对象
        Object obj = declaredConstructor.newInstance();
        // 获取一个形式参数为String[]的方法
        Method method = obj.getClass().getDeclaredMethod(methodName,new Class[]{String[].class});
        //开启强制访问
        method.setAccessible(true);
        // 调用方法
        return (Boolean) method.invoke(obj, (Object) s);
    }

目标类

public class CallMethodUtil {
private void show(String[] strings){
        System.out.println("执行show方法完毕,ok"+strings[0]+"---"+strings[1]);
    }
}

执行结果

图片.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容