Skip to content

Commit

Permalink
refactor: 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
kiarakim committed Sep 13, 2023
1 parent 5bdbf4a commit 732a70f
Showing 1 changed file with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import web.org.springframework.web.bind.annotation.RequestMethod;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class AnnotationHandlerMapping {

Expand All @@ -32,19 +32,27 @@ public void initialize() {

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);
}
}
}
reflections.getTypesAnnotatedWith(Controller.class)
.forEach(this::processControllerClass);
}

private void processControllerClass(Class<?> aClass) {
Arrays.stream(aClass.getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.forEach(this::processControllerMethod);
}

private void processControllerMethod(Method method) {
HandlerKey handlerKey = makeHandlerKey(method);
HandlerExecution handlerExecution = new HandlerExecution(method);
handlerExecutions.put(handlerKey, handlerExecution);
}

private static HandlerKey makeHandlerKey(Method method) {
RequestMapping annotation = method.getAnnotation(RequestMapping.class);
String url = annotation.value();
RequestMethod requestMethod = annotation.method()[0];
return new HandlerKey(url, requestMethod);
}

public Object getHandler(final HttpServletRequest request) {
Expand Down

0 comments on commit 732a70f

Please sign in to comment.