public static T convertValue()

in wicket-util/src/main/java/org/apache/wicket/util/lang/Objects.java [295:370]


	public static <T> T convertValue(final Object value, final Class<T> toType)
	{
		Object result = null;

		if (value != null)
		{
			/* If array -> array then convert components of array individually */
			if (value.getClass().isArray() && toType.isArray())
			{
				Class<?> componentType = toType.getComponentType();

				result = Array.newInstance(componentType, Array.getLength(value));
				for (int i = 0, icount = Array.getLength(value); i < icount; i++)
				{
					Array.set(result, i, convertValue(Array.get(value, i), componentType));
				}
			}
			else
			{
				if ((toType == Integer.class) || (toType == Integer.TYPE))
				{
					result = (int)longValue(value);
				}
				if ((toType == Double.class) || (toType == Double.TYPE))
				{
					result = doubleValue(value);
				}
				if ((toType == Boolean.class) || (toType == Boolean.TYPE))
				{
					result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE;
				}
				if ((toType == Byte.class) || (toType == Byte.TYPE))
				{
					result = (byte)longValue(value);
				}
				if ((toType == Character.class) || (toType == Character.TYPE))
				{
					result = (char)longValue(value);
				}
				if ((toType == Short.class) || (toType == Short.TYPE))
				{
					result = (short)longValue(value);
				}
				if ((toType == Long.class) || (toType == Long.TYPE))
				{
					result = longValue(value);
				}
				if ((toType == Float.class) || (toType == Float.TYPE))
				{
					result = (float) doubleValue(value);
				}
				if (toType == BigInteger.class)
				{
					result = bigIntValue(value);
				}
				if (toType == BigDecimal.class)
				{
					result = bigDecValue(value);
				}
				if (toType == String.class)
				{
					result = stringValue(value);
				}
			}
		}
		else
		{
			if (toType.isPrimitive())
			{
				result = primitiveDefaults.get(toType);
			}
		}
		@SuppressWarnings("unchecked")
		T finalResult = (T)result;
		return finalResult;
	}