Search in sources :

Example 1 with EnumConstantInvalidException

use of io.leopard.lang.inum.EnumConstantInvalidException 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)

Example 2 with EnumConstantInvalidException

use of io.leopard.lang.inum.EnumConstantInvalidException in project leopard by tanhaichao.

the class WeixinPayClientImpl method micropayForStatus.

@Override
public WeixinMicropayStatus micropayForStatus(String outTradeNo, String scene, String authCode, String subject, double amount, String spbillCreateIp) throws WeixinPayException {
    try {
        WxPayMicropayResult result = this.micropay(outTradeNo, scene, authCode, subject, amount, spbillCreateIp);
        Json.print(result, "result");
        return WeixinMicropayStatus.SUCCESS;
    } catch (WeixinPayException e) {
        logger.error(e.getMessage(), e);
        String errCode = e.getErrCode().toLowerCase();
        try {
            WeixinMicropayStatus status = EnumUtil.toEnum(errCode, WeixinMicropayStatus.class);
            return status;
        } catch (EnumConstantInvalidException e2) {
            logger.error("resultCode:" + e.getResultCode() + " errCode:" + e.getErrCode() + " errMsg:" + e.getErrCodeDes());
            throw e;
        }
    }
}
Also used : WxPayMicropayResult(com.github.binarywang.wxpay.bean.result.WxPayMicropayResult) EnumConstantInvalidException(io.leopard.lang.inum.EnumConstantInvalidException)

Example 3 with EnumConstantInvalidException

use of io.leopard.lang.inum.EnumConstantInvalidException in project leopard by tanhaichao.

the class OnumMethodArgumentResolver method resolveName.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception {
    HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
    Object value = RequestBodyParser.getParameter(request, name);
    if (value == null) {
        return null;
    }
    Class<?> clazz = parameter.getParameterType();
    if (value instanceof String) {
        String json = (String) value;
        if (json.startsWith("{")) {
            Map<String, Object> map = Json.toMap(json);
            Object key = map.get("key");
            if (key == null) {
                throw new EnumConstantInvalidException("枚举的key不允许为null.");
            }
            return EnumUtil.toEnum(key, (Class<Enum>) clazz);
        }
    }
    if (Snum.class.isAssignableFrom(clazz)) {
        String key = (String) value;
        return EnumUtil.toEnum(key, (Class<Enum>) clazz);
    } else if (Inum.class.isAssignableFrom(clazz)) {
        int key;
        if (value instanceof Integer) {
            key = (int) value;
        } else {
            key = Integer.parseInt((String) value);
        }
        return EnumUtil.toEnum(key, (Class<Enum>) clazz);
    } else {
        throw new RuntimeException("未知枚举类型[" + clazz.getName() + "].");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EnumConstantInvalidException(io.leopard.lang.inum.EnumConstantInvalidException) Inum(io.leopard.lang.inum.Inum)

Example 4 with EnumConstantInvalidException

use of io.leopard.lang.inum.EnumConstantInvalidException in project leopard by tanhaichao.

the class UnderlineJson method toEnumList.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> List<T> toEnumList(String json, Class<T> clazz) {
    JavaType javaType = getObjectMapper().getTypeFactory().constructParametrizedType(ArrayList.class, List.class, Map.class);
    List<Map<String, Object>> mapList;
    try {
        mapList = getObjectMapper().readValue(json, javaType);
    } catch (Exception e) {
        // throw new JsonException(e.getMessage(), e);
        return toEnumListByOnlyKey(json, clazz);
    }
    List<T> list = new ArrayList<T>();
    for (Map<String, Object> map : mapList) {
        Object key = map.get("key");
        if (key == null) {
            throw new EnumConstantInvalidException("枚举的key不允许为null.");
        }
        list.add((T) EnumUtil.toEnum(key, (Class<Enum>) clazz));
    }
    return list;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ArrayList(java.util.ArrayList) Map(java.util.Map) EnumConstantInvalidException(io.leopard.lang.inum.EnumConstantInvalidException) JsonException(io.leopard.json.JsonException) EnumConstantInvalidException(io.leopard.lang.inum.EnumConstantInvalidException)

Aggregations

EnumConstantInvalidException (io.leopard.lang.inum.EnumConstantInvalidException)4 JsonException (io.leopard.json.JsonException)2 Inum (io.leopard.lang.inum.Inum)2 ArrayList (java.util.ArrayList)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 WxPayMicropayResult (com.github.binarywang.wxpay.bean.result.WxPayMicropayResult)1 Bnum (io.leopard.lang.inum.Bnum)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 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1