java 反射比较两个实体类中值是否相等
这里排除了XmlAttribute,XmlTransient注解的值进行比较
com.xxx.xxx.domain 这个是存放实体类的路劲
package com.xxx.xxx.utils;
import com.jfinal.plugin.activerecord.Record;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: fxp
* @Date: 2023/4/18 11:50
* @Description
*/
public class S127ObjectUtil {
public static Boolean compare(Object o1,Object o2) throws IllegalAccessException {
Boolean b = false;
if (null == o1 && null == o2){
return true;
}else if (null == o1 || null == o2){
return b;
}
Class<?> aClass = o1.getClass();
Class<?> aClass1 = o2.getClass();
if (aClass != aClass1){
return b;
}
Field[] declaredFields = aClass.getDeclaredFields();
b = compareByField(declaredFields,o1,o2,false);
if (!b){
return b;
}
Class<?> superclass = aClass.getSuperclass();
Field[] superclassDeclaredFields = superclass.getDeclaredFields();
b = compareByField(superclassDeclaredFields,o1,o2,false);
if (!b){
return b;
}
b = true;
return b;
}
public static Boolean compareByField(Field[] declaredFields,Object o1,Object o2,Boolean b) throws IllegalAccessException {
for (Field field : declaredFields) {
String pack = field.getGenericType().toString();
if (pack.contains("com.maphao.manage.domain.caris.roles")){
continue;
}
XmlAttribute annotation1 = field.getAnnotation(XmlAttribute.class);
if (null != annotation1){
continue;
}
XmlTransient annotation2 = field.getAnnotation(XmlTransient.class);
if (null != annotation2){
continue;
}
field.setAccessible(true);
Object ov1 = field.get(o1);
Object ov2 = field.get(o2);
if (null == ov1 && null == ov2){
continue;
}else if (null == ov1 || null == ov2){
return b;
}
// 集合
if (field.getType() == List.class){
//获取集合的泛型
ParameterizedType type = (ParameterizedType) field.getGenericType();
Class<?> clazz = (Class<?>) type.getActualTypeArguments()[0];
ArrayList<?> objects1 = (ArrayList<?>) ObjectUtils.castList(field.get(o1), clazz);
ArrayList<?> objects2 = (ArrayList<?>) ObjectUtils.castList(field.get(o2), clazz);
if (objects1.size() != objects2.size()){
return b;
}
if (objects1.size() == 0){
continue;
}
for (int i = 0; i < objects1.size(); i++) {
b = compare(objects1.get(i), objects2.get(i));
if (!b){
return b;
}
}
}
else if (ov1.getClass().isArray()){
pack = ov1.getClass().getComponentType().toString();
Object[] list1 = (Object[]) ov1;
Object[] list2 = (Object[]) ov2;
if (list1.length != list2.length){
return false;
}
if (pack.contains("com.xxx.xxx.domain")){
for (int i = 0; i < list1.length; i++) {
b = compare(list1[i],list2[i]);
if (!b){
return false;
}
}
}else {
for (int i = 0; i < list1.length; i++) {
if (!list1[i].equals(list2[i])){
return false;
}
}
}
}
// 这里为引用类型
else if (pack.contains("com.maphao.manage.domain")){
b = compare(ov1,ov2);
if (!b){
return b;
}
}
else if(!ov1.toString().equals(ov2.toString())){
return b;
}
}
b = true;
return b;
}
public static Object recrodToJavaBean(Record record, Class clazz) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException{
if(record != null){
Object obj = clazz.newInstance();
String[] columns = record.getColumnNames();
for(String col: columns){
Field field = clazz.getDeclaredField(col);
if(field != null){
if(!field.isAccessible()){
field.setAccessible(true);
}
field.set(obj, record.get(col));
}
}
return obj;
}
return null;
}
}