Search in sources :

Example 1 with Mapping

use of com.duangframework.core.annotation.mvc.Mapping in project duangframework by tcrct.

the class RouteHelper method duang.

public static void duang() {
    Map<Class<?>, Object> controllerMap = BeanUtils.getAllBeanMaps().get(CONTROLLER_ENDWITH_NAME);
    if (ToolsKit.isEmpty(controllerMap)) {
        throw new EmptyNullException("mvc controller is null");
    }
    Set<String> excludedMethodName = ObjectKit.buildExcludedMethodName();
    Method[] baseControllerMethods = BaseController.class.getMethods();
    for (Method method : baseControllerMethods) {
        excludedMethodName.add(method.getName());
    }
    // 遍历所有Controller对象,取出Mapping注解,生成路由集合
    for (Iterator<Map.Entry<Class<?>, Object>> it = controllerMap.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<Class<?>, Object> entry = it.next();
        Class<?> controllerClass = entry.getKey();
        Mapping controllerMapping = controllerClass.getAnnotation(Mapping.class);
        // String controllerKey = ToolsKit.isEmpty(controllerMapping) ? buildMappingKey(controllerClass.getSimpleName()) : controllerMapping.value().toLowerCase();
        String controllerKey = buildMappingKey(controllerMapping, controllerClass.getSimpleName());
        for (Method method : controllerClass.getMethods()) {
            Mapping methodMapping = method.getAnnotation(Mapping.class);
            String methodName = method.getName();
            // Object类公用 方法名并且没有参数的方法
            if (!excludedMethodName.contains(methodName) && method.getParameterTypes().length == 0) {
                Action action = buildAction(controllerClass, controllerKey, methodMapping, method);
                String actionKey = action.getActionKey();
                if (actionKey.contains("{") && actionKey.contains("}")) {
                    action.setRestfulKey(actionKey);
                    restfulActionMapping.put(actionKey, action);
                }
                if (!ToolsKit.isExist(actionKey, actionMapping)) {
                    actionMapping.put(actionKey, action);
                }
            }
        }
    }
    List<String> keyList = getAllActionKeys();
    logger.warn("**************** Controller Mapper Key ****************");
    for (String key : keyList) {
        logger.warn(key);
    }
    if (!actionMapping.isEmpty()) {
        InstanceFactory.setActionMapping(actionMapping);
    }
    if (!restfulActionMapping.isEmpty()) {
        InstanceFactory.setRestfulActionMapping(restfulActionMapping);
    }
    logger.warn("RouteHelper Success...");
}
Also used : Action(com.duangframework.mvc.core.Action) Mapping(com.duangframework.core.annotation.mvc.Mapping) Method(java.lang.reflect.Method) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Aggregations

Mapping (com.duangframework.core.annotation.mvc.Mapping)1 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)1 Action (com.duangframework.mvc.core.Action)1 Method (java.lang.reflect.Method)1