Example usage for javax.el ELResolver setValue

List of usage examples for javax.el ELResolver setValue

Introduction

In this page you can find the example usage for javax.el ELResolver setValue.

Prototype

public abstract void setValue(ELContext context, Object base, Object property, Object value);

Source Link

Usage

From source file:org.apache.myfaces.config.ManagedBeanBuilder.java

@SuppressWarnings("unchecked")
private void initializeProperties(FacesContext facesContext, ManagedBean beanConfiguration, Object bean) {
    ELResolver elResolver = facesContext.getApplication().getELResolver();
    ELContext elContext = facesContext.getELContext();

    for (ManagedProperty property : beanConfiguration.getManagedProperties()) {
        Object value = null;//from ww w  .  j a  v  a2 s. co  m

        switch (property.getType()) {
        case ManagedProperty.TYPE_LIST:

            // JSF 1.1, 5.3.1.3
            // Call the property getter, if it exists.
            // If the getter returns null or doesn't exist, create a java.util.ArrayList,
            // otherwise use the returned Object ...
            if (PropertyUtils.isReadable(bean, property.getPropertyName())) {
                value = elResolver.getValue(elContext, bean, property.getPropertyName());
            }

            value = value == null ? new ArrayList<Object>() : value;

            if (value instanceof List) {
                initializeList(facesContext, property.getListEntries(), (List<Object>) value);

            } else if (value != null && value.getClass().isArray()) {
                int length = Array.getLength(value);
                ArrayList<Object> temp = new ArrayList<Object>(length);
                for (int i = 0; i < length; i++) {
                    temp.add(Array.get(value, i));
                }
                initializeList(facesContext, property.getListEntries(), temp);
                value = Array.newInstance(value.getClass().getComponentType(), temp.size());
                length = temp.size();

                for (int i = 0; i < length; i++) {
                    Array.set(value, i, temp.get(i));
                }
            } else {
                value = new ArrayList<Object>();
                initializeList(facesContext, property.getListEntries(), (List<Object>) value);
            }

            break;
        case ManagedProperty.TYPE_MAP:

            // JSF 1.1, 5.3.1.3
            // Call the property getter, if it exists.
            // If the getter returns null or doesn't exist, create a java.util.HashMap,
            // otherwise use the returned java.util.Map .
            if (PropertyUtils.isReadable(bean, property.getPropertyName())) {
                value = elResolver.getValue(elContext, bean, property.getPropertyName());
            }
            value = value == null ? new HashMap<Object, Object>() : value;

            if (!(value instanceof Map)) {
                value = new HashMap<Object, Object>();
            }

            initializeMap(facesContext, property.getMapEntries(), (Map<Object, Object>) value);
            break;
        case ManagedProperty.TYPE_NULL:
            break;
        case ManagedProperty.TYPE_VALUE:
            // check for correct scope of a referenced bean
            if (!isInValidScope(facesContext, property, beanConfiguration)) {
                throw new FacesException("Property " + property.getPropertyName()
                        + " references object in a scope with shorter lifetime than the target scope "
                        + beanConfiguration.getManagedBeanScope());
            }
            value = property.getRuntimeValue(facesContext);
            break;
        default:
            throw new FacesException("unknown ManagedProperty type: " + property.getType());
        }

        Class<?> propertyClass = null;

        if (property.getPropertyClass() == null) {
            propertyClass = elResolver.getType(elContext, bean, property.getPropertyName());
        } else {
            propertyClass = ClassUtils.simpleJavaTypeToClass(property.getPropertyClass());
        }

        if (null == propertyClass) {
            throw new IllegalArgumentException(
                    "unable to find the type of property " + property.getPropertyName());
        }

        Object coercedValue = coerceToType(facesContext, value, propertyClass);
        elResolver.setValue(elContext, bean, property.getPropertyName(), coercedValue);
    }
}

From source file:org.apache.myfaces.ov2021.config.ManagedBeanBuilder.java

@SuppressWarnings("unchecked")
private void initializeProperties(FacesContext facesContext, ManagedBean beanConfiguration, Object bean) {
    ELResolver elResolver = facesContext.getApplication().getELResolver();
    ELContext elContext = facesContext.getELContext();

    for (ManagedProperty property : beanConfiguration.getManagedProperties()) {
        Object value = null;/*from  www  .  j  av a2 s  . co  m*/

        switch (property.getType()) {
        case ManagedProperty.TYPE_LIST:

            // JSF 1.1, 5.3.1.3
            // Call the property getter, if it exists.
            // If the getter returns null or doesn't exist, create a java.util.ArrayList,
            // otherwise use the returned Object ...
            if (PropertyUtils.isReadable(bean, property.getPropertyName())) {
                value = elResolver.getValue(elContext, bean, property.getPropertyName());
            }

            value = value == null ? new ArrayList<Object>() : value;

            if (value instanceof List) {
                initializeList(facesContext, property.getListEntries(), (List<Object>) value);

            } else if (value != null && value.getClass().isArray()) {
                int length = Array.getLength(value);
                ArrayList<Object> temp = new ArrayList<Object>(length);
                for (int i = 0; i < length; i++) {
                    temp.add(Array.get(value, i));
                }
                initializeList(facesContext, property.getListEntries(), temp);
                value = Array.newInstance(value.getClass().getComponentType(), temp.size());
                length = temp.size();

                for (int i = 0; i < length; i++) {
                    Array.set(value, i, temp.get(i));
                }
            } else {
                value = new ArrayList<Object>();
                initializeList(facesContext, property.getListEntries(), (List<Object>) value);
            }

            break;
        case ManagedProperty.TYPE_MAP:

            // JSF 1.1, 5.3.1.3
            // Call the property getter, if it exists.
            // If the getter returns null or doesn't exist, create a java.util.HashMap,
            // otherwise use the returned java.util.Map .
            if (PropertyUtils.isReadable(bean, property.getPropertyName()))
                value = elResolver.getValue(elContext, bean, property.getPropertyName());
            value = value == null ? new HashMap<Object, Object>() : value;

            if (!(value instanceof Map)) {
                value = new HashMap<Object, Object>();
            }

            initializeMap(facesContext, property.getMapEntries(), (Map<Object, Object>) value);
            break;
        case ManagedProperty.TYPE_NULL:
            break;
        case ManagedProperty.TYPE_VALUE:
            // check for correct scope of a referenced bean
            if (!isInValidScope(facesContext, property, beanConfiguration)) {
                throw new FacesException("Property " + property.getPropertyName()
                        + " references object in a scope with shorter lifetime than the target scope "
                        + beanConfiguration.getManagedBeanScope());
            }
            value = property.getRuntimeValue(facesContext);
            break;
        }

        Class<?> propertyClass = null;

        if (property.getPropertyClass() == null) {
            propertyClass = elResolver.getType(elContext, bean, property.getPropertyName());
        } else {
            propertyClass = ClassUtils.simpleJavaTypeToClass(property.getPropertyClass());
        }

        if (null == propertyClass) {
            throw new IllegalArgumentException(
                    "unable to find the type of property " + property.getPropertyName());
        }

        Object coercedValue = coerceToType(facesContext, value, propertyClass);
        elResolver.setValue(elContext, bean, property.getPropertyName(), coercedValue);
    }
}