修改变量

master
qi_liang 4 years ago
parent b6fe9cbdae
commit f1792274e7
  1. 24
      panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java

@ -89,29 +89,29 @@ public class BeanUtil {
/** /**
* 复制属性不为空的复制为空忽略 * 复制属性不为空的复制为空忽略
*/ */
public static void copyNotNull(Object targer,Object source){ public static void copyNotNull(Object target,Object source){
Class sourceClass = source.getClass(); Class sourceClass = source.getClass();
List<Field> sourcefieldList = getAllField(sourceClass); List<Field> sourceFieldList = getAllField(sourceClass);
Map<String,Field> fieldMap = new HashMap<>(sourcefieldList.size()); Map<String,Field> fieldMap = new HashMap<>(sourceFieldList.size());
sourcefieldList.forEach(field -> { sourceFieldList.forEach(field -> {
field.setAccessible(true); field.setAccessible(true);
fieldMap.put(field.getName(),field); fieldMap.put(field.getName(),field);
}); });
Class targerClass = targer.getClass(); Class targetClass = target.getClass();
List<Field> targerFieldList = getAllField(targerClass); List<Field> targetFieldList = getAllField(targetClass);
for (Field field:targerFieldList){ for (Field field:targetFieldList){
field.setAccessible(true); field.setAccessible(true);
Field targerField = fieldMap.get(field.getName()); Field targetField = fieldMap.get(field.getName());
//判断类型是否相等 //判断类型是否相等
if (targerField!=null&&targerField.getGenericType().toString().equals(field.getGenericType().toString())){ if (targetField!=null&&targetField.getGenericType().toString().equals(field.getGenericType().toString())){
try { try {
PropertyDescriptor pd = new PropertyDescriptor(targerField.getName(), targerClass); PropertyDescriptor pd = new PropertyDescriptor(targetField.getName(), targetClass);
//获得写方法 //获得写方法
Method setFieldMethod = pd.getWriteMethod(); Method setFieldMethod = pd.getWriteMethod();
Object value = targerField.get(source); Object value = targetField.get(source);
if (value!=null) { if (value!=null) {
setFieldMethod.invoke(targer, value); setFieldMethod.invoke(target, value);
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save