1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.lang.reflect.AccessibleObject;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29
30
31
32
33
34
35
36 public final class ReflectionUtils {
37
38
39
40
41 public static Field getFieldByNameIncludingSuperclasses(String fieldName, Class<?> clazz) {
42 Field retValue = null;
43
44 try {
45 retValue = clazz.getDeclaredField(fieldName);
46 } catch (NoSuchFieldException e) {
47 Class<?> superclass = clazz.getSuperclass();
48
49 if (superclass != null) {
50 retValue = getFieldByNameIncludingSuperclasses(fieldName, superclass);
51 }
52 }
53
54 return retValue;
55 }
56
57 public static List<Field> getFieldsIncludingSuperclasses(Class<?> clazz) {
58 List<Field> fields = new ArrayList<>(Arrays.asList(clazz.getDeclaredFields()));
59
60 Class<?> superclass = clazz.getSuperclass();
61
62 if (superclass != null) {
63 fields.addAll(getFieldsIncludingSuperclasses(superclass));
64 }
65
66 return fields;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80 public static Method getSetter(String fieldName, Class<?> clazz) {
81 Method[] methods = clazz.getMethods();
82
83 fieldName = "set" + StringUtils.capitalizeFirstLetter(fieldName);
84
85 for (Method method : methods) {
86 if (method.getName().equals(fieldName) && isSetter(method)) {
87 return method;
88 }
89 }
90
91 return null;
92 }
93
94
95
96
97
98 public static List<Method> getSetters(Class<?> clazz) {
99 Method[] methods = clazz.getMethods();
100
101 List<Method> list = new ArrayList<>();
102
103 for (Method method : methods) {
104 if (isSetter(method)) {
105 list.add(method);
106 }
107 }
108
109 return list;
110 }
111
112
113
114
115
116 public static Class<?> getSetterType(Method method) {
117 if (!isSetter(method)) {
118 throw new RuntimeException("The method "
119 + method.getDeclaringClass().getName() + "." + method.getName() + " is not a setter.");
120 }
121
122 return method.getParameterTypes()[0];
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 public static void setVariableValueInObject(Object object, String variable, Object value)
138 throws IllegalAccessException {
139 Field field = getFieldByNameIncludingSuperclasses(variable, object.getClass());
140
141 field.setAccessible(true);
142
143 field.set(object, value);
144 }
145
146
147
148
149
150
151
152
153
154 public static Object getValueIncludingSuperclasses(String variable, Object object) throws IllegalAccessException {
155
156 Field field = getFieldByNameIncludingSuperclasses(variable, object.getClass());
157
158 field.setAccessible(true);
159
160 return field.get(object);
161 }
162
163
164
165
166
167
168
169
170 public static Map<String, Object> getVariablesAndValuesIncludingSuperclasses(Object object)
171 throws IllegalAccessException {
172 Map<String, Object> map = new HashMap<>();
173
174 gatherVariablesAndValuesIncludingSuperclasses(object, map);
175
176 return map;
177 }
178
179
180
181
182
183 public static boolean isSetter(Method method) {
184 return method.getReturnType().equals(Void.TYPE)
185 &&
186 !Modifier.isStatic(method.getModifiers())
187 && method.getParameterTypes().length == 1;
188 }
189
190
191
192
193
194
195
196 private static void gatherVariablesAndValuesIncludingSuperclasses(Object object, Map<String, Object> map)
197 throws IllegalAccessException {
198
199 Class<?> clazz = object.getClass();
200
201 if (Float.parseFloat(System.getProperty("java.specification.version")) >= 11
202 && Class.class.getCanonicalName().equals(clazz.getCanonicalName())) {
203
204
205 return;
206 }
207
208 Field[] fields = clazz.getDeclaredFields();
209
210 AccessibleObject.setAccessible(fields, true);
211
212 for (Field field : fields) {
213 map.put(field.getName(), field.get(object));
214 }
215
216 Class<?> superclass = clazz.getSuperclass();
217
218 if (!Object.class.equals(superclass)) {
219 gatherVariablesAndValuesIncludingSuperclasses(superclass, map);
220 }
221 }
222 }