in trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/share/expl/Coercions.java [209:304]
public static Object coerce(Object value, Class<?> type)
{
if (type != null)
{
if (value == null)
return null;
if (type.isAssignableFrom(value.getClass()))
return value;
if (type == Object.class)
{
return value;
}
// Turn the type into a Boolean
else if ((type == Boolean.class) || (type == Boolean.TYPE))
{
return toBoolean(value);
}
else if ((type == Byte.class) || (type == Byte.TYPE))
{
return toByte(value);
}
else if ((type == Short.class) || (type == Short.TYPE))
{
return toShort(value);
}
else if ((type == Integer.class) || (type == Integer.TYPE))
{
return toInteger(value);
}
else if ((type == Long.class) || (type == Long.TYPE))
{
return toLong(value);
}
else if ((type == Float.class) || (type == Float.TYPE))
{
return toFloat(value);
}
else if ((type == Double.class) || (type == Double.TYPE))
{
return toDouble(value);
}
else if ((type == Character.class) || (type == Character.TYPE))
{
return toCharacter(value);
}
else if (type == String.class)
{
return toString(value);
}
else if ((type == Number.class))
{
return toNumber(toString(value));
}
else if (type.isArray()) // see bug 3234064.
{
// we support three types of array coercions.
// 1. coerce a List into an array.
// 2. coerce a single value into an array of size 1.
// 3. coerce an array into an array.
Class<?> arrayType = type.getComponentType();
Object res = Array.newInstance(arrayType, 1);
if (value instanceof List)
{
// we probably should coerce each element of this list; however,
// let's not worry about it until we have a requirement:
res = ((List<Object>) value).toArray((Object[]) res);
}
else if (value.getClass().isArray())
{
// we probably should coerce each element of this array; however,
// let's not worry about it until we have a requirement:
res = value;
}
else
{
Object arrayValue = coerce(value, arrayType);
Array.set(res, 0, arrayValue);
}
return res;
}
else if (Enum.class.isAssignableFrom(type))
{
return Enum.valueOf((Class<? extends Enum>) type, value.toString());
}
throw new IllegalArgumentException(_LOG.getMessage(
"CANNOT_COERCE_VALUE_OF_TYPE", new Object[]{value.getClass(), type.getName()}));
}
throw new NullPointerException(_LOG.getMessage(
"NULL_TYPE"));
}