Commit 077f7ff2 authored by yanzg's avatar yanzg

将源码打包进jar包

parent 0f2649ca
......@@ -390,57 +390,61 @@ public class StringHelper {
/**
* 根据类型来获取值
*
* @param cls 类型
* @param from 对象
* @param fromCls 类型
* @param from 对象
* @return 返回获取的值
*/
public static <T> T to(Class<T> cls, Object from) {
String vName = cls.getName();
public static <T> T to(Class<T> fromCls, Object from) {
Object to = null;
if (isType(cls, String.class)) {
if (isType(fromCls, String.class)) {
to = toString(from);
} else if (TYPE_BOOL.equals(vName)
|| (isType(cls, Boolean.class) && from != null)) {
} else if (isBaseType(TYPE_BOOL, Boolean.class, fromCls, from)) {
String strValue = toString(from);
to = toBoolean(strValue);
} else if (TYPE_INT.equals(vName)
|| (isType(cls, Integer.class) && from != null)) {
to = toInt(from);
} else if (TYPE_LONG.equals(vName)
|| (isType(cls, Long.class) && from != null)) {
} else if (isBaseType(TYPE_INT, Integer.class, fromCls, from)) {
to = toInt(from);
} else if (TYPE_DOUBLE.equals(vName) ||
(isType(cls, Double.class) && from != null)) {
to = toDouble(from);
} else if (TYPE_FLOAT.equals(vName)
|| (isType(cls, Float.class) && from != null)) {
} else if (isBaseType(TYPE_LONG, Long.class, fromCls, from)) {
to = toLong(from);
} else if (isBaseType(TYPE_DOUBLE, Double.class, fromCls, 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);
} else if (cls.isEnum()) {
} else if (fromCls.isEnum()) {
String strValue = toString(from);
if (strValue != null) {
to = EnumHelper.toEnum(cls, strValue);
to = EnumHelper.toEnum(fromCls, strValue);
}
}
if (to == null && from == null) {
return null;
} else if (to != null) {
return (T) to;
} else if (cls == List.class && from instanceof List) {
} else if (fromCls == List.class && from instanceof List) {
return (T) from;
} else if (cls == Map.class && from instanceof Map) {
} else if (fromCls == Map.class && from instanceof Map) {
return (T) from;
} else if (cls == Object.class) {
} else if (fromCls == Object.class) {
return (T) from;
} else {
if (ObjectHelper.isSub(cls, from.getClass())) {
if (ObjectHelper.isSub(fromCls, from.getClass())) {
return (T) from;
}
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
*
......
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