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 05c6c91c63..0462aa314c 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 @@ -6,12 +6,9 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashSet; -import java.util.List; 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; import org.slf4j.LoggerFactory; @@ -19,7 +16,6 @@ import java.util.HashMap; import java.util.Map; import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; public class AnnotationHandlerMapping { @@ -44,44 +40,14 @@ private void validateBasePackage(Object[] basePackage) { public void initialize() { log.info("Initialized AnnotationHandlerMapping!"); - handlerExecutions.putAll(findHandlersFrom(basePackage)); + handlerExecutions.putAll(extractHandler()); } - private Map findHandlersFrom(Object[] basePackage) { - List basePackages = convertToString(basePackage); - Map handlers = new HashMap<>(); - for (String targetPackage : basePackages) { - Map packageHandler = extractHandler(targetPackage); - checkDuplication(handlers, packageHandler); - handlers.putAll(packageHandler); - } - return handlers; - } - - private static List convertToString(Object[] basePackage) { - return Arrays.stream(basePackage) - .map(o -> (String) o) - .collect(Collectors.toList()); - } - - private Map extractHandler(String targetPackage) { - Reflections reflections = new Reflections(targetPackage); + private Map extractHandler() { + Reflections reflections = new Reflections(basePackage); 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()); - duplicatedHandlerKeys.retainAll(newHandlers.keySet()); - if (!duplicatedHandlerKeys.isEmpty()) { - HandlerKey duplicatedHandlerKey = duplicatedHandlerKeys.iterator().next(); - log.error("duplication handler : {}", duplicatedHandlerKey); - throw new IllegalArgumentException("Duplicated HandlerKey"); - } + .reduce(new HashMap<>(), migrateHandler()); } private Map extractHandlerFromClass(Class targetClass) { @@ -89,10 +55,7 @@ private Map extractHandlerFromClass(Class targe return Arrays.stream(targetClass.getMethods()) .filter(this::haveRequestMapping) .map(method -> extractHandlerFromMethod(method, handler)) - .reduce( - new HashMap<>(), - migrateHandler() - ); + .reduce(new HashMap<>(), migrateHandler()); } private Object makeClass(Class targetClass) { @@ -106,40 +69,42 @@ private Object makeClass(Class targetClass) { } } - private Map extractHandlerFromMethod(Method method, - Object handler) { - return Arrays.stream(method.getAnnotation(RequestMapping.class).method()) - .map(makeHandler(method, handler, method.getAnnotation(RequestMapping.class))) - .reduce( - new HashMap<>(), - migrateHandler() - ); + private boolean haveRequestMapping(Method method) { + return Arrays.stream(method.getDeclaredAnnotations()) + .anyMatch(RequestMapping.class::isInstance); } - 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 Map extractHandlerFromMethod(Method method, + Object handler) { + HandlerExecution handlerExecution = new HandlerExecution(handler, method); + RequestMapping annotation = method.getAnnotation(RequestMapping.class); + return Arrays.stream(annotation.method()) + .map(requestMethod -> { + Map extractedHandlerMapping = new HashMap<>(); + extractedHandlerMapping.put(new HandlerKey(annotation.value(), requestMethod), + handlerExecution); + return extractedHandlerMapping; + }) + .reduce(new HashMap<>(), migrateHandler()); } private BinaryOperator> migrateHandler() { - return (extractedHandler, extractingController) -> { - checkDuplication(extractedHandler, extractingController); - extractedHandler.putAll(extractingController); - return extractedHandler; + return (originHandler, migrateHandler) -> { + checkDuplication(originHandler, migrateHandler); + originHandler.putAll(migrateHandler); + return originHandler; }; } - private boolean haveRequestMapping(Method method) { - return Arrays.stream(method.getDeclaredAnnotations()) - .anyMatch(RequestMapping.class::isInstance); + private void checkDuplication(Map originHandlers, + Map newHandlers) { + Set duplicatedHandlerKeys = new HashSet<>(originHandlers.keySet()); + duplicatedHandlerKeys.retainAll(newHandlers.keySet()); + if (!duplicatedHandlerKeys.isEmpty()) { + HandlerKey duplicatedHandlerKey = duplicatedHandlerKeys.iterator().next(); + log.error("duplication handler : {}", duplicatedHandlerKey); + throw new IllegalArgumentException("Duplicated HandlerKey"); + } } public Object getHandler(final HttpServletRequest request) {