Skip to content

Commit

Permalink
refactor: AnnotationHandlerMapping 메서드 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
BGuga committed Sep 13, 2023
1 parent d9ef3a5 commit 9bc9b97
Showing 1 changed file with 39 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.reflections.Reflections;
import org.slf4j.Logger;
Expand All @@ -19,6 +20,7 @@
import java.util.HashMap;
import java.util.Map;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.ModelAndView;

public class AnnotationHandlerMapping {
Expand Down Expand Up @@ -64,6 +66,15 @@ private static List<String> convertToString(Object[] basePackage) {
.collect(Collectors.toList());
}

private Map<HandlerKey, HandlerExecution> extractHandler(String targetPackage) {
Reflections reflections = new Reflections(targetPackage);
return reflections.getTypesAnnotatedWith(Controller.class).stream()
.map(this::extractHandlerFromClass)
.reduce(
new HashMap<>(),
migrateHandler());
}

private void checkDuplication(Map<HandlerKey, HandlerExecution> originHandlers,
Map<HandlerKey, HandlerExecution> newHandlers) {
Set<HandlerKey> duplicatedHandlerKeys = new HashSet<>(originHandlers.keySet());
Expand All @@ -75,15 +86,6 @@ private void checkDuplication(Map<HandlerKey, HandlerExecution> originHandlers,
}
}

private Map<HandlerKey, HandlerExecution> extractHandler(String targetPackage) {
Reflections reflections = new Reflections(targetPackage);
return reflections.getTypesAnnotatedWith(Controller.class).stream()
.map(this::extractHandlerFromClass)
.reduce(
new HashMap<>(),
migrateHandler());
}

private Map<HandlerKey, HandlerExecution> extractHandlerFromClass(Class<?> targetClass) {
Object handler = makeClass(targetClass);
return Arrays.stream(targetClass.getMethods())
Expand All @@ -95,24 +97,40 @@ private Map<HandlerKey, HandlerExecution> extractHandlerFromClass(Class<?> targe
);
}

private Object makeClass(Class<?> targetClass) {
try {
return targetClass.getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException |
InvocationTargetException |
InstantiationException |
IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}

private Map<HandlerKey, HandlerExecution> extractHandlerFromMethod(Method method,
Object handler) {
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
return Arrays.stream(requestMapping.method())
.map(requestMethod -> {
Map<HandlerKey, HandlerExecution> extractedHandlerMapping = new HashMap<>();
extractedHandlerMapping.put(
new HandlerKey(requestMapping.value(), requestMethod),
new HandlerExecutionImpl(handler, method)
);
return extractedHandlerMapping;
})
return Arrays.stream(method.getAnnotation(RequestMapping.class).method())
.map(makeHandler(method, handler, method.getAnnotation(RequestMapping.class)))
.reduce(
new HashMap<>(),
migrateHandler()
);
}

private Function<RequestMethod, Map<HandlerKey, HandlerExecution>> makeHandler(Method method,
Object handler,
RequestMapping requestMapping) {
return requestMethod -> {
Map<HandlerKey, HandlerExecution> extractedHandlerMapping = new HashMap<>();
extractedHandlerMapping.put(
new HandlerKey(requestMapping.value(), requestMethod),
new HandlerExecution(handler, method)
);
return extractedHandlerMapping;
};
}

private BinaryOperator<Map<HandlerKey, HandlerExecution>> migrateHandler() {
return (extractedHandler, extractingController) -> {
checkDuplication(extractedHandler, extractingController);
Expand All @@ -121,17 +139,6 @@ private BinaryOperator<Map<HandlerKey, HandlerExecution>> migrateHandler() {
};
}

private Object makeClass(Class<?> targetClass) {
try {
return targetClass.getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException |
InvocationTargetException |
InstantiationException |
IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}

private boolean haveRequestMapping(Method method) {
return Arrays.stream(method.getDeclaredAnnotations())
.anyMatch(RequestMapping.class::isInstance);
Expand All @@ -141,26 +148,7 @@ public Object getHandler(final HttpServletRequest request) {
Optional<HandlerKey> findHandler = handlerExecutions.keySet().stream()
.filter(handlerKey -> handlerKey.canHandle(request))
.findAny();
if (findHandler.isPresent()) {
return handlerExecutions.get(findHandler.get());
}
return null;
}

private class HandlerExecutionImpl extends HandlerExecution {

private final Object handler;
private final Method handlerMethod;

private HandlerExecutionImpl(Object handler, Method handlerMethod) {
this.handler = handler;
this.handlerMethod = handlerMethod;
}

@Override
public ModelAndView handle(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return (ModelAndView) handlerMethod.invoke(handler, request, response);
}
return findHandler.map(handlerExecutions::get)
.orElse(null);
}
}

0 comments on commit 9bc9b97

Please sign in to comment.