java reflection - 리플렉션한 메소드 파라미터 분석

This commit is contained in:
kim
2021-01-16 15:15:33 +09:00
parent 8fc0754ab5
commit 5bb0c8d488
15 changed files with 145 additions and 8 deletions

View File

@@ -1,17 +1,19 @@
package com.example.reflect.controller;
import com.example.reflect.anno.RequestMapping;
import com.example.reflect.controller.dto.JoinDto;
import com.example.reflect.controller.dto.LoginDto;
public class UserController {
@RequestMapping("/join")
public String join(){
@RequestMapping("/user/join")
public String join(JoinDto dto){
System.out.println("join() 함수 호출");
return "/";
}
@RequestMapping("/login")
public String login(){
@RequestMapping("/user/login")
public String login(LoginDto dto){
System.out.println("login()함수 호출");
return "/";
}

View File

@@ -0,0 +1,40 @@
package com.example.reflect.controller.dto;
public class JoinDto {
private String username;
private String password;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "JoinDto{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}

View File

@@ -0,0 +1,30 @@
package com.example.reflect.controller.dto;
public class LoginDto {
private String username;
private String password;
@Override
public String toString() {
return "LoginDto{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@@ -10,6 +10,8 @@ import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Enumeration;
public class Dispatcher implements Filter {
@@ -50,14 +52,26 @@ public class Dispatcher implements Filter {
for (Method method : methods) { // 메소드 갯수만큼 반복
Annotation annotation = method.getDeclaredAnnotation(RequestMapping.class);
RequestMapping requestMapping = (RequestMapping) annotation;
System.out.println(requestMapping.value());
// System.out.println(requestMapping.value());
if(requestMapping.value().equals(endPoint)) {
try {
String path = (String) method.invoke(userController);
Parameter[] parameters = method.getParameters();
String path = null;
if(parameters.length != 0) {
// System.out.println("parameters[0].getType() : " + parameters[0].getType());
Object dtoInstance = parameters[0].getType().getConstructor().newInstance();
// String username = request.getParameter("username");
// String password = request.getParameter("password");
// System.out.println(username + ", " + password);
Enumeration<String> keys = request.getParameterNames(); // username, password
// keys 값을 parameterName -> setParameterName 으로 변경
RequestDispatcher dis = request.getRequestDispatcher(path);
// 내부에서 실행하기 때문에 필터 안탐
path = "/";
} else {
path = (String) method.invoke(userController);
}
RequestDispatcher dis = request.getRequestDispatcher(path); // 내부에서 실행하기 때문에 필터 안탐
dis.forward(request, response);
} catch (Exception e) {
e.printStackTrace();

View File

@@ -0,0 +1,51 @@
package com.example.reflect.model;
public class User {
private int id;
private String username;
private String password;
private String email;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}