反射の実践テスト

3696 ワード

 /**
     * JSON    
     *
     * @param json                json 
     * @param jsonTransType json       model 
     * @param functionName    service     
     * @return          ResultRes
     */
    private  ResultRes activity(String functionName, String json, Class 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  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  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;
    }