[Reflection] - Value Object 로그 만들자

language/java 2013. 11. 8. 10:23

http://bbaeggar.tistory.com/20  원문  

기존에 있던 함수는 int형이나 중간에 오류가 나면 로그가 정확하게 생성이 않되어 조금 변경함



 public static String getRequestStringMethods(Object obj)

    {

        StringBuffer buffer = new StringBuffer();

        Class dymClass =null;

        Method[] methods=null;

        try

        {

             dymClass = obj.getClass();

             methods = dymClass.getMethods();

        }

        catch(Exception e)

        {

            log.debug("getRequestStringMethods||111||Exception::"+e);

        }

        buffer.append(dymClass.getSimpleName());

        buffer.append(" : ");

        for(int i = 0; i < methods.length; i++)

        {

            try

            {

                String methodName = methods[i].getName();

                if("get".equals(methodName.substring(0, 3)) && !"getClass".equals(methodName))

                {

                    String value = "";

                    if(methods[i].invoke(obj, null)==null)

                    {

                        value = (String)methods[i].invoke(obj, null);

                    }

                    else

                    {

                        value = (String)methods[i].invoke(obj, null).toString();

                    }

//                    Object[] params = new Object[] { new Integer( value ) };

                    buffer.append("[" + methodName.substring(3, 4).toLowerCase() + methodName.subSequence(4, methodName.length()) + "]");

                    buffer.append(":\"" + value + "\"");

                    buffer.append(" ");

                }

            }

            catch(IllegalArgumentException e)

            {

                log.debug("getRequestStringMethods||IllegalArgumentException::"+e);

            }

            catch(IllegalAccessException e)

            {

                log.debug("getRequestStringMethods||IllegalAccessException::"+e);

            }

            catch(InvocationTargetException e)

            {

                log.debug("getRequestStringMethods||InvocationTargetException::"+e);

            }

            catch(Exception e)

            {

                log.debug("getRequestStringMethods||Exception::"+e);

            }

        }

        return buffer.toString();

    }

    

    



    

    public static String getRequestStringFields(Object obj) {

        

        StringBuffer buffer = new StringBuffer();

        Class dymClass = null;

        Field[] fields = null;

        try

        {

            dymClass = obj.getClass();

            fields = dymClass.getDeclaredFields();

        }

        catch(Exception e)

        {

            log.debug("getRequestStringFields||Exception111::" + e);

        }

        buffer.append(dymClass.getSimpleName());

        buffer.append(" : ");

        for(int i = 0; i < fields.length; i++)

        {

            try

            {

                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 = "";

                if(method.invoke(obj, null)==null)

                {

                    value = (String)method.invoke(obj, null);

                }

                else

                {

                    value = (String)method.invoke(obj, null).toString();

                }

                buffer.append("[" + methodName.substring(3, 4).toLowerCase() + methodName.subSequence(4, methodName.length()) + "]");

                buffer.append(":\"" + value + "\"");

                buffer.append(" ");

            }

            catch(IllegalArgumentException e)

            {

                log.debug("getRequestStringFields||IllegalArgumentException::" + e);

            }

            catch(IllegalAccessException e)

            {

                log.debug("getRequestStringFields||IllegalAccessException::" + e);

            }

            catch(InvocationTargetException e)

            {

                log.debug("getRequestStringFields||InvocationTargetException::" + e);

            }

            catch(Exception e)

            {

                log.debug("getRequestStringFields||Exception::" + e);

            }

        }

        return buffer.toString();

    }

: