- 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);
- }
- }
来源: http://www.bubuko.com/infodetail-3000492.html