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