1. CLASS
package T2017.t09;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import testUtil.testVO;
public class t0927_setter_getter_test {
public static void main(String arg[]) throws IllegalArgumentException, InvocationTargetException{
testVO tvo = new testVO();
Map<String,Object> rMap = new HashMap<String, Object>();;
rMap.put("a", "a1");
rMap.put("b", "b1");
rMap.put("c", "c1");
valueSettings(tvo, rMap);
System.out.println(tvo.getA() + ", " + tvo.getB() + ", " + tvo.getC());
}
public static void valueSettings(Object obj, Map<String, Object> param) throws IllegalArgumentException, InvocationTargetException{
String methodType = null;
Object getterVal = null;
java.lang.reflect.Method setter = null;
for(String key : param.keySet()){
try {
getterVal = null;
if(getterVal == null){
methodType = setter(key, true);
setter = getSwitchType(obj, methodType);
if (setter != null) {
setter.setAccessible(true);
setter.invoke(obj, param.get(key));
}
}
} catch (IllegalAccessException e) {
e.getStackTrace();
}
}
}
private static java.lang.reflect.Method getSwitchType(Object obj, String methodType) {
try {
java.lang.reflect.Method[] methods = obj.getClass().getMethods();
for (java.lang.reflect.Method m : methods)
if (m.getName().equals(methodType)) return m;
} catch (SecurityException e) {
e.getStackTrace();
}
return null;
}
private static String setter(String prop, boolean tf) { // tf는 추후 vo에서 값을 가져올 때 사용할 용도..
String setter = "set";
setter += prop.substring(0, 1).toUpperCase() + prop.substring(1);
return setter;
}
package testUtil;
public class testVO {
private String a = null;
private String b = null;
private String c = null;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
2. RUN
a1, b1, c1
'course > 자바' 카테고리의 다른 글
동음이어 검색용 정규식 생성. (0) | 2017.12.19 |
---|---|
문자열의 일정 자릿수까지 특정 문자로 변경. (0) | 2017.04.18 |
간단한 문자열 검색 (0) | 2017.04.18 |