| 15 | |
| 16 | public class MethodCall3 { |
| 17 | public static void main(String[] args) throws Exception{ |
| 18 | Map map = new HashMap(); |
| 19 | map.put("year", "2021"); |
| 20 | map.put("month", "10"); |
| 21 | map.put("day", "1"); |
| 22 | |
| 23 | Model model = null; |
| 24 | Class clazz = Class.forName("com.fastcampus.ch2.YoilTellerMVC"); |
| 25 | Object obj = clazz.newInstance(); |
| 26 | |
| 27 | // YoilTellerMVC.main(int year, int month, int day, Model model) |
| 28 | Method main = clazz.getDeclaredMethod("main", int.class, int.class, int.class, Model.class); |
| 29 | |
| 30 | Parameter[] paramArr = main.getParameters(); |
| 31 | Object[] argArr = new Object[main.getParameterCount()]; |
| 32 | |
| 33 | for(int i=0;i<paramArr.length;i++) { |
| 34 | String paramName = paramArr[i].getName(); |
| 35 | Class paramType = paramArr[i].getType(); |
| 36 | Object value = map.get(paramName); // map에서 못찾으면 value는 null |
| 37 | |
| 38 | // paramType중에 Model이 있으면, 생성 & 저장 |
| 39 | if(paramType==Model.class) { |
| 40 | argArr[i] = model = new BindingAwareModelMap(); |
| 41 | } else if(value != null) { // map에 paramName이 있으면, |
| 42 | // value와 parameter의 타입을 비교해서, 다르면 변환해서 저장 |
| 43 | argArr[i] = convertTo(value, paramType); |
| 44 | } |
| 45 | } |
| 46 | System.out.println("paramArr="+Arrays.toString(paramArr)); |
| 47 | System.out.println("argArr="+Arrays.toString(argArr)); |
| 48 | |
| 49 | |
| 50 | // Controller의 main()을 호출 - YoilTellerMVC.main(int year, int month, int day, Model model) |
| 51 | String viewName = (String)main.invoke(obj, argArr); |
| 52 | System.out.println("viewName="+viewName); |
| 53 | |
| 54 | // Model의 내용을 출력 |
| 55 | System.out.println("[after] model="+model); |
| 56 | |
| 57 | // 텍스트 파일을 이용한 rendering |
| 58 | render(model, viewName); |
| 59 | } // main |
| 60 | |
| 61 | private static Object convertTo(Object value, Class type) { |
| 62 | if(type==null || value==null || type.isInstance(value)) // 타입이 같으면 그대로 반환 |