웹 서비스 작업을 하면서 아쉽지만 Request를 각 서비스별로 생성하게 되었다. 유연하게 Request 객체를 하나만 생성해서 작업하면 좋것지만, Http binding(GET) 을 지원해야 한다는 제약 사항 때문에 이래저래 귀찮은 일이 좀 생긴듯.

여 기서 하나.. 각 request 별 입력값을 로그로 남겨야 하는데.. 각 value object 에 toString()을 구현하기가 참 귀찮다는 것이다. 그래서 생각한 것이 reflection 을 이용해서 한번에 해결하자 라는 취지로 만들게 되었음.

public class Test {

    public String getRequestStringMethods(Object obj) {
  
        StringBuffer buffer = new StringBuffer();
      
        try {
            Class dymClass      = obj.getClass();
            Method[] methods = dymClass.getMethods();
          
            buffer.append(dymClass.getSimpleName());
            buffer.append(" : ");
            for(int i = 0;i<methods.length;i++) {
                String methodName = methods[i].getName();
                if("get".equals(methodName.substring(0, 3)) && !"getClass".equals(methodName)) {                
                    String value = (String)methods[i].invoke(obj, null);                  
                    buffer.append("[" + methodName.substring(3, 4).toLowerCase() + methodName.subSequence(4, methodName.length()) + "]");
                    buffer.append(":\"" + value + "\"");
                    buffer.append(" ");
                }
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
        return buffer.toString();
    }
  
    public String getRequestStringFields(Object obj) {
      
        StringBuffer buffer = new StringBuffer();
      
        try {
            Class dymClass      = obj.getClass();
            Field[] fields      = dymClass.getDeclaredFields();
          
            buffer.append(dymClass.getSimpleName());
            buffer.append(" : ");
            for(int i = 0;i<fields.length;i++) {
                String methodName = "get" + fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1, fields[i].getName().length());              
                Method method       = obj.getClass().getMethod(methodName, null);
                String value = (String)method.invoke(obj, null);                  
                buffer.append("[" + methodName.substring(3, 4).toLowerCase() + methodName.subSequence(4, methodName.length()) + "]");
                buffer.append(":\"" + value + "\"");
                buffer.append(" ");
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
        return buffer.toString();
    }
  
    public static void main(String[] args) {
      
        TestVo vo = new TestVo();
        vo.setAge("33");
        vo.setId("service_01");
        vo.setMsg("message");
        vo.setName("bbaeggar");
        vo.setValue("value");
      
        vo.setValue1("value1");
        vo.setValue2("value2");
        vo.setValue3("value3");
        vo.setValue4("value4");
        vo.setValue5("value5");
        vo.setValue6("value6");
        vo.setValue7("value7");
        vo.setValue8("value8");
        vo.setValue9("value9");
        vo.setValue10("value10");
        System.out.println(new Test().getRequestStringFields(vo));
        System.out.println(new Test().getRequestStringMethods(vo));

    }
}

Result
TestVo : [id]:"service_01" [name]:"bbaeggar" [msg]:"message" [value]:"value" [age]:"33" [value1]:"value1" [value2]:"value2" [value3]:"value3" [value4]:"value4" [value5]:"value5" [value6]:"value6" [value7]:"value7" [value8]:"value8" [value9]:"value9" [value10]:"value10"

TestVo : [name]:"bbaeggar" [value]:"value" [id]:"service_01" [msg]:"message" [age]:"33" [value1]:"value1" [value2]:"value2" [value3]:"value3" [value4]:"value4" [value5]:"value5" [value6]:"value6" [value7]:"value7" [value8]:"value8" [value9]:"value9" [value10]:"value10"

getRequestStringFields() 메소드를 사용하면 필드를 선언한 순서대로 값을 가지고 온다.
getRequestStringMethods() 는 좀 멍청한 넘임.
Posted by 짱가쟁이