Skip to content
New issue

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

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

Already on GitHub? # to your account

[MVC 구현하기 - 1단계] 에버(손채영) 미션 제출합니다. #694

Merged
merged 21 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
df6fa40
test: 학습테스트 Reflection
helenason Sep 20, 2024
cb172db
feat: 핸들러 매핑 로직 추가
helenason Sep 20, 2024
6e4c0e8
feat: 뷰 로직 JspView로 위임
helenason Sep 20, 2024
f0cda4b
refactor: 핸들러 매핑 reflection 사용하도록
helenason Sep 20, 2024
2ecca46
feat: RequestMapping method 선언하지 않은 경우 모든 HTTP method 지원
helenason Sep 20, 2024
e312664
test: 유효하지 않은 요청의 경우 예외 발생
helenason Sep 20, 2024
4e45a35
refactor: 핸들 로직 reflection 사용하도록
helenason Sep 20, 2024
e76390e
refactor: DispatcherServlet service 메서드 ModelAndView를 다루도록
helenason Sep 20, 2024
aa674d8
style: 불필요한 import문 제거
helenason Sep 20, 2024
d4d274b
refactor: HandlerKey 캐싱
helenason Sep 22, 2024
0118167
refactor: HandlerExecution 내에서 매번 인스턴스 생성하지 않도록
helenason Sep 22, 2024
ba17506
test: EnumSource 활용하여 파라미터 주입
helenason Sep 22, 2024
de7370e
test: getMethods vs getDeclaredMethods 학습테스트 작성
helenason Sep 22, 2024
613a0c1
style: 재정의 메서드 순서 및 구조 통일
helenason Sep 22, 2024
5b297ce
style: EOF
helenason Sep 22, 2024
54f3b36
test: 학습테스트 Servlet
helenason Sep 22, 2024
fdd3078
test: 학습테스트 Filter
helenason Sep 22, 2024
8ace1d8
test: 포함 여부 확인 로직 수정
helenason Sep 22, 2024
6afede4
style: 불필요한 주석 제거
helenason Sep 22, 2024
279fc92
test: 동등성 대신 동일성 테스트하도록 및 테스트를 위한 비즈니스 코드 제거
helenason Sep 25, 2024
21fcb37
refactor: HandlerKey 캐시 자료구조 변경
helenason Sep 25, 2024
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
2 changes: 1 addition & 1 deletion study/src/test/java/reflection/Junit3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void test2() throws Exception {
System.out.println("Running Test2");
}

public void three() throws Exception {
private void privateTest3() throws Exception {
System.out.println("Running Test3");
}
}
50 changes: 47 additions & 3 deletions study/src/test/java/reflection/Junit3TestRunner.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
package reflection;

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class Junit3TestRunner {

Choose a reason for hiding this comment

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

학습) Reflection이란 무엇이고, 어떻게 활용할 수 있나요?

Copy link
Member Author

Choose a reason for hiding this comment

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

Reflection은 런타임의 실행 중인 자바 프로그램이 자신을 조사하거나 프로그램의 내부 속성을 조작하는 API입니다.

Reflection은 추상화를 깨는 등의 단점을 가지고 있기 때문에, 애플리케이션 단보다는 보통 프레임워크나 라이브러리를 만들 때 활용되고, 런타임에 알아낼 수 있는 정보를 통해 컴파일 타임에 작업을 할 경우 사용하기 좋습니다!

저희가 흔히 사용하는 라이브러리 중 활용되는 예시로는, JUnit 라이브러리에서 @test 어노테이션이 붙은 메서드만 실행하는 작업, Spring이 자동으로 의존성을 주입하는 작업 등이 있습니다 :)


Class<Junit3Test> clazz = Junit3Test.class;

@Test
void run() throws Exception {
Class<Junit3Test> clazz = Junit3Test.class;

// TODO Junit3Test에서 test로 시작하는 메소드 실행
Junit3Test junit3Test = clazz.getConstructor().newInstance();
List<Method> testMethods = getDeclaredTestMethods(clazz);

Choose a reason for hiding this comment

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

학습) getDeclaredXX()와 getxx()의 차이는 무엇일까요?

Copy link
Member Author

@helenason helenason Sep 22, 2024

Choose a reason for hiding this comment

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

getDeclaredMethods는 상속 메서드를 제외하고 직접 선언한 메서드만을 불러옵니다. 접근 제어자에 대한 제한은 없습니다. 반면, getMethods는 상속 메서드를 포함한 모든 메서드를 반환하며, public 접근 제어자에 한해서만 반환됩니다.

관련하여 학습한 코드도 커밋해두었습니다! de7370e

assertThat(testMethods).hasSize(2);
for (Method method : testMethods) {
method.invoke(junit3Test);
}
}

private List<Method> getDeclaredTestMethods(Class<Junit3Test> clazz) {
Method[] methods = clazz.getDeclaredMethods();
Method[] methods = clazz.getMethods();
return Arrays.stream(methods)
.filter(method -> method.getName().startsWith("test"))
.toList();
}

@DisplayName("getMethods: 상속 메서드를 포함한 모든 public 메서드를 반환한다.")

Choose a reason for hiding this comment

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

👍👍

@Test
void getMethods() {
// given & when
Method[] methods = clazz.getMethods();

// then
List<String> actual = Arrays.stream(methods)
.map(Method::getName)
.toList();
assertThat(actual).containsExactly(
"test1",
"test2",
"equals",
"toString",
"hashCode",
"getClass",
"notify",
"notifyAll",
"wait",
"wait",
"wait");
}

@DisplayName("getDeclaredMethods: 상속 메서드를 제외하고 직접 선언된 모든 메서드를 반환한다.")
@Test

Choose a reason for hiding this comment

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

👍👍

void getDeclaredMethods() {
// given & when
Method[] methods = clazz.getDeclaredMethods();

// then
List<String> actual = Arrays.stream(methods)
.map(Method::getName)
.toList();
assertThat(actual).containsExactly(
"test1",
"test2",
"privateTest3");
}
}