001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.util;
017
018import java.lang.reflect.Field;
019import java.lang.reflect.InvocationTargetException;
020import java.lang.reflect.Method;
021
022import org.apache.log4j.Logger;
023
024/**
025 * Dumps the fields of the given class.
026 * 
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public class ClassDumper {
030    private static final Logger LOG = Logger.getLogger(ClassDumper.class);
031
032    public static void dumpFieldsToLog(Object o) {
033        if (LOG.isDebugEnabled()) {
034            LOG.debug(dumpFields(o));
035        } else if (LOG.isInfoEnabled()) {
036            if (o == null) {
037                LOG.info("null");
038            } else {
039                LOG.info(o.getClass() + ": " + o.toString());
040            }
041        }
042    }
043
044    public static String dumpFields(Object o) {
045        StringBuffer buf = new StringBuffer();
046
047        if (o == null) {
048            return "NULL";
049        }
050
051        Class clazz = o.getClass();
052        // maybe just iterating over getter methods themselves would be a better strategy?
053        // or maybe just jakarta commons lang ToStringBuilder.reflectionToString(String, MULTI_LINE_STYLE):
054        // http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/ToStringBuilder.html
055        Field[] fields = clazz.getDeclaredFields();
056
057        for (int i = 0; i < fields.length; ++i) {
058            try {
059                String methodName = "get" + fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1);
060                Method method = clazz.getMethod(methodName, null);
061                Object value = method.invoke(o, null);
062                buf.append(fields[i].getName()).append(" : ");
063
064                if (value == null) {
065                    buf.append("null\n");
066                } else {
067                    buf.append(value.toString()).append("\n");
068                }
069            } catch (IllegalAccessException e) {
070                buf.append(fields[i].getName()).append(" unavailable by security policy\n");
071            } catch (NoSuchMethodException ex) {
072                buf.append(fields[i].getName()).append(" no getter method for this field\n");
073            } catch (InvocationTargetException ex) {
074                buf.append(fields[i].getName()).append(" unable to invoke the method on target\n");
075            }
076        }
077
078        return buf.toString();
079    }
080}