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

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

PropertyDescriptor依赖于openbeans库

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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);
}
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :