java 对象反射拿取对象名称及数据的校验

发布时间 2024-01-12 11:14:09作者: 何一徐一
public class AttributeFs {


/**
* William
* 循环实体list属性统一判断操作返回异常问题
* @param errorList
* @param object
* @return
*/
public static List getAttribute(List errorList, Object object) {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
if (ConstantCommon.FILTERTYPE.equals(field.getType().toString())) {
String name = field.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
String propertyName = field.getAnnotation(ApiModelProperty.class).value();
try {
Method m = object.getClass().getMethod("get" + name);
List nList = (List) m.invoke(object);
if (exceed(nList)) {
errorList.add("前端参数"+propertyName+"超过限制数{"+ConstantCommon.SIZE+"}现有数{"+nList.size()+"}");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
return errorList;
}

public static boolean exceed(List<?> list) {
return !CollectionUtils.isEmpty(list) && list.size() >= ConstantCommon.SIZE;
}