java 比较两个对象是否一致

 /**

     * 比较对象值是否发生了改变

     * @param obj1  

     * @param obj2

     * @return

     */

    public static boolean contrastObj(Object obj1, Object obj2) {

        boolean isEquals = true;

        if (obj1 instanceof objec && obj2 instanceof objec ) {

            objec pojo1 = (objec ) obj1;

            objec  pojo2 = (objec ) obj2;

            List<String> textList = new ArrayList<String>();

            

                Class<? extends objec > clazz = pojo1.getClass();

                Field[] fields = pojo1.getClass().getDeclaredFields();

                int count = 0 ;

                for (Field field : fields) {

                    PropertyDescriptor pd;

                 

                    try {

                        pd = new PropertyDescriptor(field.getName(), clazz);

                       if(count==72){

                           

                       }

               

                    Method getMethod = pd.getReadMethod();

                    Object o1 = getMethod.invoke(pojo1);

                    Object o2 = getMethod.invoke(pojo2);

                    count++;

                    if(o1==null && o2 == null){

                        continue;

                    }

                    if(o1.equals(0) && o2.equals(0)){

                        continue;

                    }

                    if (!o1.toString().equals(o2.toString())) {

                        isEquals = false;

                        textList.add(getMethod.getName() + ":" + "false");

                    } else {

                        textList.add(getMethod.getName() + ":" + "true");

                    }

                    } catch (Exception e) {

                    }

                }

            

        }

        return isEquals;

    }

猜你喜欢

转载自1306577707.iteye.com/blog/2413194