diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java index 85c29da5bc..2350a4040e 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java @@ -11,6 +11,7 @@ import java.util.Optional; import java.util.Set; import java.util.function.BinaryOperator; +import java.util.function.Function; import java.util.stream.Collectors; import org.reflections.Reflections; import org.slf4j.Logger; @@ -19,6 +20,7 @@ import java.util.HashMap; import java.util.Map; import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; import webmvc.org.springframework.web.servlet.ModelAndView; public class AnnotationHandlerMapping { @@ -64,6 +66,15 @@ private static List convertToString(Object[] basePackage) { .collect(Collectors.toList()); } + private Map extractHandler(String targetPackage) { + Reflections reflections = new Reflections(targetPackage); + return reflections.getTypesAnnotatedWith(Controller.class).stream() + .map(this::extractHandlerFromClass) + .reduce( + new HashMap<>(), + migrateHandler()); + } + private void checkDuplication(Map originHandlers, Map newHandlers) { Set duplicatedHandlerKeys = new HashSet<>(originHandlers.keySet()); @@ -75,15 +86,6 @@ private void checkDuplication(Map originHandlers, } } - private Map extractHandler(String targetPackage) { - Reflections reflections = new Reflections(targetPackage); - return reflections.getTypesAnnotatedWith(Controller.class).stream() - .map(this::extractHandlerFromClass) - .reduce( - new HashMap<>(), - migrateHandler()); - } - private Map extractHandlerFromClass(Class targetClass) { Object handler = makeClass(targetClass); return Arrays.stream(targetClass.getMethods()) @@ -95,24 +97,40 @@ private Map extractHandlerFromClass(Class targe ); } + private Object makeClass(Class targetClass) { + try { + return targetClass.getDeclaredConstructor().newInstance(); + } catch (NoSuchMethodException | + InvocationTargetException | + InstantiationException | + IllegalAccessException e) { + throw new IllegalArgumentException(e); + } + } + private Map extractHandlerFromMethod(Method method, Object handler) { - RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); - return Arrays.stream(requestMapping.method()) - .map(requestMethod -> { - Map extractedHandlerMapping = new HashMap<>(); - extractedHandlerMapping.put( - new HandlerKey(requestMapping.value(), requestMethod), - new HandlerExecutionImpl(handler, method) - ); - return extractedHandlerMapping; - }) + return Arrays.stream(method.getAnnotation(RequestMapping.class).method()) + .map(makeHandler(method, handler, method.getAnnotation(RequestMapping.class))) .reduce( new HashMap<>(), migrateHandler() ); } + private Function> makeHandler(Method method, + Object handler, + RequestMapping requestMapping) { + return requestMethod -> { + Map extractedHandlerMapping = new HashMap<>(); + extractedHandlerMapping.put( + new HandlerKey(requestMapping.value(), requestMethod), + new HandlerExecution(handler, method) + ); + return extractedHandlerMapping; + }; + } + private BinaryOperator> migrateHandler() { return (extractedHandler, extractingController) -> { checkDuplication(extractedHandler, extractingController); @@ -121,17 +139,6 @@ private BinaryOperator> migrateHandler() { }; } - private Object makeClass(Class targetClass) { - try { - return targetClass.getDeclaredConstructor().newInstance(); - } catch (NoSuchMethodException | - InvocationTargetException | - InstantiationException | - IllegalAccessException e) { - throw new IllegalArgumentException(e); - } - } - private boolean haveRequestMapping(Method method) { return Arrays.stream(method.getDeclaredAnnotations()) .anyMatch(RequestMapping.class::isInstance); @@ -141,26 +148,7 @@ public Object getHandler(final HttpServletRequest request) { Optional findHandler = handlerExecutions.keySet().stream() .filter(handlerKey -> handlerKey.canHandle(request)) .findAny(); - if (findHandler.isPresent()) { - return handlerExecutions.get(findHandler.get()); - } - return null; - } - - private class HandlerExecutionImpl extends HandlerExecution { - - private final Object handler; - private final Method handlerMethod; - - private HandlerExecutionImpl(Object handler, Method handlerMethod) { - this.handler = handler; - this.handlerMethod = handlerMethod; - } - - @Override - public ModelAndView handle(HttpServletRequest request, - HttpServletResponse response) throws Exception { - return (ModelAndView) handlerMethod.invoke(handler, request, response); - } + return findHandler.map(handlerExecutions::get) + .orElse(null); } }