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