public static Object convertToType(Object value, Class<?> valueType, Logger logger) {
        // skip null
        if (value == null) {
            return null;
        }

        // its already the same type
        if (valueType.isAssignableFrom(value.getClass())) {
            return value;
        }

        FacesContext context = FacesContext.getCurrentInstance();

        // primivites dont need complex conversion, try the ELContext first
        if (BeanUtils.isPrimitiveOrPrimitiveWrapper(valueType)) {
            try {
                return context.getELContext().convertToType(value, valueType);
            }
            catch (ELException e) {
                logger.log(Level.INFO, e, () -> "Could not convert '" + value + "' to " + valueType + " via ELContext!");
            }
        }

        Converter targetConverter = context.getApplication().createConverter(valueType);
        if (targetConverter == null) {
            logger.log(Level.FINE, () -> "Skip conversion as no converter was found for " + valueType
                    + "; Create a Converter for it!");
            return value;
        }

        Converter sourceConverter = context.getApplication().createConverter(value.getClass());
        if (sourceConverter == null) {
            logger.log(Level.FINE, () -> "Skip conversion as no converter was found for " + value.getClass()
                    + "; Create a Converter for it!");
        }

        // first convert the object to string
        String stringValue = sourceConverter == null
                ? value.toString()
                : sourceConverter.getAsString(context, UIComponent.getCurrentComponent(context), value);

        // now convert the string to the required target
        try {
            return targetConverter.getAsObject(context, UIComponent.getCurrentComponent(context), stringValue);
        }
        catch (ConverterException e) {
            logger.log(Level.INFO, e, () -> "Could not convert '" + stringValue + "' to " + valueType + " via " + targetConverter.getClass().getName());
            return value;
        }
    }
Execute a callback in a stateless scope by temporarily removing and restoring a variable from the FacesContext attributes.

@param context The FacesContext used for accessing attributes
@param var The variable to be temporarily removed and restored in the context attributes
@param runnable The callback to be executed
@param <E> The type of Throwable that may be thrown
@throws E If the callback throws an exception of type E
primefaces/src/main/java/org/primefaces/util/ComponentUtils.java