本文共 823 字,大约阅读时间需要 2 分钟。
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);}}转载于:https://blog.51cto.com/12165865/2369109