首页 > 编程语言 > 详细

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

时间:2019-03-26 11:25:39      阅读:259      评论:0      收藏:0      [点我收藏+]
private static void handlerNullList(Object object) { try { Class<? extends Object> clazz = object.getClass(); Method[] declaredMethods = clazz.getDeclaredMethods(); for(Method method : declaredMethods) { String methodName = method.getName(); Type type = method.getAnnotatedReturnType().getType(); if(methodName.indexOf("get") == 0 && type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if(pt.getRawType().equals(List.class)) { 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); } }

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

原文:https://blog.51cto.com/12165865/2369109

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!