1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
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
54
55
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 }