Search in sources :

Example 6 with FastPropertyDescriptor

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

the class AbstractMemoryPolicy method createIdForPage.

private void createIdForPage(StringBuilder b, IPage<?> page, Object o) {
    b.append("/");
    b.append(page.getClass().getName());
    if (page.getUserPreferenceContext() != null) {
        b.append("/");
        b.append(page.getUserPreferenceContext());
    }
    if (o != null) {
        b.append("/");
        b.append(o.getClass().getName());
    }
    FastBeanInfo pi = new FastBeanInfo(page.getClass(), page.getClass().getSuperclass());
    for (FastPropertyDescriptor prop : pi.getPropertyDescriptors()) {
        if (prop.getReadMethod() != null && (Date.class.isAssignableFrom(prop.getPropertyType()) || Number.class.isAssignableFrom(prop.getPropertyType()) || String.class.isAssignableFrom(prop.getPropertyType()) || long.class.isAssignableFrom(prop.getPropertyType()))) {
            // only accept Numbers, Strings or Dates
            try {
                b.append("/");
                b.append(prop.getName());
                b.append("=");
                b.append(prop.getReadMethod().invoke(page, new Object[0]));
            } catch (Exception e) {
                LOG.error("Error reading property {}", prop, e);
            // nop - ignore this property
            }
        }
    }
}
Also used : FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) Date(java.util.Date) FastBeanInfo(org.eclipse.scout.rt.platform.reflect.FastBeanInfo)

Example 7 with FastPropertyDescriptor

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

the class StatementProcessor method createOutputRec.

