Commit 077f7ff2 authored by yanzg's avatar yanzg

将源码打包进jar包

parent 0f2649ca
...@@ -390,57 +390,61 @@ public class StringHelper { ...@@ -390,57 +390,61 @@ public class StringHelper {
/** /**
* 根据类型来获取值 * 根据类型来获取值
* *
* @param cls 类型 * @param fromCls 类型
* @param from 对象 * @param from 对象
* @return 返回获取的值 * @return 返回获取的值
*/ */
public static <T> T to(Class<T> cls, Object from) { public static <T> T to(Class<T> fromCls, Object from) {
String vName = cls.getName();
Object to = null; Object to = null;
if (isType(cls, String.class)) { if (isType(fromCls, String.class)) {
to = toString(from); to = toString(from);
} else if (TYPE_BOOL.equals(vName) } else if (isBaseType(TYPE_BOOL, Boolean.class, fromCls, from)) {
|| (isType(cls, Boolean.class) && from != null)) {
String strValue = toString(from); String strValue = toString(from);
to = toBoolean(strValue); to = toBoolean(strValue);
} else if (TYPE_INT.equals(vName) } else if (isBaseType(TYPE_INT, Integer.class, fromCls, from)) {
|| (isType(cls, Integer.class) && from != null)) {
to = toInt(from);
} else if (TYPE_LONG.equals(vName)
|| (isType(cls, Long.class) && from != null)) {
to = toInt(from); to = toInt(from);
} else if (TYPE_DOUBLE.equals(vName) || } else if (isBaseType(TYPE_LONG, Long.class, fromCls, from)) {
(isType(cls, Double.class) && from != null)) { to = toLong(from);
to = toDouble(from); } else if (isBaseType(TYPE_DOUBLE, Double.class, fromCls, from)) {
} else if (TYPE_FLOAT.equals(vName)
|| (isType(cls, Float.class) && from != null)) {
to = toDouble(from); to = toDouble(from);
} else if (isType(cls, Date.class)) { } else if (isBaseType(TYPE_FLOAT, Float.class, fromCls, from)) {
to = toFloat(from);
} else if (isType(fromCls, Date.class)) {
to = DateHelper.getDateTime(from); to = DateHelper.getDateTime(from);
} else if (cls.isEnum()) { } else if (fromCls.isEnum()) {
String strValue = toString(from); String strValue = toString(from);
if (strValue != null) { if (strValue != null) {
to = EnumHelper.toEnum(cls, strValue); to = EnumHelper.toEnum(fromCls, strValue);
} }
} }
if (to == null && from == null) { if (to == null && from == null) {
return null; return null;
} else if (to != null) { } else if (to != null) {
return (T) to; return (T) to;
} else if (cls == List.class && from instanceof List) { } else if (fromCls == List.class && from instanceof List) {
return (T) from; return (T) from;
} else if (cls == Map.class && from instanceof Map) { } else if (fromCls == Map.class && from instanceof Map) {
return (T) from; return (T) from;
} else if (cls == Object.class) { } else if (fromCls == Object.class) {
return (T) from; return (T) from;
} else { } else {
if (ObjectHelper.isSub(cls, from.getClass())) { if (ObjectHelper.isSub(fromCls, from.getClass())) {
return (T) from; return (T) from;
} }
return (T) to; return (T) to;
} }
} }
private static boolean isBaseType(String clsName, Class<?> cls, Class<?> fromCls, Object fromValue) {
if (clsName.equals(fromCls.getName())) {
return true;
}
if (fromValue == null) {
return false;
}
return isType(fromCls, cls);
}
/** /**
* 将 object 转换为String * 将 object 转换为String
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment