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-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * Dumps the fields of the given class.
 26  
  * 
 27  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
         // maybe just iterating over getter methods themselves would be a better strategy?
 53  
         // or maybe just jakarta commons lang ToStringBuilder.reflectionToString(String, MULTI_LINE_STYLE):
 54  
         // http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/ToStringBuilder.html
 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  
 }