Reflective practice test

 /**
     * JSON串操作流
     *
     * @param json          前端传过来的json串
     * @param jsonTransType json串解析后存放的model类
     * @param functionName  对应service层的方法名
     * @return 可以直接交付前端的ResultRes
     */
    private <T> ResultRes activity(String functionName, String json, Class<T> jsonTransType) {
        if (StringUtils.isEmpty(json)) {
            return this.fail(ResultCode.PARAM_ERROR, "参数错误");
        }
        try {
            System.out.println("进入流程");
            Method method = personService.getClass().getMethod(functionName, getParameterClass(personService, functionName));
            Object res;
            res = method.invoke(personService, JsonUtil.jsonToClass(json, jsonTransType));
            return this.ok(ResultCode.SUCCESS, "操作成功", JsonUtil.objectToJson(res));
        } catch (NoSuchMethodError noMethod) {
            return this.fail(ResultCode.FAIL, "业务异常");
        } catch (NullPointerException nullPoint) {
            return this.fail(ResultCode.FAIL, "数据查找失败");
        } catch (Exception unknownEx) {
            return this.fail(ResultCode.SYSTEM_ERROR, "未知错误");
        }
    }

    /**
     * 常规参数操作流
     *
     * @param functionName 对应service层的方法名
     * @param value        待交付service层的参数值
     * @return 可以直接交付前端的ResultRes
     */
    private <T> ResultRes activity(String functionName, Object value) {
        if (StringUtils.isEmpty(value.toString())) {
            return this.fail(ResultCode.PARAM_ERROR, "参数错误");
        }
        try {
            System.out.println("进入流程");
            Method method = personService.getClass().getMethod(functionName, getParameterClass(personService, functionName));
            Object res;
            res = method.invoke(personService, value);
            return this.ok(ResultCode.SUCCESS, "操作成功", JsonUtil.objectToJson(res));
        } catch (NoSuchMethodError noMethod) {
            return this.fail(ResultCode.FAIL, "业务异常");
        } catch (NullPointerException nullPoint) {
            return this.fail(ResultCode.FAIL, "数据查找失败");
        } catch (Exception unknownEx) {
            return this.fail(ResultCode.SYSTEM_ERROR, "未知错误");
        }
    }

    /**
     * 无参数操作流
     *
     * @param functionName 对应service层的方法名
     * @return 可以直接交付前端的ResultRes
     */
    private <T> ResultRes activity(String functionName) {
        try {
            Method method = personService.getClass().getMethod(functionName, getParameterClass(personService, functionName));
            Object res = method.invoke(personService);
            return this.ok(ResultCode.SUCCESS, "操作成功", res);
        } catch (NoSuchMethodError noMethod) {
            return this.fail(ResultCode.FAIL, "业务异常");
        } catch (NullPointerException nullPoint) {
            return this.fail(ResultCode.FAIL, "数据查找失败");
        } catch (Exception unknownEx) {
            return this.fail(ResultCode.SYSTEM_ERROR, "未知错误");
        }
    }

    /**
     * 获取所需要的方法类
     *
     * @param obj        类
     * @param methodName 所需要的方法名
     * @return 提供给getMethod使用的类
     */
    private Class<?>[] getParameterClass(Object obj, String methodName)
            throws IllegalArgumentException {
        Class<?> targetClass = obj.getClass();
        Method[] method = targetClass.getDeclaredMethods();
        for (Method m : method) {
            // 跳过非指定的方法
            if (!m.getName().equals(methodName)) {
                continue;
            }
            // 获取参数类型的数组 里面有参数的个数 和参数的类型

            return m.getParameterTypes();
        }
        return null;
    }

Guess you like

Origin www.cnblogs.com/ZoraZora59/p/11445674.html