Skip to content

Commit

Permalink
feat: MVC 프레임워크 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
donghae-kim committed Sep 13, 2023
1 parent c65ac58 commit ade899f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class AnnotationHandlerMapping {

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

public void initialize() {
log.info("Initialized AnnotationHandlerMapping!");
final Reflections reflections = new Reflections(basePackage);

final Set<Class<?>> controllerClasses = reflections.getTypesAnnotatedWith(Controller.class);
controllerClasses.forEach(this::makeHandlerExecutions);
}

private void makeHandlerExecutions(final Class<?> controller) {
final Set<Method> requestMethods = getRequestMethod(controller);
final Object instance = toInstance(controller);

for (Method method : requestMethods) {
final RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
final List<HandlerKey> handlerKeys = getHandlerKeys(requestMapping);
final HandlerExecution handlerExecution = getHandlerExecution(instance, method);

addHandlerExecution(handlerKeys, handlerExecution);
}
}

private Set<Method> getRequestMethod(final Class<?> controller) {
return Arrays.stream(controller.getDeclaredMethods())
.filter(method -> !method.isSynthetic())
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.collect(Collectors.toSet());
}

private Object toInstance(Class<?> controller) {
try {
return controller.getConstructor().newInstance();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}

private List<HandlerKey> getHandlerKeys(final RequestMapping requestMapping) {
final String url = requestMapping.value();
final RequestMethod[] methods = requestMapping.method();

return Arrays.stream(methods)
.map(requestMethod -> new HandlerKey(url, requestMethod))
.collect(Collectors.toList());
}

private HandlerExecution getHandlerExecution(final Object instance, final Method method) {
return new HandlerExecution(instance, method);
}

private void addHandlerExecution(final List<HandlerKey> handlerKeys, final HandlerExecution handlerExecution) {
for (HandlerKey handlerKey : handlerKeys) {
handlerExecutions.put(handlerKey, handlerExecution);
}
}

public Object getHandler(final HttpServletRequest request) {
return null;
final String requestURI = request.getRequestURI();
final RequestMethod method = RequestMethod.valueOf(request.getMethod());
final HandlerKey handlerKey = new HandlerKey(requestURI, method);

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 Object instance;
private Method method;

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

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

0 comments on commit ade899f

Please sign in to comment.