Skip to content

Commit

Permalink
feat : AnnotationHandlerMapping 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
java-saeng committed Sep 13, 2023
1 parent 815f2dd commit e7abad4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,66 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
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;

public class AnnotationHandlerMapping {

private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class);
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class);

private final Object[] basePackage;
private final Map<HandlerKey, HandlerExecution> handlerExecutions;
private final Object[] basePackages;
private final Map<HandlerKey, HandlerExecution> handlerExecutions;

public AnnotationHandlerMapping(final Object... basePackage) {
this.basePackage = basePackage;
this.handlerExecutions = new HashMap<>();
}
public AnnotationHandlerMapping(final Object... basePackages) {
this.basePackages = basePackages;
this.handlerExecutions = new HashMap<>();
}

public void initialize()
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
log.info("Initialized AnnotationHandlerMapping!");

final Reflections reflections = new Reflections(basePackages);

public void initialize() {
log.info("Initialized AnnotationHandlerMapping!");
final Set<Class<?>> controllerAnnotationClasses =
reflections.getTypesAnnotatedWith(Controller.class);

for (Class<?> controllerAnnotationClass : controllerAnnotationClasses) {
final Object instance = controllerAnnotationClass.getDeclaredConstructor().newInstance();
final Method[] methods = controllerAnnotationClass.getDeclaredMethods();

Arrays.stream(methods)
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.forEach(method -> putHandlerExecutions(method, instance));
}
}

private void putHandlerExecutions(final Method method, final Object target) {
final RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);

for (final RequestMethod requestMethod : requestMapping.method()) {
final HandlerKey handlerKey = new HandlerKey(requestMapping.value(), requestMethod);

public Object getHandler(final HttpServletRequest request) {
return null;
handlerExecutions.put(handlerKey, new HandlerExecution(method, target));
}
}

public Object getHandler(final HttpServletRequest request) {
final String requestURI = request.getRequestURI();
final String method = request.getMethod();

final HandlerKey handlerKey = new HandlerKey(requestURI, RequestMethod.find(method));

return handlerExecutions.get(handlerKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import webmvc.org.springframework.web.servlet.ModelAndView;

public class HandlerExecution {

public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return null;
}
private final Method method;
private final Object target;

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

public ModelAndView handle(
final HttpServletRequest request,
final HttpServletResponse response
) throws InvocationTargetException, IllegalAccessException {
return (ModelAndView) method.invoke(target, request, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -14,7 +15,8 @@ class AnnotationHandlerMappingTest {
private AnnotationHandlerMapping handlerMapping;

@BeforeEach
void setUp() {
void setUp()
throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
handlerMapping = new AnnotationHandlerMapping("samples");
handlerMapping.initialize();
}
Expand Down

0 comments on commit e7abad4

Please sign in to comment.