View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.util;
17  
18  import java.lang.reflect.Field;
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  
22  import org.apache.log4j.Logger;
23  
24  /**
25   * Dumps the fields of the given class.
26   * 
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class ClassDumper {
30      private static final Logger LOG = Logger.getLogger(ClassDumper.class);
31  
32      public static void dumpFieldsToLog(Object o) {
33          if (LOG.isDebugEnabled()) {
34              LOG.debug(dumpFields(o));
35          } else if (LOG.isInfoEnabled()) {
36              if (o == null) {
37                  LOG.info("null");
38              } else {
39                  LOG.info(o.getClass() + ": " + o.toString());
40              }
41          }
42      }
43  
44      public static String dumpFields(Object o) {
45          StringBuffer buf = new StringBuffer();
46  
47          if (o == null) {
48              return "NULL";
49          }
50  
51          Class clazz = o.getClass();
52          // maybe just iterating over getter methods themselves would be a better strategy?
53          // or maybe just jakarta commons lang ToStringBuilder.reflectionToString(String, MULTI_LINE_STYLE):
54          // http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/ToStringBuilder.html
55          Field[] fields = clazz.getDeclaredFields();
56  
57          for (int i = 0; i < fields.length; ++i) {
58              try {
59                  String methodName = "get" + fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1);
60                  Method method = clazz.getMethod(methodName, null);
61                  Object value = method.invoke(o, null);
62                  buf.append(fields[i].getName()).append(" : ");
63  
64                  if (value == null) {
65                      buf.append("null\n");
66                  } else {
67                      buf.append(value.toString()).append("\n");
68                  }
69              } catch (IllegalAccessException e) {
70                  buf.append(fields[i].getName()).append(" unavailable by security policy\n");
71              } catch (NoSuchMethodException ex) {
72                  buf.append(fields[i].getName()).append(" no getter method for this field\n");
73              } catch (InvocationTargetException ex) {
74                  buf.append(fields[i].getName()).append(" unable to invoke the method on target\n");
75              }
76          }
77  
78          return buf.toString();
79      }
80  }