Commit a7a67dbc authored by yanzg's avatar yanzg

修复bug

parent dfd49478
......@@ -20,7 +20,7 @@ public class ObjectHelper {
/**
* 缓存的类型
*/
private static final Map<Class<?>, HashMap<String, MethodField>> mapCache = new HashMap<>();
private static final Map<Class<?>, HashMap<String, MethodField>> MAP_CACHE = new HashMap<>();
// --------------------------------------------- 类型判断 ---------------------------------------------------------
......@@ -29,9 +29,9 @@ public class ObjectHelper {
*
* @param to 需要转换后的类型
* @param from 转换前的类型
* @return
* @return 是否可以转换
*/
public static boolean isAutoConvert(Class to, Class from) {
public static boolean isAutoConvert(Class<?> to, Class<?> from) {
return from != to && (Number.class.isAssignableFrom(to) || to.isEnum() || to == String.class);
}
......@@ -42,7 +42,7 @@ public class ObjectHelper {
* @param from 子类
* @return 是否满足条件
*/
public static boolean isSub(Class to, Class from) {
public static boolean isSub(Class<?> to, Class<?> from) {
return from == to || from.isAssignableFrom(to);
}
......@@ -137,20 +137,17 @@ public class ObjectHelper {
* @return 获取之后的值
*/
public static Object get(Object target, String field) {
Object ret = null;
if (target == null) {
return ret;
return null;
}
Class vType = target.getClass();
Class<?> vType = target.getClass();
if (target instanceof Number || vType.isEnum()) {
ret = null;
return null;
} else if (target instanceof Map) {
ret = ((Map) target).get(field);
return ((Map) target).get(field);
} else {
ret = getByType(target.getClass(), target, field);
return getByType(target.getClass(), target, field);
}
return ret;
}
/**
......@@ -190,7 +187,7 @@ public class ObjectHelper {
if (target == null) {
return;
}
Class vType = target.getClass();
Class<?> vType = target.getClass();
if (target instanceof Number || vType.isEnum()) {
return;
} else if (target instanceof Map) {
......@@ -227,11 +224,11 @@ public class ObjectHelper {
public static void setByType(Object target, MethodField item, Object value) throws IllegalAccessException, InvocationTargetException {
if (item.getField() != null && Modifier.isPublic(item.getField().getModifiers())) {
Class toType = item.getField().getType();
Class<?> toType = item.getField().getType();
Object toValue = StringHelper.to(toType, value);
item.getField().set(target, toValue);
} else if (item.getSetMethod() != null && Modifier.isPublic(item.getSetMethod().getModifiers())) {
Class toType = item.getSetMethod().getParameterTypes()[0];
Class<?> toType = item.getSetMethod().getParameterTypes()[0];
Object toValue = StringHelper.to(toType, value);
item.getSetMethod().invoke(target, toValue);
}
......@@ -246,11 +243,11 @@ public class ObjectHelper {
* @param <T> 复制的对象的类型
* @return 新的对象
*/
public static <T extends Object> T clone(T from) {
public static <T> T clone(T from) {
if (from == null) {
return from;
return null;
}
Class cls = from.getClass();
Class<?> cls = from.getClass();
try {
Object to = cls.newInstance();
writeWithFromClass(to, from);
......@@ -265,7 +262,7 @@ public class ObjectHelper {
* @param typeCache 类型
* @param fromName 来源名称
* @param toName 目标名称
* @return
* @return 返回找到的字段
*/
private static MethodField getField(HashMap<String, MethodField> typeCache, String fromName, String toName) {
if (!typeCache.containsKey(toName)) {
......@@ -280,8 +277,8 @@ public class ObjectHelper {
/**
* 获取简单名称
*
* @param fromName
* @return
* @param fromName 来源名称
* @return 名称结果
*/
private static String getSimpleName(String fromName) {
String toName = getSimpleFieldName(fromName);
......@@ -349,7 +346,8 @@ public class ObjectHelper {
String toName = getSimpleName(methodNameSource);
if ("getClass".equals(methodNameSource)) {
continue;
} else if (methodNameSimple.startsWith("set")) {
}
if (methodNameSimple.startsWith("set")) {
if (method.getParameterTypes().length != 1) {
continue;
}
......@@ -358,17 +356,11 @@ public class ObjectHelper {
obj.setSetMethod(method);
}
} else if (methodNameSimple.startsWith("get")) {
if (method.getReturnType() == null) {
continue;
}
MethodField obj = getField(typeCache, methodNameSource, toName);
if (obj.getGetMethod() == null) {
obj.setGetMethod(method);
}
} else if (methodNameSimple.startsWith("is")) {
if (method.getReturnType() == null) {
continue;
}
MethodField obj = getField(typeCache, methodNameSource, toName);
if (obj.getGetMethod() == null) {
obj.setGetMethod(method);
......@@ -386,11 +378,11 @@ public class ObjectHelper {
*/
public static HashMap<String, MethodField> getTypeField(Class<?> type) {
HashMap<String, MethodField> typeCache;
if (mapCache.containsKey(type)) {
typeCache = mapCache.get(type);
if (MAP_CACHE.containsKey(type)) {
typeCache = MAP_CACHE.get(type);
} else {
typeCache = getInitTypeField(type);
mapCache.put(type, typeCache);
MAP_CACHE.put(type, typeCache);
}
return typeCache;
}
......@@ -398,14 +390,14 @@ public class ObjectHelper {
/**
* 获取字段的名称
*
* @param type
* @param name
* @return
* @param type 类型
* @param name 名称
* @return 名称
*/
private static MethodField getMethodField(Class<?> type, String name) {
HashMap<String, MethodField> typeCache = getTypeField(type);
name = getSimpleFieldName(name);
return typeCache.containsKey(name) ? typeCache.get(name) : null;
return typeCache.getOrDefault(name, null);
}
......@@ -544,7 +536,7 @@ public class ObjectHelper {
public static Object writeWithFromMap(boolean emptyWrite, Object to, Map from) {
for (Object key : from.keySet()) {
Object fromValue = from.get(key);
if (StringHelper.isEmpty(fromValue)) {
if (StringHelper.isEmpty(fromValue) && !emptyWrite) {
continue;
}
ObjectHelper.set(to, StringHelper.toString(key), fromValue);
......@@ -595,7 +587,7 @@ public class ObjectHelper {
for (Map.Entry<String, MethodField> field : mapField.entrySet()) {
String name = field.getValue().getName();
Object fromValue = ObjectHelper.get(from, name);
if (StringHelper.isEmpty(fromValue)) {
if (StringHelper.isEmpty(fromValue) && !emptyWrite) {
continue;
}
ObjectHelper.set(to, name, fromValue);
......@@ -612,7 +604,7 @@ public class ObjectHelper {
* @return 转换后的结果
*/
public static <T> ArrayList<T> getList(Class<T> cls, Object froms) {
ArrayList<T> tos = new ArrayList<T>();
ArrayList<T> tos = new ArrayList<>();
if (froms instanceof List) {
List vCodeFrom = (List) froms;
for (Object from : vCodeFrom) {
......@@ -638,7 +630,7 @@ public class ObjectHelper {
* @param froms 需要转换的数组
* @return 数组类型
*/
public static <T extends Object> T[] getArray(T... froms) {
public static <T> T[] getArray(T... froms) {
return froms;
}
......@@ -650,7 +642,7 @@ public class ObjectHelper {
* @param <T> 泛型
* @return 数组长度
*/
public static <T extends Object> T[] createArray(Class<?> cls, int length) {
public static <T> T[] createArray(Class<?> cls, int length) {
Object array = Array.newInstance(cls, length);
return (T[]) array;
}
......
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