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()]);
}
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);
}
}
}
}
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;
}
Aggregations