java 反射比较两个实体类中值是否相等

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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值