aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/testng/internal/PropertyUtils.java
blob: ba6911040b846ba7c51dcf705d24e2e06a8a95fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.testng.internal;

import org.testng.log4testng.Logger;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Utility class for setting JavaBeans-style properties on instances.
 *
 * @author Cosmin Marginean, Apr 12, 2007
 */
public class PropertyUtils {

  private static final Logger LOGGER = Logger.getLogger(PropertyUtils.class);

  public static void setProperty(Object instance, String name, String value) {
    if (instance == null) {
      LOGGER.warn("Cannot set property " + name + " with value " + value + ". The target instance is null");
      return;
    }

    Class propClass = getPropertyType(instance.getClass(), name);
    if (propClass == null) {
      LOGGER.warn("Cannot set property " + name + " with value " + value + ". Property class could not be found");
      return;
    }

    Object realValue = Parameters.convertType(propClass, value, name);
    //TODO: Here the property desc is serched again
    setPropertyRealValue(instance, name, realValue);
  }

  public static Class getPropertyType(Class instanceClass, String propertyName) {
    if (instanceClass == null) {
      LOGGER.warn("Cannot retrieve property class for " + propertyName + ". Target instance class is null");
    }
    PropertyDescriptor propDesc = getPropertyDescriptor(instanceClass, propertyName);
    return propDesc.getPropertyType();
  }

  private static PropertyDescriptor getPropertyDescriptor(Class targetClass, String propertyName) {
    PropertyDescriptor result = null;
    if (targetClass == null) {
      LOGGER.warn("Cannot retrieve property " + propertyName + ". Class is null");
    } else {
      try {
        BeanInfo beanInfo = Introspector.getBeanInfo(targetClass);
        PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propDesc : propDescriptors) {
          if (propDesc.getName().equals(propertyName)) {
            result = propDesc;
            break;
          }
        }
      } catch (IntrospectionException ie) {
        LOGGER.warn("Cannot retrieve property " + propertyName + ". Cause is: " + ie);
      }
    }
    return result;
  }

  public static void setPropertyRealValue(Object instance, String name, Object value) {
    if (instance == null) {
      LOGGER.warn("Cannot set property " + name + " with value " + value + ". Targe instance is null");
      return;
    }

    PropertyDescriptor propDesc = getPropertyDescriptor(instance.getClass(), name);
    if (propDesc == null) {
      LOGGER.warn("Cannot set property " + name + " with value " + value + ". Property does not exist");
      return;
    }

    Method method = propDesc.getWriteMethod();
    try {
      method.invoke(instance, new Object[]{value});
    } catch (IllegalAccessException | InvocationTargetException iae) {
      LOGGER.warn("Cannot set property " + name + " with value " + value + ". Cause " + iae);
    }
  }

}