Search in sources :

Example 1 with DynamicEnum

use of io.leopard.lang.inum.daynamic.DynamicEnum in project leopard by tanhaichao.

the class LeopardBeanPropertyRowMapper method getColumnValue.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object getColumnValue(ResultSet rs, int index, ResultSetMetaData resultSetMetaData, String columnName, Field field) throws SQLException {
    Class<?> requiredType = field.getType();
    // JdbcUtils.getResultSetValue(rs, index, requiredType);// TODO 20171012 这是多余的代码吧?
    Object value;
    if (String.class.equals(requiredType)) {
        value = rs.getString(index);
    } else if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) {
        value = rs.getBoolean(index);
    } else if (byte.class.equals(requiredType) || Byte.class.equals(requiredType)) {
        value = rs.getByte(index);
    } else if (short.class.equals(requiredType) || Short.class.equals(requiredType)) {
        value = rs.getShort(index);
    } else if (int.class.equals(requiredType) || Integer.class.equals(requiredType)) {
        value = rs.getInt(index);
    } else if (long.class.equals(requiredType) || Long.class.equals(requiredType)) {
        value = rs.getLong(index);
    } else if (float.class.equals(requiredType) || Float.class.equals(requiredType)) {
        value = rs.getFloat(index);
    } else if (double.class.equals(requiredType) || Double.class.equals(requiredType) || Number.class.equals(requiredType)) {
        value = rs.getDouble(index);
    } else if (byte[].class.equals(requiredType)) {
        value = rs.getBytes(index);
    } else if (java.sql.Timestamp.class.equals(requiredType) || java.util.Date.class.equals(requiredType)) {
        value = rs.getTimestamp(index);
    } else if (java.util.Date.class.isAssignableFrom(requiredType)) {
        Timestamp timestamp = rs.getTimestamp(index);
        if (timestamp == null) {
            return null;
        }
        Date date;
        try {
            date = (Date) requiredType.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(requiredType.getName() + "没有默认构造方法.", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(requiredType.getName() + "默认构造方法实例化失败.", e);
        }
        date.setTime(timestamp.getTime());
        value = date;
    } else if (List.class.equals(requiredType)) {
        // System.out.println("name:" + field.getName() + " json:" + json);
        ParameterizedType type = (ParameterizedType) field.getGenericType();
        String elementClassName = type.getActualTypeArguments()[0].getTypeName();
        if (int.class.getName().equals(elementClassName) || Integer.class.getName().equals(elementClassName)) {
            String json = rs.getString(index);
            value = Json.toListObject(json, Integer.class);
        } else if (long.class.getName().equals(elementClassName) || Long.class.getName().equals(elementClassName)) {
            String json = rs.getString(index);
            value = Json.toListObject(json, Long.class);
        } else if (String.class.getName().equals(elementClassName)) {
            String json = rs.getString(index);
            value = Json.toListObject(json, String.class);
        } else {
            Class<?> elementType;
            try {
                elementType = Class.forName(elementClassName);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
            if (elementType.isEnum()) {
                String json = rs.getString(index);
                return toEnumList(json, elementType, field);
            } else {
                Class<?> clazz = (Class<?>) type.getActualTypeArguments()[0];
                String json = rs.getString(index);
                // System.out.println("clazz:" + clazz.getName());
                value = Json.toListObject(json, clazz, true);
            // throw new IllegalArgumentException("未知数据类型[" +
            // elementClassName + "].");
            }
        }
    } else if (requiredType.isEnum()) {
        if (Snum.class.isAssignableFrom(requiredType)) {
            String key = rs.getString(index);
            if (key == null) {
                return null;
            }
            try {
                return onumResolver.toEnum(key, (Class<? extends Enum>) requiredType, field);
            // return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
            } catch (EnumConstantInvalidException e) {
                if ("".equals(key)) {
                    return null;
                }
                throw e;
            }
        } else if (Inum.class.isAssignableFrom(requiredType)) {
            int key = rs.getInt(index);
            return onumResolver.toEnum(key, (Class<? extends Enum>) requiredType, field);
        // return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
        } else if (Bnum.class.isAssignableFrom(requiredType)) {
            byte key = rs.getByte(index);
            return onumResolver.toEnum(key, (Class<? extends Enum>) requiredType, field);
        // return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
        } else {
            throw new RuntimeException("未知枚举类型[" + requiredType.getName() + "].");
        }
    } else if (DynamicEnum.class.isAssignableFrom(requiredType)) {
        if (Snum.class.isAssignableFrom(requiredType)) {
            String key = rs.getString(index);
            if (key == null) {
                return null;
            }
            try {
                return DynamicEnumUtil.toEnum(key, (Class<? extends DynamicEnum>) requiredType);
            } catch (EnumConstantInvalidException e) {
                // e.printStackTrace();
                if ("".equals(key)) {
                    return null;
                }
                throw e;
            }
        } else if (Inum.class.isAssignableFrom(requiredType)) {
            int key = rs.getInt(index);
            // return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
            return DynamicEnumUtil.toEnum(key, (Class<? extends DynamicEnum>) requiredType);
        } else if (Bnum.class.isAssignableFrom(requiredType)) {
            byte key = rs.getByte(index);
            // return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
            return DynamicEnumUtil.toEnum(key, (Class<? extends DynamicEnum>) requiredType);
        } else {
            throw new RuntimeException("未知枚举类型[" + requiredType.getName() + "].");
        }
    } else if (Object.class.equals(requiredType)) {
        throw new RuntimeException("实体类的字段类型不能使用Object.");
    } else {
        String json = rs.getString(index);
        try {
            value = Json.toObject(json, requiredType, true);
        } catch (JsonException e) {
            System.err.println("JsonException fieldName:" + field.getName() + " " + requiredType.getName() + " json:" + json);
            throw e;
        }
    // throw new SQLException("未知数据类型[" + requiredType.getName() +
    // "].");
    }
    return value;
}
Also used : DynamicEnum(io.leopard.lang.inum.daynamic.DynamicEnum) JsonException(io.leopard.json.JsonException) Timestamp(java.sql.Timestamp) ParameterizedType(java.lang.reflect.ParameterizedType) ArrayList(java.util.ArrayList) List(java.util.List) Bnum(io.leopard.lang.inum.Bnum) Date(java.util.Date) Snum(io.leopard.lang.inum.Snum) DynamicEnum(io.leopard.lang.inum.daynamic.DynamicEnum) EnumConstantInvalidException(io.leopard.lang.inum.EnumConstantInvalidException) Inum(io.leopard.lang.inum.Inum)

Aggregations

JsonException (io.leopard.json.JsonException)1 Bnum (io.leopard.lang.inum.Bnum)1 EnumConstantInvalidException (io.leopard.lang.inum.EnumConstantInvalidException)1 Inum (io.leopard.lang.inum.Inum)1 Snum (io.leopard.lang.inum.Snum)1 DynamicEnum (io.leopard.lang.inum.daynamic.DynamicEnum)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1