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 | 0 | public class ClassDumper { |
30 | 0 | private static final Logger LOG = Logger.getLogger(ClassDumper.class); |
31 | |
|
32 | |
public static void dumpFieldsToLog(Object o) { |
33 | 0 | if (LOG.isDebugEnabled()) { |
34 | 0 | LOG.debug(dumpFields(o)); |
35 | 0 | } else if (LOG.isInfoEnabled()) { |
36 | 0 | if (o == null) { |
37 | 0 | LOG.info("null"); |
38 | |
} else { |
39 | 0 | LOG.info(o.getClass() + ": " + o.toString()); |
40 | |
} |
41 | |
} |
42 | 0 | } |
43 | |
|
44 | |
public static String dumpFields(Object o) { |
45 | 0 | StringBuffer buf = new StringBuffer(); |
46 | |
|
47 | 0 | if (o == null) { |
48 | 0 | return "NULL"; |
49 | |
} |
50 | |
|
51 | 0 | Class clazz = o.getClass(); |
52 | |
|
53 | |
|
54 | |
|
55 | 0 | Field[] fields = clazz.getDeclaredFields(); |
56 | |
|
57 | 0 | for (int i = 0; i < fields.length; ++i) { |
58 | |
try { |
59 | 0 | String methodName = "get" + fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1); |
60 | 0 | Method method = clazz.getMethod(methodName, null); |
61 | 0 | Object value = method.invoke(o, null); |
62 | 0 | buf.append(fields[i].getName()).append(" : "); |
63 | |
|
64 | 0 | if (value == null) { |
65 | 0 | buf.append("null\n"); |
66 | |
} else { |
67 | 0 | buf.append(value.toString()).append("\n"); |
68 | |
} |
69 | 0 | } catch (IllegalAccessException e) { |
70 | 0 | buf.append(fields[i].getName()).append(" unavailable by security policy\n"); |
71 | 0 | } catch (NoSuchMethodException ex) { |
72 | 0 | buf.append(fields[i].getName()).append(" no getter method for this field\n"); |
73 | 0 | } catch (InvocationTargetException ex) { |
74 | 0 | buf.append(fields[i].getName()).append(" unable to invoke the method on target\n"); |
75 | 0 | } |
76 | |
} |
77 | |
|
78 | 0 | return buf.toString(); |
79 | |
} |
80 | |
} |