@SuppressWarnings({ "unchecked", "squid:S2583", "squid:S138" })
private IBindOutput createOutputRec(ValueOutputToken bindToken, String[] path, final Object bindBase) {
    boolean terminal = (path.length == 1);
    Object o = null;
    boolean found = false;
    if (bindBase instanceof Map) {
        // handle all terminal cases for map
        o = ((Map) bindBase).get(path[0]);
        if (o != null) {
            found = true;
        } else if (((Map) bindBase).containsKey(path[0])) {
            found = true;
        }
        if (found) {
            if (o instanceof ITableBeanHolder) {
                ITableBeanHolder table = (ITableBeanHolder) o;
                return new TableBeanHolderOutput(table, path[1], bindToken);
            } else if (o instanceof IBeanArrayHolder) {
                IBeanArrayHolder holder = (IBeanArrayHolder) o;
                return new BeanArrayHolderOutput(holder, path[1], bindToken);
            } else if (o instanceof IHolder) {
                if (terminal) {
                    return createOutputTerminal((IHolder) o, bindToken);
                } else {
                    o = ((IHolder) o).getValue();
                }
            } else if (o == null) {
                if (terminal) {
                    return new MapOutput((Map) bindBase, path[0], bindToken);
                } else {
                    throw new ProcessingException("output bind {} resolves to null on path element: {}", bindToken, path[0]);
                }
            } else {
                if (terminal) {
                    return new MapOutput((Map) bindBase, path[0], bindToken);
                }
            }
        }
    } else if (bindBase instanceof NVPair) {
        // handle all terminal cases for nvpair
        if (((NVPair) bindBase).getName().equals(path[0])) {
            o = ((NVPair) bindBase).getValue();
            found = true;
            if (o instanceof ITableBeanHolder) {
                ITableBeanHolder table = (ITableBeanHolder) o;
                return new TableBeanHolderOutput(table, path[1], bindToken);
            } else if (o instanceof IBeanArrayHolder) {
                IBeanArrayHolder holder = (IBeanArrayHolder) o;
                return new BeanArrayHolderOutput(holder, path[1], bindToken);
            } else if (o instanceof IHolder) {
                if (terminal) {
                    return createOutputTerminal((IHolder) o, bindToken);
                } else {
                    o = ((IHolder) o).getValue();
                }
            } else if (o == null) {
                throw new ProcessingException("output bind {} resolves to null on path element: {}", bindToken, path[0]);
            } else {
                if (terminal) {
                    throw new ProcessingException("output bind {} is not a valid output container", bindToken);
                }
            }
        }
    } else if (bindBase instanceof ITableBeanHolder) {
        // handle all terminal cases for table holder
        ITableBeanHolder table = (ITableBeanHolder) bindBase;
        try {
            Method m = table.getRowType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new TableBeanHolderOutput(table, path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t) {
            // nop
            found = false;
        }
    } else if (bindBase instanceof IBeanArrayHolder) {
        // handle all terminal cases for BeanArrayHolder
        IBeanArrayHolder holder = (IBeanArrayHolder) bindBase;
        try {
            Method m = holder.getHolderType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new BeanArrayHolderOutput(holder, path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t1) {
            try {
                Method m = holder.getHolderType().getMethod("is" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
                if (m != null) {
                    found = true;
                    return new BeanArrayHolderOutput(holder, path[0], bindToken);
                }
            } catch (NoSuchMethodException | SecurityException t2) {
                found = false;
            // nop
            }
        }
    } else /* bean property */
    {
        // handle all terminal cases for bean property
        try {
            FastPropertyDescriptor pd = BeanUtility.getFastBeanInfo(bindBase.getClass(), null).getPropertyDescriptor(path[0]);
            if (terminal) {
                Method setter = pd != null ? pd.getWriteMethod() : null;
                if (setter != null) {
                    found = true;
                    return new AbstractBeanPropertyOutput(bindBase.getClass(), path[0], bindToken) {

                        @Override
                        protected Object[] getFinalBeanArray() {
                            return new Object[] { bindBase };
                        }
                    };
                } else {
                    Method getter = pd != null ? pd.getReadMethod() : null;
                    if (getter != null) {
                        o = getter.invoke(bindBase, (Object[]) null);
                        if (o instanceof ITableBeanHolder) {
                            throw new ProcessingException("output bind '{}' is a table bean and should not be a terminal", bindToken.getName());
                        } else if (o instanceof IBeanArrayHolder) {
                            throw new ProcessingException("output bind '{}' is a bean array and should not be a terminal", bindToken.getName());
                        } else if (o instanceof IHolder) {
                            return createOutputTerminal((IHolder) o, bindToken);
                        } else {
                            return null;
                        }
                    }
                }
            } else {
                Method getter = pd != null ? pd.getReadMethod() : null;
                if (getter != null) {
                    Object readValue = getter.invoke(bindBase, (Object[]) null);
                    o = readValue;
                    found = true;
                }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            LOG.warn("Exception while invoking bean getter", e);
        }
    }
    // 
    if (found) {
        if (terminal) {
            throw new ProcessingException("output bind '{}' was not recognized as a terminal", bindToken.getName());
        }
        // continue
        String[] newPath = new String[path.length - 1];
        System.arraycopy(path, 1, newPath, 0, newPath.length);
        return createOutputRec(bindToken, newPath, o);
    } else {
        return null;
    }
}
Also used : IHolder(org.eclipse.scout.rt.platform.holders.IHolder) Method(java.lang.reflect.Method) ITableBeanHolder(org.eclipse.scout.rt.platform.holders.ITableBeanHolder) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) Map(java.util.Map) TreeMap(java.util.TreeMap) IBeanArrayHolder(org.eclipse.scout.rt.platform.holders.IBeanArrayHolder) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 8 with FastPropertyDescriptor

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

the class AbstractBeanPropertyOutput method finishBatch.

@Override
public void finishBatch() {
    FastPropertyDescriptor desc = null;
    Object[] beans = getFinalBeanArray();
    if (beans != null) {
        int accSize = m_accumulator.size();
        for (int i = 0; i < beans.length; i++) {
            try {
                Object bean = beans[i];
                if (bean != null) {
                    if (desc == null) {
                        desc = BeanUtility.getFastBeanInfo(bean.getClass(), null).getPropertyDescriptor(m_propertyName);
                    }
                    Object value = null;
                    if (i < accSize) {
                        value = m_accumulator.get(i);
                    }
                    if (IHolder.class.isAssignableFrom(desc.getPropertyType())) {
                        @SuppressWarnings("unchecked") IHolder<Object> h = (IHolder<Object>) desc.getReadMethod().invoke(bean);
                        if (h != null) {
                            Object castValue = TypeCastUtility.castValue(value, h.getHolderType());
                            h.setValue(castValue);
                        }
                    } else {
                        Object castValue = TypeCastUtility.castValue(value, desc.getPropertyType());
                        desc.getWriteMethod().invoke(bean, castValue);
                    }
                }
            } catch (Exception e) {
                throw new ProcessingException("property " + m_propertyName, e);
            }
        }
    }
}
Also used : IHolder(org.eclipse.scout.rt.platform.holders.IHolder) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 9 with FastPropertyDescriptor

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

the class AbstractForm method storeToXml.

@Override
public void storeToXml(Element root) {
    root.setAttribute("formId", getFormId());
    root.setAttribute("formQname", getClass().getName());
    // add custom properties
    Element xProps = root.getOwnerDocument().createElement("properties");
    root.appendChild(xProps);
    IPropertyFilter filter = new IPropertyFilter() {

        @Override
        public boolean accept(FastPropertyDescriptor descriptor) {
            if (descriptor.getPropertyType().isInstance(IFormField.class)) {
                return false;
            }
            if (!descriptor.getPropertyType().isPrimitive() && !Serializable.class.isAssignableFrom(descriptor.getPropertyType())) {
                return false;
            }
            if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null) {
                return false;
            }
            return true;
        }
    };
    Map<String, Object> props = BeanUtility.getProperties(this, AbstractForm.class, filter);
    storePropertiesToXml(xProps, props);
    // add extension properties
    for (IExtension<?> ex : getAllExtensions()) {
        Map<String, Object> extensionProps = BeanUtility.getProperties(ex, AbstractFormExtension.class, filter);
        if (extensionProps.isEmpty()) {
            continue;
        }
        Element xExtension = root.getOwnerDocument().createElement("extension");
        xProps.appendChild(xExtension);
        xExtension.setAttribute("extensionId", ex.getClass().getSimpleName());
        xExtension.setAttribute("extensionQname", ex.getClass().getName());
        storePropertiesToXml(xExtension, extensionProps);
    }
    // add fields
    final Element xFields = root.getOwnerDocument().createElement("fields");
    root.appendChild(xFields);
    final Holder<RuntimeException> exceptionHolder = new Holder<>(RuntimeException.class);
    final Holder<PlatformError> errorHolder = new Holder<>(PlatformError.class);
    P_AbstractCollectingFieldVisitor v = new P_AbstractCollectingFieldVisitor() {

        @Override
        public boolean visitField(IFormField field, int level, int fieldIndex) {
            if (field.getForm() != AbstractForm.this) {
                // field is part of a wrapped form and is handled by the AbstractWrappedFormField
                return true;
            }
            Element xField = xFields.getOwnerDocument().createElement("field");
            try {
                field.storeToXml(xField);
                xFields.appendChild(xField);
            } catch (RuntimeException e) {
                exceptionHolder.setValue(e);
                return false;
            } catch (PlatformError e) {
                errorHolder.setValue(e);
                return false;
            }
            return true;
        }
    };
    visitFields(v);
    if (exceptionHolder.getValue() != null) {
        throw exceptionHolder.getValue();
    } else if (errorHolder.getValue() != null) {
        throw errorHolder.getValue();
    }
}
Also used : IHtmlListElement(org.eclipse.scout.rt.platform.html.IHtmlListElement) Element(org.w3c.dom.Element) IHolder(org.eclipse.scout.rt.platform.holders.IHolder) IPropertyHolder(org.eclipse.scout.rt.shared.data.form.IPropertyHolder) Holder(org.eclipse.scout.rt.platform.holders.Holder) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) IFormField(org.eclipse.scout.rt.client.ui.form.fields.IFormField) PlatformError(org.eclipse.scout.rt.platform.exception.PlatformError) IExtensibleObject(org.eclipse.scout.rt.shared.extension.IExtensibleObject) IPropertyFilter(org.eclipse.scout.rt.platform.reflect.IPropertyFilter)

Example 10 with FastPropertyDescriptor

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

the class StatementProcessor method createInputRec.

@SuppressWarnings({ "squid:S2583", "squid:S138" })
private IBindInput createInputRec(ValueInputToken bindToken, String[] path, Object bindBase, Class nullType) {
    boolean terminal = (path.length == 1);
    Object o = null;
    boolean found = false;
    if (bindBase instanceof Map) {
        // handle all terminal cases for map
        o = ((Map) bindBase).get(path[0]);
        if (o != null) {
            found = true;
        } else if (((Map) bindBase).containsKey(path[0])) {
            found = true;
        }
        if (found) {
            if (o instanceof ITableBeanHolder) {
                return new TableBeanHolderInput((ITableBeanHolder) o, null, path[1], bindToken);
            } else if (o instanceof TableBeanHolderFilter) {
                return new TableBeanHolderInput(((TableBeanHolderFilter) o).getTableBeanHolder(), ((TableBeanHolderFilter) o).getFilteredRows(), path[1], bindToken);
            } else if (o instanceof IBeanArrayHolder) {
                return new BeanArrayHolderInput((IBeanArrayHolder) o, null, path[1], bindToken);
            } else if (o instanceof BeanArrayHolderFilter) {
                return new BeanArrayHolderInput(((BeanArrayHolderFilter) o).getBeanArrayHolder(), ((BeanArrayHolderFilter) o).getFilteredBeans(), path[1], bindToken);
            } else {
                if (terminal) {
                    return createInputTerminal(o, nullType, bindToken);
                } else {
                    if (o == null) {
                        throw new ProcessingException("input bind {} resolves to null on path element: {}", bindToken, path[0]);
                    }
                }
            }
        }
    } else if (bindBase instanceof NVPair) {
        // handle all terminal cases for nvpair
        if (((NVPair) bindBase).getName().equals(path[0])) {
            o = ((NVPair) bindBase).getValue();
            found = true;
            if (o instanceof ITableBeanHolder) {
                return new TableBeanHolderInput((ITableBeanHolder) o, null, path[1], bindToken);
            } else if (o instanceof TableBeanHolderFilter) {
                return new TableBeanHolderInput(((TableBeanHolderFilter) o).getTableBeanHolder(), ((TableBeanHolderFilter) o).getFilteredRows(), path[1], bindToken);
            } else if (o instanceof IBeanArrayHolder) {
                return new BeanArrayHolderInput((IBeanArrayHolder) o, null, path[1], bindToken);
            } else if (o instanceof BeanArrayHolderFilter) {
                return new BeanArrayHolderInput(((BeanArrayHolderFilter) o).getBeanArrayHolder(), ((BeanArrayHolderFilter) o).getFilteredBeans(), path[1], bindToken);
            } else {
                if (terminal) {
                    return createInputTerminal(o, nullType, bindToken);
                } else {
                    if (o == null) {
                        throw new ProcessingException("input bind {} resolves to null on path element: {}", bindToken, path[0]);
                    }
                }
            }
        }
    } else if (bindBase instanceof ITableBeanHolder) {
        // handle all terminal cases for table holder
        ITableBeanHolder table = (ITableBeanHolder) bindBase;
        try {
            Method m = table.getRowType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new TableBeanHolderInput(table, null, path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t) {
            found = false;
        // nop
        }
    } else if (bindBase instanceof TableBeanHolderFilter) {
        // handle all terminal cases for table holder filter
        TableBeanHolderFilter filter = (TableBeanHolderFilter) bindBase;
        ITableBeanHolder table = filter.getTableBeanHolder();
        try {
            Method m = table.getRowType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new TableBeanHolderInput(table, filter.getFilteredRows(), path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t) {
            // nop
            found = false;
        }
    } else if (bindBase instanceof IBeanArrayHolder) {
        // handle all terminal cases for BeanArrayHolder
        IBeanArrayHolder<?> holder = (IBeanArrayHolder) bindBase;
        try {
            Method m = holder.getHolderType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new BeanArrayHolderInput(holder, null, path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t1) {
            try {
                Method m = holder.getHolderType().getMethod("is" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
                if (m != null) {
                    found = true;
                    return new BeanArrayHolderInput(holder, null, path[0], bindToken);
                }
            } catch (NoSuchMethodException | SecurityException t2) {
                found = false;
            // nop
            }
        }
    } else if (bindBase instanceof BeanArrayHolderFilter) {
        // handle all terminal cases for table holder filter
        BeanArrayHolderFilter filter = (BeanArrayHolderFilter) bindBase;
        IBeanArrayHolder<?> holder = filter.getBeanArrayHolder();
        try {
            Method m = holder.getHolderType().getMethod("get" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
            if (m != null) {
                found = true;
                return new BeanArrayHolderInput(holder, filter.getFilteredBeans(), path[0], bindToken);
            }
        } catch (NoSuchMethodException | SecurityException t1) {
            try {
                Method m = holder.getHolderType().getMethod("is" + Character.toUpperCase(path[0].charAt(0)) + path[0].substring(1));
                if (m != null) {
                    found = true;
                    return new BeanArrayHolderInput(holder, null, path[0], bindToken);
                }
            } catch (NoSuchMethodException | SecurityException t2) {
                found = false;
            // nop
            }
        }
    } else if (bindBase != null) {
        if (bindBase.getClass().isArray() && terminal) {
            return new BeanPropertyInput(path[0], (Object[]) bindBase, bindToken);
        }
        if (bindBase instanceof Collection && terminal) {
            return new BeanPropertyInput(path[0], ((Collection) bindBase).toArray(), bindToken);
        }
        /* bean property */
        try {
            Object propertyBean = bindBase;
            FastPropertyDescriptor pd = BeanUtility.getFastBeanInfo(propertyBean.getClass(), null).getPropertyDescriptor(path[0]);
            Method getter = pd != null ? pd.getReadMethod() : null;
            if (getter != null) {
                // getter exists
                o = getter.invoke(propertyBean);
                found = true;
                if (terminal) {
                    return createInputTerminal(o, getter.getReturnType(), bindToken);
                } else {
                    if (o == null) {
                        throw new ProcessingException("input bind {} resolves to null on path element: {}", bindToken, path[0]);
                    }
                }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            LOG.warn("Exception while invoking bean getter", e);
            LOG.debug("Cannot access property.", e);
        }
    }
    // 
    if (found) {
        if (terminal) {
            throw new ProcessingException("input bind '{}' was not recognized as a terminal", bindToken.getName());
        }
        // continue
        String[] newPath = new String[path.length - 1];
        System.arraycopy(path, 1, newPath, 0, newPath.length);
        IBindInput input = null;
        if (o instanceof IHolder<?>) {
            /* dereference value if current object is an IHolder. If input cannot be resolved (i.e. ProcessingException occurs),
         * search given property names on holder. Hence both of the following forms are supported on holder types:
         * :holder.property
         * :holder.value.property
         */
            try {
                input = createInputRec(bindToken, newPath, ((IHolder) o).getValue(), nullType);
            } catch (RuntimeException pe) {
            // nop, see below
            }
        }
        if (input == null) {
            // strict search without dereferncing holder objects
            input = createInputRec(bindToken, newPath, o, nullType);
        }
        return input;
    } else {
        return null;
    }
}
Also used : IHolder(org.eclipse.scout.rt.platform.holders.IHolder) FastPropertyDescriptor(org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor) TableBeanHolderFilter(org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) BeanArrayHolderFilter(org.eclipse.scout.rt.platform.holders.BeanArrayHolderFilter) Method(java.lang.reflect.Method) ITableBeanHolder(org.eclipse.scout.rt.platform.holders.ITableBeanHolder) InvocationTargetException(java.lang.reflect.InvocationTargetException) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) Collection(java.util.Collection) Map(java.util.Map) TreeMap(java.util.TreeMap) IBeanArrayHolder(org.eclipse.scout.rt.platform.holders.IBeanArrayHolder)

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