通过反射判断对象的所有属性是否为空
public static boolean isAllFieldNull(Object o) {
try {
for (Field field : o.getClass().getDeclaredFields()) {
//把私有属性公有化
field.setAccessible(true);
Object object = field.get(o);
if (object!=null) {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
如果想判断对象中,除了某个属性,其他属性是否都为空,可以在遍历判断时,增加名字判断即可
if (object!=null&& field.getName()!="number") {
return false;
}