-
Notifications
You must be signed in to change notification settings - Fork 382
[MVC 구현하기 - 1단계] 새양(양경호) 미션 제출합니다. #689
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
Merged
+443
−52
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d7388ee
test: do basic reflection tests junit 3 and 4
geoje 5802ba7
test: do ReflectionTest
geoje 3575a4c
test: solve logging all mvc classes by reflections
geoje 98bb518
feat: inject handler execution with reflection
geoje aaf05c8
feat: validate handler key duplication
geoje 1e41df4
test: validate duplication for handler key
geoje 430a6ec
test: rename for specifying
geoje 1a980a2
refactor: extract method for readability
geoje c435c7b
feat: fit for dynamic arguments
geoje 0649d47
test: join paths of controller and method
geoje 6979728
test: pass argument with required parameters
geoje 827a98b
refactor: extract deciding argument method
geoje d029c34
test: extract base package path
geoje ba909d7
test: check invalidation
geoje 0f72608
feat: implement JspView by DispatcherServlet.service
geoje bf3c880
test: validate private constructor
geoje 23c02be
test: fix meaningless method to clarify
geoje 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
This file contains hidden or 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
37 changes: 35 additions & 2 deletions
37
mvc/src/main/java/com/interface21/webmvc/servlet/mvc/tobe/HandlerExecution.java
This file contains hidden or 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,12 +1,45 @@ | ||
package com.interface21.webmvc.servlet.mvc.tobe; | ||
|
||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
public class HandlerExecution { | ||
|
||
private final Method method; | ||
private final Object controllerInstance; | ||
|
||
public HandlerExecution(Method method, Class<?> controllerClass) | ||
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | ||
this.method = method; | ||
this.controllerInstance = controllerClass.getConstructor().newInstance(); | ||
} | ||
|
||
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
return null; | ||
List<Object> preparedArgs = Stream.of(request, response) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
List<? extends Class<?>> requiredParameterClasses = Stream.of(method.getParameters()) | ||
.map(Parameter::getType) | ||
.toList(); | ||
|
||
List<Object> passingArgs = requiredParameterClasses.stream() | ||
.map(param -> decideArgumentByParameter(param, preparedArgs)) | ||
.toList(); | ||
|
||
return (ModelAndView) method.invoke(controllerInstance, passingArgs.toArray()); | ||
} | ||
|
||
private Object decideArgumentByParameter(Class<?> param, List<Object> preparedArgs) { | ||
return preparedArgs.stream() | ||
.filter(arg -> param.isAssignableFrom(arg.getClass())) | ||
.findAny() | ||
.orElse(null); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
23 changes: 23 additions & 0 deletions
23
mvc/src/test/java/com/interface21/webmvc/servlet/mvc/tobe/HandlerExecutionTest.java
This file contains hidden or 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,23 @@ | ||
package com.interface21.webmvc.servlet.mvc.tobe; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
|
||
import java.lang.reflect.Method; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import samples.NoArgsController; | ||
|
||
class HandlerExecutionTest { | ||
|
||
@Test | ||
@DisplayName("컨트롤러의 메서드가 요구하는 파라미터를 동적으로 대응하여 의존성을 주입한다.") | ||
void passArgumentsWithRequiredParameters() throws Exception { | ||
// given | ||
Method method = NoArgsController.class.getMethod("noArgsMethod"); | ||
HandlerExecution handlerExecution = new HandlerExecution(method, NoArgsController.class); | ||
|
||
// when & then | ||
assertThatCode(() -> handlerExecution.handle(null, null)) | ||
.doesNotThrowAnyException(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
mvc/src/test/java/com/interface21/webmvc/servlet/mvc/tobe/HandlerKeyTest.java
This file contains hidden or 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,21 @@ | ||
package com.interface21.webmvc.servlet.mvc.tobe; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class HandlerKeyTest { | ||
|
||
@Test | ||
@DisplayName("동등성에 대해 검증한다.") | ||
void methodName() { | ||
// given | ||
HandlerKey key1 = new HandlerKey("/api/test", RequestMethod.GET); | ||
HandlerKey key2 = new HandlerKey("/api/test", RequestMethod.GET); | ||
|
||
// when & then | ||
assertThat(key1).isEqualTo(key2); | ||
} | ||
} |
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.
HandlerExecution
이 reflection으로 컨트롤러 인스턴스를 만드는 책임을 가지는게 적절할까요? 새양의 의견이 궁금합니다 👀Uh oh!
There was an error while loading. Please reload this page.
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.
적절하지 않습니다! 컴포넌트 스캐닝을 하여
@Controller
어노테이션을 붙은 녀석들을 인스턴스로 생성하여 어떠한 공간속에 저장해둔 뒤 이HandlerExecution
은 단순히request
와response
등으로 핸들러를 실행시켜주어야 한다고 생각합니다!하지만 이 미션은 스프링을 구현하는게 아니다 보니 그런 객체 관리까지 신경쓰지 않았습니다. 😅