1 package org.testng.internal; 2 3 import org.testng.log4testng.Logger; 4 5 import java.beans.BeanInfo; 6 import java.beans.IntrospectionException; 7 import java.beans.Introspector; 8 import java.beans.PropertyDescriptor; 9 import java.lang.reflect.InvocationTargetException; 10 import java.lang.reflect.Method; 11 12 /** 13 * Utility class for setting JavaBeans-style properties on instances. 14 * 15 * @author Cosmin Marginean, Apr 12, 2007 16 */ 17 public class PropertyUtils implements IPropertyUtils { 18 19 private static final Logger LOGGER = Logger.getLogger(PropertyUtils.class); 20 21 public void setProperty(Object instance, String name, String value) { 22 if (instance == null) { 23 LOGGER.warn("Cannot set property " + name + " with value " + value + ". The target instance is null"); 24 return; 25 } 26 27 Class propClass = getPropertyType(instance.getClass(), name); 28 if (propClass == null) { 29 LOGGER.warn("Cannot set property " + name + " with value " + value + ". Property class could not be found"); 30 return; 31 } 32 33 Object realValue = Parameters.convertType(propClass, value, name); 34 //TODO: Here the property desc is serched again 35 setPropertyRealValue(instance, name, realValue); 36 } 37 38 public Class getPropertyType(Class instanceClass, String propertyName) { 39 if (instanceClass == null) { 40 LOGGER.warn("Cannot retrieve property class for " + propertyName + ". Target instance class is null"); 41 } 42 PropertyDescriptor propDesc = getPropertyDescriptor(instanceClass, propertyName); 43 return propDesc.getPropertyType(); 44 } 45 46 private static PropertyDescriptor getPropertyDescriptor(Class targetClass, String propertyName) { 47 PropertyDescriptor result = null; 48 if (targetClass == null) { 49 LOGGER.warn("Cannot retrieve property " + propertyName + ". Class is null"); 50 } else { 51 try { 52 BeanInfo beanInfo = Introspector.getBeanInfo(targetClass); 53 PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors(); 54 for (PropertyDescriptor propDesc : propDescriptors) { 55 if (propDesc.getName().equals(propertyName)) { 56 result = propDesc; 57 break; 58 } 59 } 60 } catch (IntrospectionException ie) { 61 LOGGER.warn("Cannot retrieve property " + propertyName + ". Cause is: " + ie); 62 } 63 } 64 return result; 65 } 66 67 public void setPropertyRealValue(Object instance, String name, Object value) { 68 if (instance == null) { 69 LOGGER.warn("Cannot set property " + name + " with value " + value + ". Targe instance is null"); 70 return; 71 } 72 73 PropertyDescriptor propDesc = getPropertyDescriptor(instance.getClass(), name); 74 if (propDesc == null) { 75 LOGGER.warn("Cannot set property " + name + " with value " + value + ". Property does not exist"); 76 return; 77 } 78 79 Method method = propDesc.getWriteMethod(); 80 try { 81 method.invoke(instance, new Object[]{value}); 82 } catch (IllegalAccessException | InvocationTargetException iae) { 83 LOGGER.warn("Cannot set property " + name + " with value " + value + ". Cause " + iae); 84 } 85 } 86 87 } 88