场景,获取动态的处理类, 然后调用对应的方法,动态参数

 @Test
    public void test() {
        String nameHandler = "personCardUpdateHandler";
        HpScPersonPO hpScPersonPO = new HpScPersonPO();
        String idCardNum = "123";
        commonHandler(nameHandler, new Object[] {null, hpScPersonPO, idCardNum});
    }

    public Map<String, Object> commonHandler(String nameHandler, Object[] args) {
        IDoorDeviceService doorDeviceService = new DoorUniubiFaceServiceImpl();
        try {
            HpScDevicePO hpScDevicePO = new HpScDevicePO();
            hpScDevicePO.setIpAddr("http://www.baidu.com");
            hpScDevicePO.setPassWord("abc123456");
            args[0] = hpScDevicePO;

            Class clazz = doorDeviceService.getClass();
            // 获取方法
            Class[] argsClass = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                argsClass[i] = args[i].getClass();
            }
            Method method = clazz.getMethod(nameHandler, argsClass);

            // 执行方法
            Object obj = clazz.getDeclaredConstructor().newInstance();
            DoorDeviceResponse doorDeviceResponse = (DoorDeviceResponse) method.invoke(obj, args);
            if (doorDeviceResponse.hasError()) {
                System.out.println("错误:" + doorDeviceResponse.getErrorMsg());
            }
            System.out.println("OK");
        }  catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        Map<String, Object> outData = new HashMap<>();
        return outData;
    }

 

posted on 2022-08-08 09:28  1161588342  阅读(579)  评论(0编辑  收藏  举报