【java】【反射】反射实现判断发⽣了修改操作,判断两个对象是否发⽣属性值的变更,判断两个。。。
java的反射实现:
判断发⽣了修改操作,判断两个对象是否发⽣属性值的变更,判断两个List集合内对象的属性值是否发⽣变更
今⽇份代码:
package com.sxd.streamTest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import flect.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);
java反射的作用及应用场景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 = Class();
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 (! ains(field)){
Object newFieldValue = (field);
Object oldFieldValue = (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 = Class();
Field[] declaredFields = DeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
String fieldName = Name();
Object fieldValue = null;
try {
fieldValue = (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 = Class();
for (String field : fieldSet) {
try {
Field declaredField = DeclaredField(field); declaredField.setAccessible(true);
declaredField.set((field));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return obj;
}
}
View Code
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论