利用Java反射机制获取实体对象所有属性及其对应值

  • 利用Java反射机制获取实体对象所有属性
  • 利用PropertyDescriptorgetReadMethod()getWriteMethod()方法得到Getter和Setter方法

PropertyDescriptor依赖于openbeans库

openbeans:https://code.google.com/archive/p/openbeans/

public class ReflectHelper {

    public static List<Reflect> getProperty(Object object, String exclude) {
        if (object == null) return null;
        List<Reflect> reflectList = null;
        Reflect reflect = null;
        Class clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            reflectList = new ArrayList<>();
            try {
                for (int i = 0; i < fields.length; i++) {
                    Field field = fields[i];
                    JSONField jsonField = field.getAnnotation(JSONField.class);
                    if (jsonField != null && !jsonField.serialize()) {
                        continue;
                    }
                    if (!TextUtils.isEmpty(exclude) &&
                            ((jsonField != null && exclude.equals(jsonField.name())) || exclude.equals(field.getName()))) {
                        continue;
                    }
                    //Parcelable对象
                    if (object instanceof Parcelable && "CREATOR".equals(field.getName())) {
                        continue;
                    }
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                    Method getMethod = pd.getReadMethod();
                    if (getMethod != null) {
                        reflect = new Reflect();
                        if (jsonField != null) {
                            reflect.setFieldName(jsonField.name());
                        } else {
                            reflect.setFieldName(field.getName());
                        }
                        reflect.setFieldType(field.getType());
                        reflect.setFieldValue(getMethod.invoke(object));
                        reflectList.add(reflect);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }
        }
        return reflectList;
    }

    public static List<Reflect> getProperty(Object object, List<String> excludeList) {
        if (object == null) return null;
        List<Reflect> reflectList = null;
        Reflect reflect = null;
        Class clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            reflectList = new ArrayList<>();
            try {
                for (int i = 0; i < fields.length; i++) {
                    Field field = fields[i];
                    JSONField jsonField = field.getAnnotation(JSONField.class);
                    if (jsonField != null && !jsonField.serialize()) {
                        continue;
                    }
                    if (excludeList != null && excludeList.size() > 0 &&
                            ((jsonField != null && excludeList.contains(jsonField.name())) || excludeList.contains(field.getName()))) {
                        continue;
                    }
                    //Parcelable对象
                    if (object instanceof Parcelable && "CREATOR".equals(field.getName())) {
                        continue;
                    }
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                    Method getMethod = pd.getReadMethod();
                    if (getMethod != null) {
                        reflect = new Reflect();
                        if (jsonField != null) {
                            reflect.setFieldName(jsonField.name());
                        } else {
                            reflect.setFieldName(field.getName());
                        }
                        reflect.setFieldType(field.getType());
                        reflect.setFieldValue(getMethod.invoke(object));
                        reflectList.add(reflect);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }
        }
        return reflectList;
    }

    public static class Reflect implements Comparable<Reflect> {
        private String fieldName;
        private Class fieldType;
        private Object fieldValue;

        public String getFieldName() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        public Class getFieldType() {
            return fieldType;
        }

        public void setFieldType(Class fieldType) {
            this.fieldType = fieldType;
        }

        public Object getFieldValue() {
            return fieldValue;
        }

        public void setFieldValue(Object fieldValue) {
            this.fieldValue = fieldValue;
        }

        @Override
        public int compareTo(@NonNull Reflect reflect) {
            return fieldName.compareTo(reflect.fieldName);
        }
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/using-java-reflection-mechanism-to-obtain-all-attributes-and-their-corresponding-values-of-entity-objects/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
利用Java反射机制获取实体对象所有属性及其对应值
利用Java反射机制获取实体对象所有属性 利用PropertyDescriptor的getReadMethod()和getWriteMethod()方法得到Getter和Setter方法 PropertyDescriptor依赖于o……
<<上一篇
下一篇>>
文章目录
关闭
目 录