java的反射实现:
判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更
今日份代码:
package com.sxd.streamTest; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature; 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();
List<Long> list1 = new ArrayList<>();
list1.add(1L);
list1.add(2L); List<Long> list2 = new ArrayList<>();
list1.add(1L); Student student1 = new Student();
student1.setAge(1);
student1.setName("张三1");
student1.setModifyTime(modifyTime);
student1.setStoreIdList(list1);
student1.setYn(1); Student student2 = new Student();
student2.setAge(1);
student2.setName("张三1");
student2.setModifyTime(modifyTime2);
student2.setStoreIdList(list2);
student2.setYn(-1); 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> sList1 = Arrays.asList(student2,student1);
List<Student> sList2 = Arrays.asList(student4,student3); System.out.println("List集合是否变更:" + listModifyContrast(sList2,sList1));
System.out.println("单个对象是否变更:" + objModifyContrast(student4,student5,false));
} /**
* List数据变更对比
* 比较两个相同泛型的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 (isBaseType(newObj)){
if (newObj == oldObj){
same ++;
}
}else {
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;
} /**
* 判断obj是否基本数据类型
* 这里Integer和int 都认定为基本类型
* @param obj
* @return
*/
private boolean isBaseType(Object obj){
Class aClass = obj.getClass();
if (aClass == Integer.class || aClass ==Integer.TYPE
|| aClass == Byte.class || aClass == Byte.TYPE
|| aClass == Short.class || aClass == Short.TYPE
|| aClass == Character.class || aClass == Character.TYPE
|| aClass == Long.class || aClass == Long.TYPE
|| aClass == Double.class || aClass == Double.TYPE
|| aClass == Float.class || aClass == Float.TYPE
|| aClass == Boolean.class || aClass == Boolean.TYPE
){
return true;
}else {
return false;
}
} /**
* 单个对象-属性修改对比
* 属性如果是集合类型,则要求泛型需要重写hashCode和equals方法
* @param newObj
* @param oldObj
* @param reset 是否重置属性
* @return true有变更 false无变更
*/
public Boolean objModifyContrast(Object newObj,Object oldObj,boolean reset){
boolean flag = false; if (newObj != null && oldObj != null){
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);
flag = true;
}else {
flag = false;
}
}else {
if (newObj == null && oldObj == null){
flag = false;
}else {
flag = true;
}
}
return flag;
} /**
* 获取对象的属性名 + 属性值
* @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;
} }