Skip to content

Commit

Permalink
feat: AnnotationHandlerMapping 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
parkmuhyeun committed Sep 12, 2023
1 parent aa69049 commit ae45f91
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
---

## 1. @MVC 프레임워크 구현하기
- [ ] AnnotationHandlerMapping 구현
- [x] AnnotationHandlerMapping 구현
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
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.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AnnotationHandlerMapping {

Expand All @@ -21,9 +30,43 @@ public AnnotationHandlerMapping(final Object... basePackage) {

public void initialize() {
log.info("Initialized AnnotationHandlerMapping!");
Reflections reflections = new Reflections(basePackage);
Set<Class<?>> controllers = reflections.getTypesAnnotatedWith(Controller.class);
List<Method> requestMappingMethods = mapRequestMappingMethods(controllers);

putHandlerExecutions(requestMappingMethods);
}

private List<Method> mapRequestMappingMethods(Set<Class<?>> controllers) {
return controllers.stream()
.map(Class::getDeclaredMethods).flatMap(Stream::of)
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.collect(Collectors.toList());
}

private void putHandlerExecutions(List<Method> requestMappingMethods) {
requestMappingMethods.forEach(method -> {
Object controller = createControllerInstance(method);
RequestMapping annotation = method.getAnnotation(RequestMapping.class);
String path = annotation.value();
RequestMethod[] requestMethods = annotation.method();
RequestMethod requestMethod = RequestMethod.valueOf(requestMethods[0].name());
handlerExecutions.put(new HandlerKey(path, requestMethod), new HandlerExecution(controller, method));
});
}

private Object createControllerInstance(Method method) {
try {
return method.getDeclaringClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}

public Object getHandler(final HttpServletRequest request) {
return null;
RequestMethod requestMethod = RequestMethod.valueOf(request.getMethod());
HandlerKey handlerKey = new HandlerKey(request.getRequestURI(), 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.Method;

public class HandlerExecution {

private final Object controller;
private final Method method;

public HandlerExecution(Object controller, Method method) {
this.controller = controller;
this.method = method;
}

public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return null;
return (ModelAndView) method.invoke(controller, request, response);
}
}

0 comments on commit ae45f91

Please sign in to comment.