Search in sources :

Example 1 with RequestParam

use of org.axe.annotation.mvc.RequestParam in project Axe by DongyuCai.

the class AxeRequestParamAnalyzeFilter method convertRequestParam2ActionParam.

public static void convertRequestParam2ActionParam(Method actionMethod, Param param, HttpServletRequest request, HttpServletResponse response) {
    Type[] parameterTypes = actionMethod.getGenericParameterTypes();
    Annotation[][] parameterAnnotations = actionMethod.getParameterAnnotations();
    parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes;
    //按顺序来,塞值
    List<Object> parameterValueList = new ArrayList<>();
    for (int i = 0; i < parameterTypes.length; i++) {
        Object parameterValue = null;
        do {
            Type parameterType = parameterTypes[i];
            Annotation[] parameterAnnotationAry = parameterAnnotations[i];
            RequestParam requestParam = null;
            for (Annotation anno : parameterAnnotationAry) {
                if (anno instanceof RequestParam) {
                    requestParam = (RequestParam) anno;
                    break;
                }
            }
            //## 是否@RequestParam标注的
            if (requestParam != null) {
                String fieldName = requestParam.value();
                //TODO:除了文件数组、单文件比较特殊需要转换,其他的都按照自动类型匹配,这样不够智能
                //而且,如果fieldMap和fileMap出现同名,则会导致参数混乱,不支持同名(虽然这种情况说明代码写的真操蛋!)
                parameterValue = RequestUtil.getRequestParam(param, fieldName, parameterType);
                break;
            } else {
                Class<?> parameterClass = null;
                if (parameterType instanceof Class) {
                    parameterClass = (Class<?>) parameterType;
                } else if (parameterType instanceof ParameterizedType) {
                    parameterClass = (Class<?>) ((ParameterizedType) parameterType).getRawType();
                }
                if (parameterClass != null) {
                    //* 如果是HttpServletRequest
                    if (ReflectionUtil.compareType(HttpServletRequest.class, parameterClass)) {
                        parameterValue = request;
                        break;
                    }
                    if (ReflectionUtil.compareType(HttpServletResponse.class, parameterClass)) {
                        parameterValue = response;
                        break;
                    }
                    //* 如果是Param
                    if (ReflectionUtil.compareType(Param.class, parameterClass)) {
                        parameterValue = param;
                        break;
                    }
                    //* 如果是Map<String,Object> 
                    if (ReflectionUtil.compareType(Map.class, parameterClass)) {
                        parameterValue = param.getBodyParamMap();
                        break;
                    }
                }
            }
        //## 其他杂七杂八类型,只能给null,框架不管
        } while (false);
        parameterValueList.add(parameterValue);
    }
    param.setActionParamList(parameterValueList);
}
Also used : RequestParam(org.axe.annotation.mvc.RequestParam) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 2 with RequestParam

use of org.axe.annotation.mvc.RequestParam in project Axe by DongyuCai.

the class RequestUtil method checkActionMethod.

/**
	 * 检查ActionMethod是否合规
	 * 不准包含基本类型
	 * 基本类型指: int、short、long、double、float、boolean、char等
	 */
