01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.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 rkirkend
29: */
30: public class ClassDumper {
31: private static final Logger LOG = Logger
32: .getLogger(ClassDumper.class);
33:
34: public static void dumpFieldsToLog(Object o) {
35: if (LOG.isDebugEnabled()) {
36: LOG.debug(dumpFields(o));
37: } else if (LOG.isInfoEnabled()) {
38: if (o == null) {
39: LOG.info("null");
40: } else {
41: LOG.info(o.getClass() + ": " + o.toString());
42: }
43: }
44: }
45:
46: public static String dumpFields(Object o) {
47: StringBuffer buf = new StringBuffer();
48:
49: if (o == null) {
50: return "NULL";
51: }
52:
53: Class clazz = o.getClass();
54: // maybe just iterating over getter methods themselves would be a better strategy?
55: // or maybe just jakarta commons lang ToStringBuilder.reflectionToString(String, MULTI_LINE_STYLE):
56: // http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/ToStringBuilder.html
57: Field[] fields = clazz.getDeclaredFields();
58:
59: for (int i = 0; i < fields.length; ++i) {
60: try {
61: String methodName = "get"
62: + fields[i].getName().substring(0, 1)
63: .toUpperCase()
64: + fields[i].getName().substring(1);
65: Method method = clazz.getMethod(methodName, null);
66: Object value = method.invoke(o, null);
67: buf.append(fields[i].getName()).append(" : ");
68:
69: if (value == null) {
70: buf.append("null\n");
71: } else {
72: buf.append(value.toString()).append("\n");
73: }
74: } catch (IllegalAccessException e) {
75: buf.append(fields[i].getName()).append(
76: " unavailable by security policy\n");
77: } catch (NoSuchMethodException ex) {
78: buf.append(fields[i].getName()).append(
79: " no getter method for this field\n");
80: } catch (InvocationTargetException ex) {
81: buf.append(fields[i].getName()).append(
82: " unable to invoke the method on target\n");
83: }
84: }
85:
86: return buf.toString();
87: }
88: }
89:
90: /*
91: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
92: *
93: * This file is part of the EDEN software package. For license information, see
94: * the LICENSE file in the top level directory of the EDEN source distribution.
95: */
|