Java比较两个对象的不同之处
大概思路是利用反射实现。
自定义注解代码:
/**
* 自定义注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CompareField {
String value() default "";
}
实体类代码:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@CompareField(value = "姓名")
private String name;
@CompareField(value = "性别")
private String sex;
private Integer age;
}
核心实现类代码:
/**
* Java比较两个对象的不同之处
*/
public class CompareObjUtils<T> {
/**
* 只比较有注解的字段
*
* @param oldBean
* @param newBean
* @return
*/
public String compareObjWithAnnotion(Object oldBean, Object newBean) {
String result = "";
T pojo1 = (T) oldBean;
T pojo2 = (T) newBean;
try {
Class clazz = pojo1.getClass();
Field[] fields = pojo1.getClass().getDeclaredFields();
int i = 1;
for (Field field : fields) {
boolean hasAnnotation = field.isAnnotationPresent(CompareField.class);
// 如果没有注解,则continue
if (!hasAnnotation) {
continue;
}
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
Method getMethod = pd.getReadMethod();
CompareField annotation = field.getAnnotation(CompareField.class);
Object o1 = getMethod.invoke(pojo1);
Object o2 = getMethod.invoke(pojo2);
if (o1 == null || o2 == null) {
continue;
}
if (!o1.toString().equals(o2.toString())) {
if (i != 1) {
result += "; ";
}
//要显示的字段名:name,age,sex......
String fieldName = field.getName();
//注解上的名称:@CompareField(value = "姓名")
String annotationValue = annotation.value();
//拼接返回结果,可根据具体需求拼接。
result += annotationValue + "、" + fieldName + ",旧值:" + o1 + ",新值:" + o2;
}
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
测试类代码:
public class Test {
public static void main(String[] args) {
User u1 = new User("张三", "M", 20);
User u2 = new User("李四", "M", 20);
CompareObjUtils<User> contrastObjUtils = new CompareObjUtils<User>();
String contrastObj = contrastObjUtils.compareObjWithAnnotion(u1, u2);
System.out.println(contrastObj);
//测试结果: 姓名、name,旧值:张三,新值:李四
}
}