Search in sources :

Example 11 with FastPropertyDescriptor

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

the class BeanUtility method getFastPropertyDescriptors.

/**
 * Get all properties from this class up to (and excluding) stopClazz
 *
 * @param filter
 */
public static FastPropertyDescriptor[] getFastPropertyDescriptors(Class<?> clazz, Class<?> stopClazz, IPropertyFilter filter) {
    FastBeanInfo info = getFastBeanInfo(clazz, stopClazz);
    FastPropertyDescriptor[] a = info.getPropertyDescriptors();
    ArrayList<FastPropertyDescriptor> filteredProperties = new ArrayList<FastPropertyDescriptor>(a.length);
    for (int i = 0; i < a.length; i++) {
        FastPropertyDescriptor pd = a[i];
        if (filter != null && !(filter.accept(pd))) {
        // ignore it
        } else {
            filteredProperties.add(pd);
        }
    }
    return filteredProperties.toArray(new FastPropertyDescriptor[filteredProperties.size()]);
}
Also used : ArrayList(java.util.ArrayList) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) FastBeanInfo(org.eclipse.scout.rt.platform.reflect.FastBeanInfo)

Example 12 with FastPropertyDescriptor

use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor 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)

Example 13 with FastPropertyDescriptor

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

the class BeanUtility method getProperties.

/**
 * @return all properties of from up to (and excluding) to stopClazz, filtering with filter
 */
public static Map<String, Object> getProperties(Object from, Class<?> stopClazz, IPropertyFilter filter) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    try {
        FastPropertyDescriptor[] props = getFastPropertyDescriptors(from.getClass(), stopClazz, filter);
        for (int i = 0; i < props.length; i++) {
            FastPropertyDescriptor fromProp = props[i];
            Method readMethod = fromProp.getReadMethod();
            if (readMethod != null) {
                Object value = readMethod.invoke(from, (Object[]) null);
                map.put(fromProp.getName(), value);
            }
        }
    } catch (Exception e) {
        throw new ProcessingException("object: " + from, e);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

FastPropertyDescriptor (org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)13 Method (java.lang.reflect.Method)8 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)5 FastBeanInfo (org.eclipse.scout.rt.platform.reflect.FastBeanInfo)5 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 IHolder (org.eclipse.scout.rt.platform.holders.IHolder)4 HashMap (java.util.HashMap)3 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Collection (java.util.Collection)2 IBeanArrayHolder (org.eclipse.scout.rt.platform.holders.IBeanArrayHolder)2 ITableBeanHolder (org.eclipse.scout.rt.platform.holders.ITableBeanHolder)2 NVPair (org.eclipse.scout.rt.platform.holders.NVPair)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1