Commit a7a67dbc authored by yanzg's avatar yanzg

修复bug

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