对象内List数组为空处理成长度为0的list

发布时间:2020-08-06 22:11:24 作者:小聪聪博客
来源:网络 阅读:406

public static void handlerNullList(Object object) {
try {
Class<? extends Object> clazz = object.getClass();
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method method : declaredMethods) {
String methodName = method.getName();
Class<?> type = method.getReturnType();
if(methodName.indexOf("get") == 0 && List.class.equals(type)) {
List getList = (List) method.invoke(object, null);
if(getList == null) {
String setMethodName = methodName.replaceFirst("g", "s");
//数组初始化(找到对应set方法,get返回值类型)
Method setMethod = clazz.getDeclaredMethod(setMethodName, List.class);
setMethod.invoke(object, Collections.emptyList());
} else {
if(getList.size() != 0) {
for(Object obj : getList) {
handlerNullList(obj);
}
}
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

推荐阅读:
  1. java如何根据List内对象的属性进行排序
  2. python判断是否为list的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

list 数组 lis

上一篇:前端开发入门

下一篇:创建外部表(external table)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》