Coverage Report - org.kuali.rice.kew.util.ClassDumper
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassDumper
0%
0/30
0%
0/12
6
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.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 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  
  * Dumps the fields of the given class.
 27  
  * 
 28  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
         // maybe just iterating over getter methods themselves would be a better strategy?
 54  
         // or maybe just jakarta commons lang ToStringBuilder.reflectionToString(String, MULTI_LINE_STYLE):
 55  
         // http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/ToStringBuilder.html
 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  
 }