105 Star 535 Fork 182

GVP帝博软件 / diboot

Closed
!1 增加深绑定的注解DeepBind,字段是Entity或者List<Entity>都可以进行深度绑定,亲测可用

帝博软件:master 帝博软件:develop

smilezmh Created on: 2020-10-09 17:49
private void bind(Class voClazz, List returnList) throws InvocationTargetException, IllegalAccessException {
        Field[] fields = voClazz.getDeclaredFields();// 获取vo所有字段
        Method[] voMethods = voClazz.getDeclaredMethods();// 获取vo所有方法

        if (V.notEmpty(fields)) {
            for (Field field : fields) {
                field.setAccessible(true);

                DeepBind annotation = field.getAnnotation(DeepBind.class);// 对每个字段获取有DeepBind标识的注解

                if (annotation != null) {// 获取需要深绑定vo中的vos
                    Class inVoClazz = Object.class;

                    if (field.getGenericType() instanceof ParameterizedType) {
                        inVoClazz = (Class) (((ParameterizedType) (field.getGenericType())).getActualTypeArguments()[0]);// vo中的深绑定的clazz
                    } else if (field.getGenericType() instanceof Class) {
                        inVoClazz = (Class) field.getGenericType();// vo中的深绑定的clazz
                    }

                    if (V.notEmpty(voMethods)) {
                        for (Method m : voMethods) {
                            List inVos = new ArrayList();

                            if (("get" + field.getName()).toLowerCase().equals(m.getName().toLowerCase())) {// 获取vo 的get方法
                                for (Object vo : returnList) {// 遍历返回list所有数据
                                    List inList = null;
                                    Object obj = null;

                                    if (V.notEmpty(vo)) {
                                        obj = m.invoke(vo, null);// 获得vo
                                    }

                                    if (V.notEmpty(obj)) {
                                        if (obj instanceof List) {
                                            inList = (List) obj;// 获取的是一个list
                                        } else if (inVoClazz == obj.getClass()) {
                                            inList = new ArrayList();
                                            inList.add(obj);
                                        }
                                    }

                                    if (V.notEmpty(inList)) {
                                        inVos.addAll(inList);
                                    }
                                }

                                if (V.notEmpty(inVos)) {// 对于DeepBind标识的注解的字段批量进行绑定
                                    RelationsBinder.bind(GenericsUtils.convertList(inVos, inVoClazz));
                                    bind(inVoClazz, inVos);// 递归调用
                                }
                            }
                        }
                    }
                }
            }
        }
    }
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DeepBind {
    @AliasFor("deep")
    boolean value() default false;

    @AliasFor("value")
    boolean deep() default false;
}

0 comments, 1 participants

Show action logs Hide action logs
dibo-software closed Pull Request 2020-10-23 10:25
smilezmh updated description 2020-10-09 18:14
smilezmh updated description 2020-10-09 18:13
smilezmh updated title 2020-10-09 18:04
smilezmh updated description 2020-10-09 18:02
smilezmh updated description 2020-10-09 18:01
smilezmh updated description 2020-10-09 17:50
smilezmh updated description 2020-10-09 17:50
Java
1
https://gitee.com/dibo_software/diboot.git
git@gitee.com:dibo_software/diboot.git
dibo_software
diboot
diboot

Search