Coverage Report - org.kuali.rice.krad.uif.util.ObjectPropertyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectPropertyUtils
0%
0/37
0%
0/6
1.909
 
 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.krad.uif.util;
 17  
 
 18  
 import org.springframework.beans.BeanWrapper;
 19  
 import org.springframework.beans.BeanWrapperImpl;
 20  
 import org.springframework.beans.BeansException;
 21  
 
 22  
 import java.beans.PropertyDescriptor;
 23  
 import java.util.Map;
 24  
 
 25  
 /**
 26  
  * Utility methods to get/set property values and working with objects
 27  
  * 
 28  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 29  
  * @see org.springframework.beans.BeanWrapper
 30  
  */
 31  0
 public class ObjectPropertyUtils {
 32  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ObjectPropertyUtils.class);
 33  
 
 34  
         public static void copyPropertiesToObject(Map<String, String> properties, Object object) {
 35  0
                 for (Map.Entry<String, String> property : properties.entrySet()) {
 36  0
                         setPropertyValue(object, property.getKey(), property.getValue());
 37  
                 }
 38  0
         }
 39  
 
 40  
         public static PropertyDescriptor[] getPropertyDescriptors(Object object) {
 41  0
                 return wrapObject(object).getPropertyDescriptors();
 42  
         }
 43  
 
 44  
         public static Class<?> getPropertyType(Class<?> object, String propertyPath) {
 45  0
                 return new BeanWrapperImpl(object).getPropertyType(propertyPath);
 46  
         }
 47  
 
 48  
         public static Class<?> getPropertyType(Object object, String propertyPath) {
 49  0
                 return wrapObject(object).getPropertyType(propertyPath);
 50  
         }
 51  
 
 52  
         @SuppressWarnings("unchecked")
 53  
         public static <T extends Object> T getPropertyValue(Object object, String propertyPath) {
 54  0
             T result = null;
 55  
             try {
 56  0
                 result = (T) wrapObject(object).getPropertyValue(propertyPath);
 57  0
             } catch (RuntimeException e) {
 58  0
                 throw new RuntimeException("Error getting property '" + propertyPath + "' from " + object, e);
 59  0
             }
 60  0
             return result;
 61  
         }
 62  
 
 63  
         public static void initializeProperty(Object object, String propertyPath) {
 64  0
                 Class<?> propertyType = getPropertyType(object, propertyPath);
 65  
         try {
 66  0
             setPropertyValue(object, propertyPath, propertyType.newInstance());
 67  0
         } catch (InstantiationException e) {
 68  
             // just set the value to null
 69  0
             setPropertyValue(object, propertyPath, null);
 70  0
         } catch (IllegalAccessException e) {
 71  0
             throw new RuntimeException("Unable to set new instance for property: " + propertyPath, e);
 72  0
         }
 73  0
     }
 74  
 
 75  
         public static void setPropertyValue(Object object, String propertyPath, Object propertyValue) {
 76  0
                 wrapObject(object).setPropertyValue(propertyPath, propertyValue);
 77  0
         }
 78  
 
 79  
         public static void setPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown) {
 80  
                 try {
 81  0
                         wrapObject(object).setPropertyValue(propertyPath, propertyValue);
 82  
                 }
 83  0
                 catch (BeansException e) {
 84  
                         // only throw exception if they have indicated to not ignore unknown
 85  0
                         if (!ignoreUnknown) {
 86  0
                                 throw new RuntimeException(e);
 87  
                         }
 88  0
                         if (LOG.isTraceEnabled()) {
 89  0
                                 LOG.trace("Ignoring exception thrown during setting of property '" + propertyPath + "': "
 90  
                                                 + e.getLocalizedMessage());
 91  
                         }
 92  0
                 }
 93  0
         }
 94  
         
 95  
     public static boolean isReadableProperty(Object object, String propertyPath) {
 96  0
         return wrapObject(object).isReadableProperty(propertyPath);
 97  
     }
 98  
 
 99  
     public static boolean isWritableProperty(Object object, String propertyPath) {
 100  0
         return wrapObject(object).isWritableProperty(propertyPath);
 101  
     }
 102  
 
 103  
         public static BeanWrapper wrapObject(Object object) {
 104  0
                 BeanWrapper beanWrapper = new BeanWrapperImpl(object);
 105  0
                 beanWrapper.setAutoGrowNestedPaths(true);
 106  
                 
 107  0
                 return beanWrapper;
 108  
         }
 109  
 
 110  
 }