-
Notifications
You must be signed in to change notification settings - Fork 305
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 구현하기 - 1단계] 마코(이규성) 미션 제출합니다. #345
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
.../webmvc/org/springframework/web/servlet/mvc/exception/CanNotInstanceHandlerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.exception; | ||
|
||
public class CanNotInstanceHandlerException extends RuntimeException { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
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 jakarta.servlet.http.HttpServletResponse; | ||
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 java.util.stream.Collectors; | ||
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 webmvc.org.springframework.web.servlet.mvc.exception.CanNotInstanceHandlerException; | ||
|
||
public class AnnotationHandlerMapping { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); | ||
private static final Set<Class<?>> supportParameters = Set.of(HttpServletRequest.class, | ||
HttpServletResponse.class); | ||
|
||
private final Object[] basePackage; | ||
private final Map<HandlerKey, HandlerExecution> handlerExecutions; | ||
|
@@ -20,10 +32,53 @@ public AnnotationHandlerMapping(final Object... basePackage) { | |
} | ||
|
||
public void initialize() { | ||
final var reflections = new Reflections(basePackage); | ||
final var classes = reflections.getTypesAnnotatedWith(Controller.class); | ||
for (final Class<?> clazz : classes) { | ||
final var handler = getHandlerInstance(clazz); | ||
final var methods = clazz.getMethods(); | ||
|
||
Arrays.stream(methods) | ||
.filter(this::supportParameters) | ||
.filter(method -> method.isAnnotationPresent(RequestMapping.class)) | ||
.forEach(method -> putHandlerExecutions(handler, method)); | ||
} | ||
log.info("Initialized AnnotationHandlerMapping!"); | ||
} | ||
|
||
private boolean supportParameters(final Method method) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 좀 hip하네요 👍 |
||
final var parameterTypes = Arrays.stream(method.getParameterTypes()) | ||
.collect(Collectors.toList()); | ||
|
||
return supportParameters.containsAll(parameterTypes) && parameterTypes.size() == 2; | ||
} | ||
|
||
private void putHandlerExecutions(final Object handler, final Method method) { | ||
final var annotation = method.getDeclaredAnnotation(RequestMapping.class); | ||
final var handlerExecution = new HandlerExecution(handler, method); | ||
|
||
Arrays.stream(annotation.method()) | ||
.map(requestMethod -> new HandlerKey(annotation.value(), requestMethod)) | ||
.forEach(handlerKey -> handlerExecutions.put(handlerKey, handlerExecution)); | ||
} | ||
|
||
private static Object getHandlerInstance(final Class<?> clazz) { | ||
try { | ||
return clazz.getConstructor().newInstance(); | ||
} catch (NoSuchMethodException | | ||
IllegalAccessException | | ||
InstantiationException | | ||
InvocationTargetException e | ||
) { | ||
throw new CanNotInstanceHandlerException(); | ||
} | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
return null; | ||
String requestURI = request.getRequestURI(); | ||
RequestMethod requestMethod = RequestMethod.valueOf(request.getMethod()); | ||
HandlerKey handlerKey = new HandlerKey(requestURI, requestMethod); | ||
|
||
return handlerExecutions.get(handlerKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
package reflection; | ||
|
||
import com.sun.nio.sctp.PeerAddressChangeNotification.AddressChangeEvent; | ||
import java.lang.reflect.Method; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class Junit3TestRunner { | ||
|
||
@Test | ||
void run() throws Exception { | ||
Class<Junit3Test> clazz = Junit3Test.class; | ||
|
||
final var junit3 = (Junit3Test) clazz.getConstructor().newInstance(); | ||
|
||
// TODO Junit3Test에서 test로 시작하는 메소드 실행 | ||
for (final Method method : clazz.getDeclaredMethods()) { | ||
if (method.getName().startsWith("test")) { | ||
method.invoke(junit3); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://tecoble.techcourse.co.kr/post/2020-05-14-foreach-vs-forloop/
foreach에 로직이 들어가면 안된다는데 어떻게 생각하시나요
근데 저도 foreach로 map에 넣는 작업을 수행하긴 했습니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋은 아티클 공유 감사합니다👍
for-loop보다 짧은 코드로 해결하려고 애써 외면하고 이 정도는 괜찮겠지 하고 타협했던 문제인데 충분히 로직을 빼고 스트림으로 해결할 수 있을 것 같네요. 2단계에서 리팩토링 해보겠습니다~