Skip to content

Commit

Permalink
feat: 1단계 - @MVC 프레임워크 구현하기
Browse files Browse the repository at this point in the history
  • Loading branch information
kiarakim committed Sep 12, 2023
1 parent 896d7eb commit 5bdbf4a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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<Class<?>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5bdbf4a

Please sign in to comment.