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