public static final Object convertType()

in ti/phase2/jars/core/src/java/org/apache/ti/script/el/util/ParseUtils.java [131:201]


    public static final Object convertType(Object value, Class toType) {
        assert toType != null;

        try {
            boolean sourceIsArray = false;

            /* only convert String types; other Object types are already assumed to be in their target types. */
            if (value != null && !(value instanceof String || (sourceIsArray = (value instanceof String[]))))
                return value;

            /* for a String[], convert each item in the array into its target type and return the resulting array. */
            if (toType.isArray()) {
                if (value == null)
                    return null;
                else {
                    Class compType = toType.getComponentType();

                    String[] strs = null;
                    if (value.getClass().isArray())
                        strs = (String[]) value;
                    else
                        strs = new String[]{(String) value};

                    Object tgt = Array.newInstance(compType, strs.length);

                    for (int i = 0; i < strs.length; i++) {
                        Object o = null;
                        try {
                            /* todo: support getting the Locale here in an ExpressionContext object */
                            o = TypeUtils.convertToObject(strs[i], compType);
                        } catch (IllegalArgumentException e) {
                            String msg = "Can not set Object types via expressions that are not supported by the set of registered type converters.  Cause: " + e;
                            LOGGER.error(msg, e);
                            throw new RuntimeException(msg, e);
                        }

                        Array.set(tgt, i, o);
                    }

                    return tgt;
                }
            }
            // convert the String into its target type and return the result
            else {
                // If the "value" is multi-valued (String[]), it needs to be converted into a single-valued object.
                // There is no policy defined for how we do this right now, so the first one will always win when 
                // multiple expressions reference the same property.  When that property is a String type, the result
                // is an HttpServletRequest that contains a String[], and here, we'll always the String[0].
                if (sourceIsArray) {
                    assert value instanceof String[];
                    assert Array.getLength(value) > 0 && Array.getLength(value) - 1 >= 0;

                    value = Array.get(value, Array.getLength(value) - 1);
                }

                try {
                    assert value == null || value instanceof String;

                    return TypeUtils.convertToObject((String) value, toType);
                } catch (IllegalArgumentException e) {
                    String msg = "The type \"" + toType.getName() + "\" can not be set through the NetUI expression language.";
                    LOGGER.error(msg, e);
                    throw new RuntimeException(msg, e);
                }
            }
        } catch (Exception e) {
            String msg = "Unable to convert a value of type \"" + value.getClass() + "\" to the array element type of \"" + toType + "\".  Cause: " + e;
            LOGGER.error(msg, e);
            throw new RuntimeException(msg, e);
        }
    }