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