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

public static void handlerNullList(Object object) {
try {
Class 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);
}
}

你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事网站设计制作、成都网站制作、企业网站建设、手机网站制作、网页设计、成都品牌网站建设、网页制作、做网站、建网站。成都创新互联公司拥有实力坚强的技术研发团队及素养的视觉设计专才。


网站名称:对象内List数组为空处理成长度为0的list
本文地址:http://myzitong.com/article/jogpoj.html