检查Pojo对象是否有null字段
//检查Pojo对象是否有null字段
public static boolean checkPojoNullField(Object o, Class<?> clz) {
try {
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.get(o) == null) {
return false;
}
}
if (clz.getSuperclass() != Object.class) {
return checkPojoNullField(o, clz.getSuperclass());
}
return true;
} catch (IllegalAccessException e) {
return false;
}
}
版本信息比较
public class Test4 {
public static void main(String[] args) {
Student stu1=new Student(20,"陈墙微","师大","女");
Student stu2=new Student(20,"赵强","师大","男");
Map<String,String> map= compare(stu1,stu2);
map.forEach((k,v)->System.out.println(" 字段 "+k+" 不同 " +v));
}
static Map<String,String> compare(Student stu1,Student stu2) {
Map<String,String> map=new HashMap<>();
Class<?> clazz=stu1.getClass();
Field [] fieldList= clazz.getDeclaredFields();
try {
for(Field f: fieldList) {
f.setAccessible(true);
Object v1 =f.get(stu1);
Object v2 =f.get(stu2);
if(!v1.equals(v2)) {
map.put(f.getName(), " stu1的值是:" +v1 +" 而stu2的值是 :"+v2);
}
}
}
catch(Exception e) {
e.printStackTrace();
}
return map;
}
}