Coverage Report - org.mule.util.BeanUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanUtils
31%
8/26
30%
3/10
5
 
 1  
 /*
 2  
  * $Id: BeanUtils.java 9852 2007-11-23 20:31:42Z rossmason $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util;
 12  
 
 13  
 import java.lang.reflect.Field;
 14  
 import java.util.HashMap;
 15  
 import java.util.Iterator;
 16  
 import java.util.Map;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 
 21  
 /**
 22  
  * <code>BeanUtils</code> provides functions for altering the way commons BeanUtils
 23  
  * works
 24  
  */
 25  
 // @ThreadSafe
 26  0
 public class BeanUtils extends org.apache.commons.beanutils.BeanUtils
 27  
 {
 28  
     public static final String SET_PROPERTIES_METHOD = "setProperties";
 29  
 
 30  
     /** logger used by this class */
 31  2
     private static final Log logger = LogFactory.getLog(BeanUtils.class);
 32  
 
 33  
     /** Exception safe version of BeanUtils.populateWithoutFail */
 34  
     public static void populateWithoutFail(Object object, Map props, boolean logWarnings)
 35  
     {
 36  
         // Check to see if our object has a setProperties method where the properties
 37  
         // map should be set
 38  34
         if (ClassUtils.getMethod(object.getClass(), SET_PROPERTIES_METHOD, new Class[]{Map.class}) != null)
 39  
         {
 40  
             try
 41  
             {
 42  0
                 BeanUtils.setProperty(object, "properties", props);
 43  
             }
 44  0
             catch (Exception e)
 45  
             {
 46  
                 // this should never happen since we explicitly check for the method
 47  
                 // above
 48  0
                 if (logWarnings)
 49  
                 {
 50  0
                     logger.warn("Property: " + SET_PROPERTIES_METHOD + "=" + Map.class.getName()
 51  
                             + " not found on object: " + object.getClass().getName());
 52  
                 }
 53  0
             }
 54  
         }
 55  
         else
 56  
         {
 57  34
             for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
 58  
             {
 59  68
                 Map.Entry entry = (Map.Entry) iterator.next();
 60  
 
 61  
                 try
 62  
                 {
 63  68
                     BeanUtils.setProperty(object, entry.getKey().toString(), entry.getValue());
 64  
                 }
 65  0
                 catch (Exception e)
 66  
                 {
 67  0
                     if (logWarnings)
 68  
                     {
 69  0
                         logger.warn("Property: " + entry.getKey() + "=" + entry.getValue()
 70  
                                 + " not found on object: " + object.getClass().getName());
 71  
                     }
 72  68
                 }
 73  68
             }
 74  
         }
 75  34
     }
 76  
 
 77  
     /**
 78  
      * The Apache BeanUtils version of this converts all values to String, which is pretty useless, it also includes
 79  
      * stuff not defined by the user
 80  
      *
 81  
      * @param object the object to Describe
 82  
      * @return a map of the properties on the object
 83  
      */
 84  
     public static Map describe(Object object)
 85  
     {
 86  0
         Map props = new HashMap(object.getClass().getDeclaredFields().length);
 87  0
         for (int i = 0; i < object.getClass().getDeclaredFields().length; i++)
 88  
         {
 89  0
             Field field = object.getClass().getDeclaredFields()[i];
 90  0
             field.setAccessible(true);
 91  
             try
 92  
             {
 93  0
                 props.put(field.getName(), field.get(object));
 94  
             }
 95  0
             catch (IllegalAccessException e)
 96  
             {
 97  0
                 logger.debug("Unable to read field: " + field.getName() + " on object: " + object);
 98  0
             }
 99  
         }
 100  0
         return props;
 101  
     }
 102  
 }