From 5bdbf4a780a166cce7b5e11cd3714d3a7e087637 Mon Sep 17 00:00:00 2001 From: kiarakim Date: Tue, 12 Sep 2023 19:08:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=201=EB=8B=A8=EA=B3=84=20-=20@MVC=20?= =?UTF-8?q?=ED=94=84=EB=A0=88=EC=9E=84=EC=9B=8C=ED=81=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mvc/tobe/AnnotationHandlerMapping.java | 29 ++++++++++++++++++- .../servlet/mvc/tobe/HandlerExecution.java | 14 +++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) 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 a355218efa..7c277647d1 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 @@ -1,11 +1,17 @@ package webmvc.org.springframework.web.servlet.mvc.tobe; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; +import org.reflections.Reflections; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; +import java.util.Set; public class AnnotationHandlerMapping { @@ -21,9 +27,30 @@ public AnnotationHandlerMapping(final Object... basePackage) { public void initialize() { log.info("Initialized AnnotationHandlerMapping!"); + putHandlerExecutions(); + } + + private void putHandlerExecutions() { + Reflections reflections = new Reflections(basePackage); + Set> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Controller.class); + for (Class aClass : typesAnnotatedWith) { + for (Method method : aClass.getDeclaredMethods()) { + if (method.isAnnotationPresent(RequestMapping.class)) { + RequestMapping annotation = method.getAnnotation(RequestMapping.class); + String url = annotation.value(); + RequestMethod requestMethod = annotation.method()[0]; + HandlerKey handlerKey = new HandlerKey(url, requestMethod); + HandlerExecution handlerExecution = new HandlerExecution(method); + handlerExecutions.put(handlerKey, handlerExecution); + } + } + } } public Object getHandler(final HttpServletRequest request) { - return null; + String requestURI = request.getRequestURI(); + RequestMethod requestMethod = RequestMethod.valueOf(request.getMethod()); + HandlerKey handlerKey = new HandlerKey(requestURI, requestMethod); + return handlerExecutions.get(handlerKey); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java index 37c583fbdf..5d69ea22ea 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java @@ -4,9 +4,19 @@ import jakarta.servlet.http.HttpServletResponse; import webmvc.org.springframework.web.servlet.ModelAndView; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + public class HandlerExecution { - public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { - return null; + private final Method method; + + public HandlerExecution(Method method) { + this.method = method; + } + + public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) + throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + return (ModelAndView) method.invoke(method.getDeclaringClass().getDeclaredConstructor().newInstance(), request, response); } }