View Javadoc

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