-
Notifications
You must be signed in to change notification settings - Fork 0
이영민 3주차 WAS
이번주에는 리플렉션이랑 JVM 을 배웠습니다.
리플렉션에서 어노테이션을 가져와서 클래스 혹은 메서드를 체크하고 실행시키는 것을 배워서 이번주 구현에 적용할 수 있겠다 싶었습니다.
public Map<HttpEndPoint, Function<Void, RenderData>> getHandlers(HttpHandleType type) {
Map<HttpEndPoint, Function<Void, RenderData>> handlers = new HashMap<>();
try {
Class<?>[] classes = componentScanner.getClasses(packageName);
Arrays.stream(classes)
.filter(handlerClass -> handlerClass.isAnnotationPresent(HttpHandler.class))
.forEach(handlerClass -> registerHandlerMethods(handlers, handlerClass, type));
} catch (Exception e) {
throw new RuntimeException("패키지에서 핸들러 클래스를 로드하지 못했습니다: " + packageName, e);
}
return handlers;
}
private void registerHandlerMethods(Map<HttpEndPoint, Function<Void, RenderData>> handlers,
Class<?> handlerClass, HttpHandleType type) {
try {
Object handlerInstance = dependencyFactory.createHandlerInstance(handlerClass);
Arrays.stream(handlerClass.getMethods())
.filter(method -> method.isAnnotationPresent(HttpFunction.class))
.forEach(method -> {
HttpFunction httpFunction = method.getAnnotation(HttpFunction.class);
if (httpFunction.type() == type) {
HttpEndPoint endPoint = new HttpEndPoint(httpFunction.path(), httpFunction.method());
Function<Void, RenderData> handlerFunction = createHandlerFunction(handlerInstance, method);
handlers.put(endPoint, handlerFunction);
}
});
} catch (Exception e) {
throw new RuntimeException("클래스에 대한 핸들러 메서드를 등록하지 못했습니다: " + handlerClass.getName(), e);
}
}
제가 구현한 HttpHandlerRegistry 의 일부 메서드입니다.
getClasses 로 가져온 클래스 리스트를 순회하면서 클래스에 HttpHandler 어노테이션이 있는지 체크합니다.
또한 해당 클래스 내부 메서드를 순회하며 HttpFunction 어노테이션이 있는지 체크합니다.
HttpFunction 의 value 로 HttpEndPoint 를 생성하고 최종적으로 Function 을 생성하여 handlers 에 put 하는 게 주요 골자입니다.
JVM은 크게 클래스 로더, 런타임 데이터 영역, 실행 엔진으로 나눌 수 있습니다.
-
클래스 로더(Class Loader): 자바 클래스 파일을 JVM으로 동적으로 로드하는 역할을 합니다. 클래스 로더는 클래스 파일을 로드하고, 링크(Link) 과정을 거쳐 JVM 내에서 사용할 수 있도록 준비합니다.
-
런타임 데이터 영역(Runtime Data Area): 자바 프로그램을 실행하기 위해 JVM이 할당하는 메모리 영역입니다. 이 영역은 여러 하위 영역으로 나뉘며, 각 영역은 프로그램 실행 중에 다른 종류의 데이터를 저장합니다. 주요 영역으로는 메소드 영역(Method Area), 힙 영역(Heap Area), 스택 영역(Stack Area), PC 레지스터(Program Counter Register), 네이티브 메소드 스택(Native Method Stack)이 있습니다.
-
실행 엔진(Execution Engine): 실제로 바이트 코드를 실행하여 프로그램을 동작하게 하는 역할을 합니다. 실행 엔진은 인터프리터(Interpreter), JIT 컴파일러(Just-In-Time Compiler), 가비지 컬렉터(Garbage Collector) 등으로 구성됩니다. 인터프리터는 바이트 코드를 한 줄씩 읽어 실행하고, JIT 컴파일러는 자주 실행되는 코드의 성능을 높이기 위해 바이트 코드를 네이티브 코드로 변환하여 실행합니다.