use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.
the class TableRowDataMapper method exportTableRowData.
@Override
public void exportTableRowData(ITableRow row, AbstractTableRowData rowData) {
for (IColumn column : m_columnSet.getColumns()) {
if (m_ignoredColumns.contains(column)) {
continue;
}
Object value = column.getValue(row);
FastPropertyDescriptor propertyDesc = m_propertyDescriptorByColumn.get(column);
if (propertyDesc != null) {
try {
Method columnWriteMethod = propertyDesc.getWriteMethod();
Object dto = getDataContainer(rowData, columnWriteMethod.getDeclaringClass());
columnWriteMethod.invoke(dto, value);
} catch (Exception t) {
LOG.warn("Error writing row data property for column [{}]", column.getClass().getName(), t);
}
} else {
rowData.setCustomValue(column.getColumnId(), value);
}
}
rowData.setRowState(row.getStatus());
exportCustomValues(row, rowData);
}
use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.
the class TableRowDataMapper method getValue.
private Object getValue(IColumn<?> column, AbstractTableRowData rowData) {
Object value = null;
FastPropertyDescriptor propertyDesc = m_propertyDescriptorByColumn.get(column);
if (propertyDesc != null) {
try {
Method columnReadMethod = propertyDesc.getReadMethod();
Object dto = getDataContainer(rowData, columnReadMethod.getDeclaringClass());
value = columnReadMethod.invoke(dto);
} catch (Exception e) {
LOG.warn("Error reading row data property for column [{}]", column.getClass().getName(), e);
}
} else {
value = rowData.getCustomValue(column.getColumnId());
}
return value;
}
use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.
the class TableRowDataPropertyFilterTest method testTableRowDataPropertyFilter.
@Test
public void testTableRowDataPropertyFilter() throws Exception {
TableRowDataPropertyFilter propertyFilter = new TableRowDataPropertyFilter();
FastPropertyDescriptor[] props = BeanUtility.getFastPropertyDescriptors(P_TableRowData.class, AbstractTableRowData.class, propertyFilter);
Map<String, FastPropertyDescriptor> propertyDescriptorsByName = new HashMap<String, FastPropertyDescriptor>();
for (FastPropertyDescriptor prop : props) {
propertyDescriptorsByName.put(prop.getName(), prop);
}
assertEquals(3, propertyDescriptorsByName.size());
assertTrue(propertyDescriptorsByName.containsKey("intColumn"));
assertTrue(propertyDescriptorsByName.containsKey("booleanColumn"));
assertTrue(propertyDescriptorsByName.containsKey("objectColumn"));
}
use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.
the class JsonObjectUtility method jsonValueToJava.
@SuppressWarnings("unchecked")
private static <T> T jsonValueToJava(Object jval, Class<T> type, boolean throwForMissingProperty) {
if (jval == null || jval == JSONObject.NULL) {
return null;
}
// basic types
if (type == String.class) {
return (T) jval;
}
if (type == byte[].class) {
return (T) new JsonByteArray((String) jval).getBytes();
}
if (type == Date.class) {
return (T) new JsonDate((String) jval).asJavaDate();
}
if (type == int.class || type == Integer.class) {
return (T) jval;
}
if (type == long.class || type == Long.class) {
return (T) jval;
}
if (type == boolean.class || type == Boolean.class) {
return (T) jval;
}
// array
if (jval instanceof JSONArray) {
JSONArray jarray = (JSONArray) jval;
int n = jarray.length();
T array = (T) Array.newInstance(type.getComponentType(), n);
for (int i = 0; i < n; i++) {
Array.set(array, i, jsonArrayElementToJava(jarray, i, type.getComponentType(), throwForMissingProperty));
}
return array;
}
// bean
JSONObject jbean = (JSONObject) jval;
T o;
try {
o = (T) type.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("type " + type + " object " + jval, e);
}
try {
HashSet<String> missingNames = new HashSet<>();
String[] nameArray = getNames(jbean);
if (nameArray != null) {
for (String key : nameArray) {
missingNames.add(key);
}
for (String key : nameArray) {
try {
// NOSONAR
Field f = type.getField(key);
if (Modifier.isStatic(f.getModifiers())) {
continue;
}
Object val = jsonObjectPropertyToJava(jbean, key, f.getType(), throwForMissingProperty);
f.set(o, val);
missingNames.remove(key);
} catch (NoSuchElementException | NoSuchFieldException e) {
// NOSONAR
// nop
}
}
}
if (missingNames.size() > 0) {
FastBeanInfo beanInfo = new FastBeanInfo(type, Object.class);
for (FastPropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
Method m = desc.getWriteMethod();
if (m == null) {
continue;
}
String key = desc.getName();
Object val = jsonObjectPropertyToJava(jbean, key, m.getParameterTypes()[0], throwForMissingProperty);
m.invoke(o, val);
missingNames.remove(key);
}
}
if (throwForMissingProperty && missingNames.size() > 0) {
throw new IllegalArgumentException("properties " + missingNames + " do not exist in " + type);
}
return o;
} catch (IllegalAccessException | InvocationTargetException | RuntimeException e) {
throw new IllegalArgumentException(jbean + " to " + type, e);
}
}
use of org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor in project scout.rt by eclipse.
the class JsonBean method toJson.
@Override
public Object toJson() {
if (m_bean == null) {
return null;
}
Class<?> type = m_bean.getClass();
// basic types
if (type.isPrimitive() || type == String.class || type == Boolean.class || Number.class.isAssignableFrom(type)) {
return m_bean;
}
// binary resource
if (BinaryResource.class.isAssignableFrom(type)) {
BinaryResource binaryResource = (BinaryResource) m_bean;
m_binaryResourceMediator.addBinaryResource(binaryResource);
return m_binaryResourceMediator.createUrl(binaryResource);
}
// array
if (type.isArray()) {
JSONArray jsonArray = new JSONArray();
int n = Array.getLength(m_bean);
for (int i = 0; i < n; i++) {
IJsonObject jsonObject = createJsonObject(Array.get(m_bean, i));
jsonArray.put(jsonObject.toJson());
}
return jsonArray;
}
// collection
if (Collection.class.isAssignableFrom(type)) {
JSONArray jsonArray = new JSONArray();
Collection collection = (Collection) m_bean;
for (Object object : collection) {
IJsonObject jsonObject = createJsonObject(object);
jsonArray.put(jsonObject.toJson());
}
return jsonArray;
}
// Map
if (Map.class.isAssignableFrom(type)) {
JSONObject jsonMap = new JSONObject();
Map map = (Map) m_bean;
@SuppressWarnings("unchecked") Set<Entry> entries = (Set<Entry>) map.entrySet();
for (Entry entry : entries) {
if (!(entry.getKey() instanceof String)) {
throw new IllegalArgumentException("Cannot convert " + type + " to json object");
}
IJsonObject jsonObject = createJsonObject(entry.getValue());
jsonMap.put((String) entry.getKey(), jsonObject.toJson());
}
return jsonMap;
}
// bean
if (type.getName().startsWith("java.")) {
throw new IllegalArgumentException("Cannot convert " + type + " to json object");
}
try {
TreeMap<String, Object> properties = new TreeMap<>();
for (Field f : type.getFields()) {
if (Modifier.isStatic(f.getModifiers())) {
continue;
}
String key = f.getName();
Object val = f.get(m_bean);
IJsonObject jsonObject = createJsonObject(val);
properties.put(key, jsonObject.toJson());
}
FastBeanInfo beanInfo = new FastBeanInfo(type, Object.class);
for (FastPropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
Method m = desc.getReadMethod();
if (m == null) {
continue;
}
// skip ignored annotated getters with context GUI
IgnoreProperty ignoredPropertyAnnotation = m.getAnnotation(IgnoreProperty.class);
if (ignoredPropertyAnnotation != null && Context.GUI.equals(ignoredPropertyAnnotation.value())) {
continue;
}
String key = desc.getName();
Object val = m.invoke(m_bean);
IJsonObject jsonObject = createJsonObject(val);
properties.put(key, jsonObject.toJson());
}
JSONObject jbean = new JSONObject();
for (Map.Entry<String, Object> e : properties.entrySet()) {
jbean.put(e.getKey(), e.getValue());
}
return jbean;
} catch (Exception e) {
throw new IllegalArgumentException(type + " to json", e);
}
}
Aggregations