Search in sources :

Example 6 with FastBeanInfo

use of org.eclipse.scout.rt.platform.reflect.FastBeanInfo in project scout.rt by eclipse.

the class BeanUtility method setProperties.

/**
 * @param lenient
 *          true just logs warnings on exceptions, false throws exceptions set all properties on to, filtering with
 *          filter
 */
public static void setProperties(Object to, Map<String, Object> map, boolean lenient, IPropertyFilter filter) {
    FastBeanInfo toInfo = getFastBeanInfo(to.getClass(), null);
    for (Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<String, Object> entry = it.next();
        String name = entry.getKey();
        Object value = entry.getValue();
        try {
            FastPropertyDescriptor desc = toInfo.getPropertyDescriptor(name);
            if (desc != null && (filter == null || filter.accept(desc))) {
                Method writeMethod = desc.getWriteMethod();
                if (writeMethod != null) {
                    writeMethod.invoke(to, new Object[] { TypeCastUtility.castValue(value, writeMethod.getParameterTypes()[0]) });
                }
            }
        } catch (Exception e) {
            if (lenient) {
                LOG.warn("Could not set property property '{}' to value '{}'", name, value, e);
            } else {
                throw new ProcessingException("property " + name + " with value " + value, e);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) FastBeanInfo(org.eclipse.scout.rt.platform.reflect.FastBeanInfo) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

FastBeanInfo (org.eclipse.scout.rt.platform.reflect.FastBeanInfo)6 FastPropertyDescriptor (org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)5 Method (java.lang.reflect.Method)3 Field (java.lang.reflect.Field)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 NoSuchElementException (java.util.NoSuchElementException)1 Set (java.util.Set)1 IgnoreProperty (org.eclipse.scout.rt.platform.annotations.IgnoreProperty)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 BinaryResource (org.eclipse.scout.rt.platform.resource.BinaryResource)1