java.lang.reflect.Method클래스를 이용한 Method Invoke예제.
특정 부분의 자동화등에 응용하면 아주 유용하다...
소스보면 그리 어렵지 않다.. 쉽지만 응용하기 나름 ㅎㅎ
Vo(value object) class
-----------------------------------------------------------------
package com.shift.gef.xml;
public class Vo {
private String name;
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
VoTest(value object) class
-----------------------------------------------------------------
package com.shift.gef.xml;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Method;
public class VoTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Vo vo = new Vo();
/** Method Invoke **/
invoke(vo, "setAddress", new Object[]{"서울시 영등포구"});
invoke(vo, "setName", new Object[]{"로져래빗"});
/** 결과 **/
System.out.println("주소 : " + vo.getAddress());
System.out.println("이름 : " + vo.getName());
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Vo vo = new Vo();
/** Method Invoke **/
invoke(vo, "setAddress", new Object[]{"서울시 영등포구"});
invoke(vo, "setName", new Object[]{"로져래빗"});
/** 결과 **/
System.out.println("주소 : " + vo.getAddress());
System.out.println("이름 : " + vo.getName());
/** 결과 Invoke **/
System.out.println("Invoke 주소 : " + invoke(vo, "getAddress", null));
System.out.println("Invoke 이름 : " + invoke(vo, "getName", null));
}
System.out.println("Invoke 주소 : " + invoke(vo, "getAddress", null));
System.out.println("Invoke 이름 : " + invoke(vo, "getName", null));
}
/**
* 특정 클래스의 내용을 invoke
* @param obj Method Invoke할 오브젝트
* @param methodName Method Name
* @param objList Parameter Object List
* @return
*/
public static Object invoke(Object obj, String methodName, Object[] objList) {
Method[] methods = obj.getClass().getMethods();
for(int i=0; i<methods.length; i++) {
if(methods[i].getName().equals(methodName)) {
try {
if (methods[i].getReturnType().getName().equals("void")) {
methods[i].invoke(obj, objList);
} else {
return methods[i].invoke(obj, objList);
}
* 특정 클래스의 내용을 invoke
* @param obj Method Invoke할 오브젝트
* @param methodName Method Name
* @param objList Parameter Object List
* @return
*/
public static Object invoke(Object obj, String methodName, Object[] objList) {
Method[] methods = obj.getClass().getMethods();
for(int i=0; i<methods.length; i++) {
if(methods[i].getName().equals(methodName)) {
try {
if (methods[i].getReturnType().getName().equals("void")) {
methods[i].invoke(obj, objList);
} else {
return methods[i].invoke(obj, objList);
}
} catch(IllegalAccessException lae) {
System.out.println("LAE : " + lae.getMessage());
} catch(InvocationTargetException ite) {
System.out.println("ITE : " + ite.getMessage());
}
}
}
return null;
}
}
System.out.println("LAE : " + lae.getMessage());
} catch(InvocationTargetException ite) {
System.out.println("ITE : " + ite.getMessage());
}
}
}
return null;
}
}
No comments:
Post a Comment