public static void checkActionMethod(Method actionMethod) throws Exception {
    Type[] parameterTypeAry = actionMethod.getGenericParameterTypes();
    Annotation[][] parameterAnnotations = actionMethod.getParameterAnnotations();
    parameterTypeAry = parameterTypeAry == null ? new Type[0] : parameterTypeAry;
    for (int i = 0; i < parameterTypeAry.length; i++) {
        Type parameterType = parameterTypeAry[i];
        Annotation[] parameterAnnotationAry = parameterAnnotations[i];
        //所有的方法参数,都不准是语法糖小写,如int,为了默认值null
        if (parameterType instanceof Class && ((Class<?>) parameterType).isPrimitive()) {
            throw new Exception(actionMethod.toGenericString() + "#" + parameterType + "# is a primitive");
        }
        //如果标注了@RequestParam,需要检测是否是自定义Bean类型
        boolean isRequestParam = false;
        for (Annotation anno : parameterAnnotationAry) {
            if (anno instanceof RequestParam) {
                isRequestParam = true;
                break;
            }
        }
        if (isRequestParam) {
            if (parameterType instanceof ParameterizedType) {
                Type[] actualTypes = ((ParameterizedType) parameterType).getActualTypeArguments();
                for (Type actualType : actualTypes) {
                    if (!checkActionMethodParamType(actualType)) {
                        throw new Exception(actionMethod.toGenericString() + "#" + parameterType + "# is a customer pojo type");
                    }
                    //额外判断,不许是List
                    if (actualType instanceof Class && ReflectionUtil.compareType(List.class, (Class<?>) actualType)) {
                        throw new Exception(actionMethod.toGenericString() + "#" + parameterType + "# is List<List> type");
                    }
                }
            } else if (parameterType instanceof Class) {
                Class<?> parameterClassType = (Class<?>) parameterType;
                if (parameterClassType.isArray()) {
                    Class<?> componentType = parameterClassType.getComponentType();
                    if (!checkActionMethodParamType(componentType)) {
                        throw new Exception(actionMethod.toGenericString() + "#" + parameterClassType + "# is a customer pojo type");
                    }
                } else {
                    if (!checkActionMethodParamType(parameterClassType)) {
                        throw new Exception(actionMethod.toGenericString() + "#" + parameterClassType + "# is a customer pojo type");
                    }
                }
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RequestParam(org.axe.annotation.mvc.RequestParam) ArrayList(java.util.ArrayList) List(java.util.List) Annotation(java.lang.annotation.Annotation)

Example 3 with RequestParam

use of org.axe.annotation.mvc.RequestParam in project Axe by DongyuCai.

the class DispatcherServlet method convertRequest2RequestParam.

private List<Object> convertRequest2RequestParam(Method actionMethod, Param param, HttpServletRequest request, HttpServletResponse response) {
    Type[] parameterTypes = actionMethod.getGenericParameterTypes();
    Annotation[][] parameterAnnotations = actionMethod.getParameterAnnotations();
    parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes;
    //按顺序来,塞值
    List<Object> parameterValueList = new ArrayList<>();
    for (int i = 0; i < parameterTypes.length; i++) {
        Object parameterValue = null;
        do {
            Type parameterType = parameterTypes[i];
            Annotation[] parameterAnnotationAry = parameterAnnotations[i];
            RequestParam requestParam = null;
            for (Annotation anno : parameterAnnotationAry) {
                if (anno instanceof RequestParam) {
                    requestParam = (RequestParam) anno;
                    break;
                }
            }
            //## 是否@RequestParam标注的
            if (requestParam != null) {
                String fieldName = requestParam.value();
                //TODO:除了文件数组、单文件比较特殊需要转换,其他的都按照自动类型匹配,这样不够智能
                //而且,如果fieldMap和fileMap出现同名,则会导致参数混乱,不支持同名(虽然这种情况说明代码写的真操蛋!)
                parameterValue = RequestUtil.getRequestParam(param, fieldName, parameterType);
                break;
            } else {
                Class<?> parameterClass = null;
                if (parameterType instanceof Class) {
                    parameterClass = (Class<?>) parameterType;
                } else if (parameterType instanceof ParameterizedType) {
                    parameterClass = (Class<?>) ((ParameterizedType) parameterType).getRawType();
                }
                if (parameterClass != null) {
                    //* 如果是HttpServletRequest
                    if (ReflectionUtil.compareType(HttpServletRequest.class, parameterClass)) {
                        parameterValue = request;
                        break;
                    }
                    if (ReflectionUtil.compareType(HttpServletResponse.class, parameterClass)) {
                        parameterValue = response;
                        break;
                    }
                    //* 如果是Param
                    if (ReflectionUtil.compareType(Param.class, parameterClass)) {
                        parameterValue = param;
                        break;
                    }
                    //* 如果是Map<String,Object> 
                    if (ReflectionUtil.compareType(Map.class, parameterClass)) {
                        parameterValue = param.getBodyParamMap();
                        break;
                    }
                }
            }
        //## 其他杂七杂八类型,只能给null,框架不管
        } while (false);
        parameterValueList.add(parameterValue);
    }
    return parameterValueList;
}
Also used : RequestParam(org.axe.annotation.mvc.RequestParam) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ContentType(org.axe.constant.ContentType)

Aggregations

Annotation (java.lang.annotation.Annotation)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 RequestParam (org.axe.annotation.mvc.RequestParam)3 WildcardType (java.lang.reflect.WildcardType)1 List (java.util.List)1 ContentType (org.axe.constant.ContentType)1