1. 메소드 호출할 클래스 선언
Class inputCls = Class.forName('myproj.Test');
2. 선언한 클래스 Contructor 가져옴.
Constructor<?> inputConst = inputCls.getConstructor();
3. Instance 가져옴.
Object inputIns = inputConst.newInstance();
4. 선언한 클래스의 메소드 가져옴.
Method InpMethod = inputCls.getMethod("METHOD_NAME"), new Class[]{String.clss});// 메소드 input 변수가 String인 경우 String.class. 아닌 경우 해당 class
5. 메소드 invoke
InpMethod.invoke(inputIns, new Object[]{"INPUT STRING"});//메소드를 실행하며 해당 input 값을 넣어줌.
--> 메소드가 get이 있어서 set 메소드를 호출하고 해당 객체에서 get메소드를 호출하여 값을 가져올 수 있음.
get 메소드를 실행하지 않을 경우 2번 생략 후 inputCls.newInstance(); 를 대체해서 넣어줘도 된다.
'프로그래밍 > Java' 카테고리의 다른 글
Reference 출력, String 비교 equals 사용 이유 (0) | 2013.11.21 |
---|