private Object mapper(ResultSet rs, Class clz) throws Exception { Field[] fs = clz.getDeclaredFields(); Object obj = clz.newInstance(); for (Field f : fs) { String name = f.getName(); f.setAccessible(true); Class type = f.getType(); Object val = rs.getObject(name); if (val == null) { continue; } if (val instanceof BigDecimal) { BigDecimal tmp = (BigDecimal) val; if (type.getName().endsWith("Long")) { val = tmp.longValue(); } else if (type.getName().endsWith("Integer")) { val = tmp.intValue(); } else if (type.getName().endsWith("Short")) { val = tmp.shortValue(); } else if (type.getName().endsWith("Double")) { val = tmp.doubleValue(); } else { } } f.set(obj, val); } return obj; }
// If the Decision Table model was pre-5.4 Numeric data-types were always stored as // BigDecimals. This function attempts to set the correct DTCellValue property based // on the *true* data type. private void convertDTCellValueFromNumeric(DataType.DataTypes dataType, DTCellValue52 dcv) { // Generic type NUMERIC was always stored as a BigDecimal final BigDecimal value = (BigDecimal) dcv.getNumericValue(); switch (dataType) { case NUMERIC_BIGDECIMAL: dcv.setNumericValue(value == null ? null : value); break; case NUMERIC_BIGINTEGER: dcv.setNumericValue(value == null ? null : value.toBigInteger()); break; case NUMERIC_BYTE: dcv.setNumericValue(value == null ? null : value.byteValue()); break; case NUMERIC_DOUBLE: dcv.setNumericValue(value == null ? null : value.doubleValue()); break; case NUMERIC_FLOAT: dcv.setNumericValue(value == null ? null : value.floatValue()); break; case NUMERIC_INTEGER: dcv.setNumericValue(value == null ? null : value.intValue()); break; case NUMERIC_LONG: dcv.setNumericValue(value == null ? null : value.longValue()); break; case NUMERIC_SHORT: dcv.setNumericValue(value == null ? null : value.shortValue()); break; } }
@SuppressWarnings("unchecked") public static <T> T cast(Object obj, Class<T> clazz) throws Exception { if (obj == null) { return null; } TypeToken<T> type = TypeToken.of(clazz).wrap(); TypeToken<?> objType = TypeToken.of(obj.getClass()).wrap(); if (type.isAssignableFrom(objType)) { return (T) obj; } if (TypeToken.of(List.class).isAssignableFrom(type) && objType.isArray()) { List<Object> list = Arrays.asList((Object[]) obj); return (T) list; } if (type.isArray() && TypeToken.of(List.class).isAssignableFrom(objType)) { List<?> list = (List<?>) obj; TypeToken<?> componentType = type.getComponentType(); Class<?> rawType; if (componentType == null) { rawType = Object.class; } else { rawType = componentType.getRawType(); } Object[] array = (Object[]) Array.newInstance(rawType, list.size()); return (T) list.toArray(array); } if (clazz.isEnum()) { return (T) Enum.valueOf((Class<? extends Enum>) clazz, obj.toString()); } if (TypeToken.of(String.class).isAssignableFrom(type)) { return (T) obj.toString(); } if (TypeToken.of(Number.class).isAssignableFrom(type)) { BigDecimal num = new BigDecimal(obj.toString()); if (TypeToken.of(Integer.class).isAssignableFrom(type)) { return (T) Integer.valueOf(num.intValue()); } if (TypeToken.of(Long.class).isAssignableFrom(type)) { return (T) Long.valueOf(num.longValue()); } if (TypeToken.of(Short.class).isAssignableFrom(type)) { return (T) Short.valueOf(num.shortValue()); } if (TypeToken.of(Byte.class).isAssignableFrom(type)) { return (T) Byte.valueOf(num.byteValue()); } if (TypeToken.of(Float.class).isAssignableFrom(type)) { return (T) Float.valueOf(num.floatValue()); } if (TypeToken.of(Double.class).isAssignableFrom(type)) { return (T) Double.valueOf(num.doubleValue()); } } return null; }
public short getShort() throws SQLException { truncated_ = 0; outOfBounds_ = false; if (value_.compareTo(SHORT_MAX_VALUE) > 0 || value_.compareTo(SHORT_MIN_VALUE) < 0) { // we don't count the fractional part of the number as truncation int length = value_.toBigInteger().toByteArray().length; truncated_ = length - 2; outOfBounds_ = true; } return (short) value_.shortValue(); }
public static final short objectToShort(Object o) { if (o instanceof Number) return ((Number) o).shortValue(); try { if (o == null) return -1; else { BigDecimal bigDecimal = new BigDecimal(o.toString()); bigDecimal.intValue(); return bigDecimal.shortValue(); } } catch (NumberFormatException e) { e.printStackTrace(); return -1; } }
public Short fromBigDecimal(BigDecimal value) { return value != null ? value.shortValue() : null; }
/** * Converts a Object into a Short. Calls convertNativeToNumber by default. Override this function * if you want to handle small integers specially. */ public Short convertNativeToShort(tsColumn coldef, Object o) throws tinySQLException { BigDecimal bd = convertNativeToNumber(coldef, o); return new Short(bd.shortValue()); }