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