From: 刘洪青 Date: Tue, 3 Sep 2019 01:14:03 +0000 (+0800) Subject: fix: 修正工具类问题 X-Git-Tag: v0.0.1^2~67 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=adc334e7c7587be37839300413d2d867377c10de;p=institute%2Fsw-backend.git fix: 修正工具类问题 --- diff --git a/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java b/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java index 662b24a..902c592 100644 --- a/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java +++ b/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java @@ -1,6 +1,7 @@ package com.supwisdom.institute.backend.common.framework.entity; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.Date; import javax.persistence.Column; @@ -30,7 +31,7 @@ public class EntityUtils { * ,待覆盖的实体 * @return 合并后的newEntity */ - public static T merge(T oldEntity, T newEntity) { + public static T merge(T oldEntity, T newEntity) { for (Class clazz = oldEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { for (Field field : clazz.getDeclaredFields()) { @@ -59,19 +60,32 @@ public class EntityUtils { for (Class clazz = targetEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { for (Field field : clazz.getDeclaredFields()) { - Column[] annotations = field.getAnnotationsByType(Column.class); - if (annotations == null || annotations.length == 0) { - Id[] idAnnotations = field.getAnnotationsByType(Id.class); - if (idAnnotations == null || idAnnotations.length == 0) { - continue; - } + +// Column[] annotations = field.getAnnotationsByType(Column.class); +// if (annotations == null || annotations.length == 0) { +// Id[] idAnnotations = field.getAnnotationsByType(Id.class); +// if (idAnnotations == null || idAnnotations.length == 0) { +// continue; +// } +// } + + if (Modifier.isStatic(field.getModifiers())) { + continue; } String fieldName = field.getName(); + + if(fieldName.equals("serialVersionUID")){ + continue; + } + if (!ReflectUtils.existField(sourceEntity, fieldName)) { + continue; + } + Object sFieldValue = ReflectUtils.getFieldValue(sourceEntity, fieldName); if (sFieldValue != null) { - ReflectUtils.setFieldValue(targetEntity, fieldName, sFieldValue,field.getType()); + ReflectUtils.setFieldValue(targetEntity, fieldName, sFieldValue, field.getType()); } } } @@ -79,15 +93,25 @@ public class EntityUtils { return targetEntity; } - public static T fatherToChild (S father, T child){ + public static C fatherToChild (F father, C child){ for (Class clazz = child.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { for (Field field : clazz.getDeclaredFields()) { + if (Modifier.isStatic(field.getModifiers())) { + continue; + } + String fieldName = field.getName(); - if(fieldName.equals("serialVersionUID")){continue;} + if(fieldName.equals("serialVersionUID")){ + continue; + } + if (!ReflectUtils.existField(father, fieldName)) { + continue; + } + Object sFieldValue = ReflectUtils.getFieldValue(father, fieldName); if (sFieldValue != null) { - ReflectUtils.setFieldValue(child, fieldName, sFieldValue,field.getType()); + ReflectUtils.setFieldValue(child, fieldName, sFieldValue, field.getType()); } } } diff --git a/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java b/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java index 1b9313e..cddd545 100644 --- a/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java +++ b/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java @@ -33,7 +33,7 @@ public class ReflectUtils { * 目标属性 * @return 目标字段 */ - private static Field getField(Object obj, String fieldName) { + public static Field getField(Object obj, String fieldName) { Field field = null; for (Class clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { try { @@ -45,6 +45,11 @@ public class ReflectUtils { } return field; } + + public static boolean existField(Object obj, String fieldName) { + Field field = getField(obj, fieldName); + return field != null; + } /** * 调用Getter方法.