반응형

자바(Java) Reflect 예제 동적 Method 호출

 
ReflectMethod.java
 
001.package reflect;
002. 
003.public class ReflectMethod {
004.  
005.   //field
006.   public String field;
007.  
008.   //constructor
009.   public ReflectMethod() {}
010. 
011.   public ReflectMethod(String field) {
012.      this.field = field;
013.   }
014.  
015.   //method
016.   public String getField() {
017.      System.out.println("call field : " + this.field);
018.      return this.field;
019.   }
020.   public void methodA() {
021.      System.out.println("call method A");
022.   }
023.   public void methodB(String str) {
024.      System.out.println("call method B : " + str);
025.   }
026.   public String methodC() {
027.      System.out.println("call method C");
028.      return "method C";
029.   }
030.}
031. 
032.ReflectMain.java
033. 
034.package reflect;
035. 
036.import java.lang.reflect.Constructor;
037.import java.lang.reflect.Field;
038.import java.lang.reflect.InvocationTargetException;
039.import java.lang.reflect.Method;
040. 
041.public class ReflectMain {
042.   public static void main(String[] arguments) throws
043.                                         ClassNotFoundException,
044.                                         InstantiationException,
045.                                         IllegalAccessException,
046.                                         SecurityException,
047.                                         NoSuchMethodException,
048.                                         IllegalArgumentException,
049.                                         InvocationTargetException,
050.                                         NoSuchFieldException {
051.   
052.  /*
053.   * 원시형 클래스 타입
054.   * char : Character.TYPE
055.   * byte : Byte.TYPE
056.   * short : Short.TYPE
057.   * int : Integer.TYPE
058.   * long : Long.TYPE
059.   * float : Float.TYPE
060.   * double : Double.TYPE
061.   */
062.   
063.      //클래스 가져오기
064.      Class getClass1 = Class.forName("reflect.ReflectMethod");
065.      Class getClass2 = ReflectMethod.class;
066.      Class getClass3 = new ReflectMethod().getClass();
067.   
068.      //클래스 객체를 이용한 객체 생성
069. 
070.      //생성자에 인자가 없는 경우
071.      Object object1 = getClass1.newInstance();
072.      //생성자에 인자가 있을 경우
073.      Class[] constructorParamClass = new Class[] {String.class};
074.      Object[] constructorParamObject = new Object[] {"Reflect Test!!!"};
075.      Constructor constructor = getClass1.getConstructor(constructorParamClass);
076.      Object object2 = constructor.newInstance(constructorParamObject);
077. 
078.      //필드 값 가져오기
079.      Field field = getClass1.getField("field");
080.      Object fieldObject = field.get(object2);
081.   
082.      System.out.println(fieldObject);
083.   
084.      //메소드 호출
085.   
086.      //메소드에 인자가 없는 경우
087.      Method method1 = getClass1.getMethod("methodA");
088.      method1.invoke(object1);
089.      //메소드에 인자가 있을 경우
090.      Class[] methodParamClass = new Class[] {String.class};
091.      Object[] methodParamObject = new Object[] {"1 parameter"};
092.      Method method2 = getClass1.getMethod("methodB", methodParamClass);
093.      method2.invoke(object1, methodParamObject);
094.      //메소드에 리턴값이 있을 경우
095.      Method method3 = getClass1.getMethod("methodC");
096.      Object returnObject = method3.invoke(object1);
097.   
098.      System.out.println("methodC return value : " + returnObject);
099.   }
100.}

콘솔출력결과
Reflect Test!!!

call method A
call method B : 1 parameter
call method C
methodC return value : method C

 

반응형
LIST

+ Recent posts