Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mvc step1 주드 미션 제출합니다 #342

Merged
merged 9 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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.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 {

Expand All @@ -20,10 +26,28 @@ public AnnotationHandlerMapping(final Object... basePackage) {
}

public void initialize() {
Reflections reflections = new Reflections(basePackage);
Set<Class<?>> controllers = reflections.getTypesAnnotatedWith(Controller.class);
for (Class<?> controller : controllers) {
Method[] methods = controller.getMethods();
Arrays.stream(methods)
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.forEach(this::putHandlers);
Copy link

@rawfishthelgh rawfishthelgh Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A

stream의 forEach 연산은 가급적 출력용으로만 사용하는게 좋을 것 같다는 생각이 드는데, 어떻게 생각하시나요?
현재는 stream 외부 변수에 영향을 주는 로직이 forEach에 존재하고 있네요
참고

}
log.info("Initialized AnnotationHandlerMapping!");
}

private void putHandlers(Method method) {
RequestMapping annotation = method.getAnnotation(RequestMapping.class);
for (RequestMethod requestMethod : annotation.method()) {
final var handlerKey = new HandlerKey(annotation.value(), requestMethod);
handlerExecutions.put(handlerKey, new HandlerExecution(method));
}
}

public Object getHandler(final HttpServletRequest request) {
return null;
return handlerExecutions.get(
new HandlerKey(request.getRequestURI(), RequestMethod.valueOf(request.getMethod()))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

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

public class HandlerExecution {

private final Method method;

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

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