Example usage for javax.el ExpressionFactory coerceToType

List of usage examples for javax.el ExpressionFactory coerceToType

Introduction

In this page you can find the example usage for javax.el ExpressionFactory coerceToType.

Prototype

public abstract Object coerceToType(Object obj, Class<?> expectedType);

Source Link

Document

Coerce the supplied object to the requested type.

Usage

From source file:com.flexive.faces.FxJsfComponentUtils.java

/**
 * Coerce a value to its type/*from   w ww.  j a v a  2s.c om*/
 *
 * @param context       faces context
 * @param value         value to coerce
 * @param itemValueType value class
 * @return coerced value
 */
public static Object coerceToModelType(FacesContext context, Object value, Class itemValueType) {
    Object newValue;
    try {
        ExpressionFactory ef = context.getApplication().getExpressionFactory();
        newValue = ef.coerceToType(value, itemValueType);
    } catch (ELException ele) {
        newValue = value;
    } catch (IllegalArgumentException iae) {
        newValue = value;
    }
    return newValue;
}

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

@SuppressWarnings("unchecked")
public static <T> T coerceToType(FacesContext facesContext, Object value, Class<? extends T> desiredClass) {
    if (value == null) {
        return null;
    }// w ww  .ja  va 2s .  c  om

    try {
        ExpressionFactory expFactory = facesContext.getApplication().getExpressionFactory();
        // Use coersion implemented by JSP EL for consistency with EL
        // expressions. Additionally, it caches some of the coersions.
        return (T) expFactory.coerceToType(value, desiredClass);
    } catch (ELException e) {
        String message = "Cannot coerce " + value.getClass().getName() + " to " + desiredClass.getName();
        log.log(Level.SEVERE, message, e);
        throw new FacesException(message, e);
    }
}

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

@SuppressWarnings("unchecked")
public static <T> T coerceToType(FacesContext facesContext, Object value, Class<? extends T> desiredClass) {
    if (value == null)
        return null;

    try {//  w w  w  .  ja  v  a  2s .  c  o m
        ExpressionFactory expFactory = facesContext.getApplication().getExpressionFactory();
        // Use coersion implemented by JSP EL for consistency with EL
        // expressions. Additionally, it caches some of the coersions.
        return (T) expFactory.coerceToType(value, desiredClass);
    } catch (ELException e) {
        String message = "Cannot coerce " + value.getClass().getName() + " to " + desiredClass.getName();
        log.log(Level.SEVERE, message, e);
        throw new FacesException(message, e);
    }
}