Coverage report

  %line %branch
net.sf.mindoro.commons.util.BeanUtils
70% 
88% 

 1  
 /*
 2  
  * Created on May 13, 2005
 3  
  *
 4  
  * TODO To change the template for this generated file go to
 5  
  * Window - Preferences - Java - Code Style - Code Templates
 6  
  */
 7  
 package net.sf.mindoro.commons.util;
 8  
 
 9  
 import java.lang.reflect.InvocationTargetException;
 10  
 import java.lang.reflect.Method;
 11  
 import java.lang.reflect.Modifier;
 12  
 
 13  
 import org.apache.commons.logging.Log;
 14  
 import org.apache.commons.logging.LogFactory;
 15  
 
 16  
 /**
 17  
  * @author aisrael
 18  
  * 
 19  
  * TODO To change the template for this generated type comment go to Window -
 20  
  * Preferences - Java - Code Style - Code Templates
 21  
  */
 22  
 public final class BeanUtils {
 23  
 
 24  6
     private static final Log log = LogFactory.getLog(BeanUtils.class);
 25  
 
 26  
     /**
 27  
      * BeanUtils instances should not be constructed in regular programming.
 28  
      */
 29  0
     private BeanUtils() {
 30  
         // noop
 31  0
     }
 32  
 
 33  
     /**
 34  
      * Return a String representation of the given object listing all public
 35  
      * properties (suitable for logging or debugging).
 36  
      * 
 37  
      * @param obj
 38  
      *            any Object
 39  
      * @return a String representation of this object listing all public
 40  
      *         properties
 41  
      */
 42  
     public static String beanToString(final Object obj) {
 43  2
         final StringBuffer sb = new StringBuffer();
 44  
 
 45  2
         sb.append('(');
 46  2
         if (null == obj) {
 47  0
             sb.append("null");
 48  
         } else {
 49  2
             final Class objClass = obj.getClass();
 50  2
             sb.append(objClass.getName()).append(' ');
 51  
 
 52  2
             final Method[] methods = objClass.getDeclaredMethods();
 53  26
             for (int i = 0; i < methods.length; ++i) {
 54  24
                 final Method method = methods[i];
 55  24
                 if (Modclass="keyword">ifier.isPublic(method.getModclass="keyword">ifiers()) && method.getName().startsWith("get")
 56  
                         && method.getParameterTypes().length == 0) {
 57  
                     // most probably a public property getter
 58  12
                     extractPropertyValue(sb, obj, method);
 59  
                 }
 60  
             }
 61  
         }
 62  2
         sb.append(')');
 63  2
         return sb.toString();
 64  
     }
 65  
 
 66  
     /**
 67  
      * @param sb
 68  
      *            StringBuffer
 69  
      * @param obj
 70  
      *            Object
 71  
      * @param method
 72  
      *            Method
 73  
      */
 74  
     private static void extractPropertyValue(final StringBuffer sb, class="keyword">final Object obj, class="keyword">final Method method) {
 75  
         try {
 76  12
             final Object value = method.invoke(obj, new Object[0]);
 77  12
             sb.append('(').append(getFriendlyName(method.getName())).append(' ');
 78  
 
 79  12
             if (null == value) {
 80  6
                 sb.append("null");
 81  
             } else {
 82  6
                 final String valueToString = value.toString();
 83  6
                 if (value.getClass() == java.lang.String.class) {
 84  6
                     sb.append('"').append(valueToString).append('"');
 85  
                 } else {
 86  0
                     sb.append(valueToString);
 87  
                 }
 88  
             }
 89  12
             sb.append(')');
 90  0
         } catch (final IllegalArgumentException e) {
 91  0
             log.error("IllegalArgumentException caught invoking \"" + method.getName() + "\" on "
 92  
                     + obj.toString(), e);
 93  0
         } catch (final IllegalAccessException e) {
 94  0
             log.error("IllegalAccessException caught invoking \"" + method.getName() + "\" on "
 95  
                     + obj.toString(), e);
 96  0
         } catch (final InvocationTargetException e) {
 97  0
             log.error("InvocationTargetException caught invoking \"" + method.getName() + "\" on "
 98  
                     + obj.toString(), e);
 99  12
         }
 100  12
     }
 101  
 
 102  
     /**
 103  
      * @param name
 104  
      *            getXXX() method name
 105  
      * @return 'friendly name
 106  
      */
 107  
     private static String getFriendlyName(final String name) {
 108  
         final String result;
 109  
 
 110  12
         if (name.startsWith("get")) {
 111  12
             result = name.substring(3, 4).toLowerCase() + name.substring(4, name.length());
 112  
         } else {
 113  0
             result = name;
 114  
         }
 115  
 
 116  12
         return result;
 117  
     }
 118  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.