标签:bsp declared import out get dex package system tcl
java的反射实现:
判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更
今日份代码:
package com.sxd.streamTest;
import java.lang.reflect.Field;
import java.util.*;
/**
* @ClassName Test
* @Description 判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更
* @Author sxd
* @Date 2020/02/14 10:07
* @Version 1.0
*/
public class Test {
//忽略的属性
List<String> ignoreFields = Arrays.asList("yn","id");
/**
* 测试的入口方法,实例化几个对象
*/
@org.junit.Test
public void test(){
Date modifyTime = new Date();
Date modifyTime2 = new Date();
Student student1 = new Student();
student1.setAge(1);
student1.setName("张三1");
student1.setModifyTime(modifyTime);
student1.setYn(1);
Student student2 = new Student();
student2.setAge(1);
student2.setName("张三2");
student2.setModifyTime(modifyTime2);
student2.setYn(-1);
List<Student> list1 = Arrays.asList(student1,student2);
Student student3 = new Student();
student3.setAge(3);
student3.setName("张三3");
student3.setModifyTime(modifyTime);
student3.setYn(1);
Student student4 = new Student();
student4.setAge(4);
student4.setName("张三4");
student4.setModifyTime(modifyTime2);
student4.setYn(-1);
Student student5 = new Student();
student5.setAge(4);
student5.setName("张三4");
student5.setModifyTime(modifyTime2);
student5.setYn(11);
List<Student> list2 = Arrays.asList(student4,student3);
System.out.println("List集合是否变更:" + listModifyContrast(list2,list1));
System.out.println("单个对象是否变更:" + objModifyContrast(student5,student4,false));
}
/**
* List数据变更对比
* @param newList
* @param oldList
* @return true有变更 false无变更
*/
public Boolean listModifyContrast(List<? extends Object> newList, List<? extends Object> oldList){
boolean flag = false;
if (newList != null && oldList != null){
if (newList.size() != oldList.size()){
flag = true;
}else {
int same = 0;
for (Object newObj : newList) {
for (Object oldObj : oldList) {
if (!objModifyContrast(newObj,oldObj,false)){
same ++;
}
}
}
if (same != newList.size()){
flag = true;
}
}
}else {
if (newList == null && oldList == null){
flag = false;
}else {
flag = true;
}
}
return flag;
}
/**
* 单个对象-属性修改对比
* @param newObj
* @param oldObj
* @param reset 是否重置属性
* @return true有变更 false无变更
*/
public Boolean objModifyContrast(Object newObj,Object oldObj,boolean reset){
Map<String, Object> newMap = getReflexMap(newObj);
Map<String, Object> oldMap = getReflexMap(oldObj);
int diff = 0;
for (String field : newMap.keySet()) {
if (! ignoreFields.contains(field)){
Object newFieldValue = newMap.get(field);
Object oldFieldValue = oldMap.get(field);
if (newFieldValue != null && oldFieldValue != null){
if (newFieldValue.equals(oldFieldValue)){
if (reset){
newMap.put(field,null);
oldMap.put(field,null);
}
}else {
diff ++;
}
}else {
if (!(newFieldValue == null && oldFieldValue == null)){
diff ++;
}
}
}
}
if (diff > 0){
setReflexObj(newMap, newObj);
setReflexObj(oldMap, oldObj);
return true;
}else {
return false;
}
}
/**
* 获取对象的属性名 + 属性值
* @param obj
* @return
*/
public Map<String,Object> getReflexMap(Object obj){
Map<String,Object> map = new HashMap<>();
Class<? extends Object> aClass = obj.getClass();
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
String fieldName = declaredField.getName();
Object fieldValue = null;
try {
fieldValue = declaredField.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
map.put(fieldName,fieldValue);
}
return map;
}
/**
* 设置 对象的 属性名+属性值
* @param map
* @return
*/
public Object setReflexObj(Map<String,Object> map,Object obj){
Set<String> fieldSet = map.keySet();
Class<? extends Object> aClass = obj.getClass();
for (String field : fieldSet) {
try {
Field declaredField = aClass.getDeclaredField(field);
declaredField.setAccessible(true);
declaredField.set(obj,map.get(field));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return obj;
}
}
【java】【反射】反射实现判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更
标签:bsp declared import out get dex package system tcl
原文地址:https://www.cnblogs.com/sxdcgaq8080/p/12306433.html