// Integer
if (targetClass == Integer.class || targetClass == int.class) {
long longValue = value.longValue();
if (longValue > Integer.MAX_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too large for "
+ targetClass.getName());
}
if (longValue < Integer.MIN_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too small "
+ targetClass.getName());
}
return Integer.valueOf(value.intValue());
}
// Long
if (targetClass == Long.class || targetClass == long.class) {
return Long.valueOf(value.longValue());
}
// Boolean
if (targetClass == Boolean.class || targetClass == boolean.class) {
long longValue = value.longValue();
return Boolean.valueOf(longValue > 0 ? true : false);
}
// Byte
if (targetClass == Byte.class || targetClass == byte.class) {
long longValue = value.longValue();
if (longValue > Byte.MAX_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too large for "
+ targetClass.getName());
}
if (longValue < Byte.MIN_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too small "
+ targetClass.getName());
}
return Byte.valueOf(value.byteValue());
}
// Double
if (targetClass == Double.class || targetClass == double.class) {
return Double.valueOf(value.doubleValue());
}
// BigDecimal
if (targetClass == BigDecimal.class) {
if (value instanceof Float || value instanceof Double) {
return new BigDecimal(value.toString());
} else if (value instanceof BigInteger) {
return new BigDecimal((BigInteger) value);
} else {
return BigDecimal.valueOf(value.longValue());
}
}
// BigInteger
if (targetClass == BigInteger.class) {
if (value instanceof BigDecimal) {
return ((BigDecimal) value).toBigInteger();
} else {
return BigInteger.valueOf(value.longValue());
}
}
// Short
if (targetClass == Short.class || targetClass == short.class) {
long longValue = value.longValue();
if (longValue > Short.MAX_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too large for "
+ targetClass.getName());
}
if (longValue < Short.MIN_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too small "
+ targetClass.getName());
}
return Short.valueOf(value.shortValue());
}
// Float
if (targetClass == Float.class || targetClass == float.class) {
double doubleValue = value.doubleValue();
if (doubleValue > Float.MAX_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too large for "
+ targetClass.getName());
}
if (doubleValue < Float.MIN_VALUE) {
throw new BeanMappingException(srcClass.getName() + " value '" + value + "' is too small for "
+ targetClass.getName());
}
return Float.valueOf(value.floatValue());
}
// Character
if (targetClass == Character.class || targetClass == char.class) {
long longValue = value.longValue();
// Character没有很明显的值上下边界,直接依赖于jvm的转型
return Character.valueOf((char) longValue);
}
throw new BeanMappingException("Unsupported convert: [" + srcClass.getName() + "," + targetClass.getName()
+ "]");
}