/**
* @param object 对象
* @param excludedProperty 过滤属性
**/
public static <T> void validationAttributeNotEmpty(T object, String excludedProperty) {
// 获取对象的所有属性
Field[] fields = object.getClass().getDeclaredFields();
// 遍历每个属性
for (Field field : fields) {
// 排除指定属性
if (field.getName().equals(excludedProperty)) {
continue;
}
field.setAccessible(true);
try {
// 判断属性值是否为空
if (field.get(object) == null) {
Excel annotation = field.getAnnotation(Excel.class);
String propertyName = (annotation != null) ? annotation.name() : field.getName();
throw new BizException(propertyName + "不能为空");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
JAVA反射校验对象属性不为空
于 2023-06-30 17:09:00 首次发布
该方法用于对Java对象的属性进行验证,确保除指定的excludedProperty外,其他属性不为空。它遍历对象的所有声明字段,跳过指定的过滤属性,然后设置字段访问权限并检查其值。如果属性值为空,且字段有Excel注解,使用注解的name,否则使用字段名,抛出一个包含属性名信息的BizException。

被折叠的 条评论
为什么被折叠?



