/**
* 比较两个对象属性是否相同
*/
public void compareObj(Object obj1, Object obj2) {
List<String> textList = new ArrayList<>();
try {
//((clazz.getSimpleName().equals("Building") && ((Building)obj2).getStatus() == 3))
Class clazz = obj1.getClass();
Field[] fields = obj1.getClass().getDeclaredFields();
for (Field field : fields) {
PropertyDescriptor pd = new PropertyDescriptor(field
.getName(), clazz);
Method getMethod = pd.getReadMethod();
Object o1 = getMethod.invoke(obj1);
Object o2 = getMethod.invoke(obj2);
if (o2 == null || o2 == "")
continue;
if (o1 != null){
if (!o1.toString().equals(o2.toString())) {
textList.add(getMethod.getName() + ":" + "false");
} else {
textList.add(getMethod.getName() + ":" + "true");
}
}
}
} catch (Exception e) {
LOGGER.error("compare object failure:"+e);
}
for (Object object : textList) {
System.out.println(object);
}
}
对象比较类
最新推荐文章于 2024-10-02 06:13:56 发布