-
Notifications
You must be signed in to change notification settings - Fork 661
/
ReflectionUtils.java
358 lines (294 loc) · 11 KB
/
ReflectionUtils.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.util;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.springframework.beans.BeanUtils;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils.FieldFilter;
/**
* Spring Data specific reflection utility methods and classes.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @since 1.5
*/
@UtilityClass
public class ReflectionUtils {
private static final boolean KOTLIN_IS_PRESENT = ClassUtils.isPresent("kotlin.Unit",
BeanUtils.class.getClassLoader());
/**
* Creates an instance of the class with the given fully qualified name or returns the given default instance if the
* class cannot be loaded or instantiated.
*
* @param classname the fully qualified class name to create an instance for.
* @param defaultInstance the instance to fall back to in case the given class cannot be loaded or instantiated.
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T createInstanceIfPresent(String classname, T defaultInstance) {
try {
Class<?> type = ClassUtils.forName(classname, ClassUtils.getDefaultClassLoader());
return (T) BeanUtils.instantiateClass(type);
} catch (Exception e) {
return defaultInstance;
}
}
/**
* A {@link FieldFilter} that has a description.
*
* @author Oliver Gierke
*/
public interface DescribedFieldFilter extends FieldFilter {
/**
* Returns the description of the field filter. Used in exceptions being thrown in case uniqueness shall be enforced
* on the field filter.
*
* @return
*/
String getDescription();
}
/**
* A {@link FieldFilter} for a given annotation.
*
* @author Oliver Gierke
*/
@RequiredArgsConstructor
public static class AnnotationFieldFilter implements DescribedFieldFilter {
private final @NonNull Class<? extends Annotation> annotationType;
/*
* (non-Javadoc)
* @see org.springframework.util.ReflectionUtils.FieldFilter#matches(java.lang.reflect.Field)
*/
public boolean matches(Field field) {
return AnnotationUtils.getAnnotation(field, annotationType) != null;
}
/*
* (non-Javadoc)
* @see org.springframework.data.util.ReflectionUtils.DescribedFieldFilter#getDescription()
*/
public String getDescription() {
return String.format("Annotation filter for %s", annotationType.getName());
}
}
/**
* Finds the first field on the given class matching the given {@link FieldFilter}.
*
* @param type must not be {@literal null}.
* @param filter must not be {@literal null}.
* @return the field matching the filter or {@literal null} in case no field could be found.
*/
@Nullable
public static Field findField(Class<?> type, FieldFilter filter) {
return findField(type, new DescribedFieldFilter() {
public boolean matches(Field field) {
return filter.matches(field);
}
public String getDescription() {
return String.format("FieldFilter %s", filter.toString());
}
}, false);
}
/**
* Finds the field matching the given {@link DescribedFieldFilter}. Will make sure there's only one field matching the
* filter.
*
* @see #findField(Class, DescribedFieldFilter, boolean)
* @param type must not be {@literal null}.
* @param filter must not be {@literal null}.
* @return the field matching the given {@link DescribedFieldFilter} or {@literal null} if none found.
* @throws IllegalStateException in case more than one matching field is found
*/
@Nullable
public static Field findField(Class<?> type, DescribedFieldFilter filter) {
return findField(type, filter, true);
}
/**
* Finds the field matching the given {@link DescribedFieldFilter}. Will make sure there's only one field matching the
* filter in case {@code enforceUniqueness} is {@literal true}.
*
* @param type must not be {@literal null}.
* @param filter must not be {@literal null}.
* @param enforceUniqueness whether to enforce uniqueness of the field
* @return the field matching the given {@link DescribedFieldFilter} or {@literal null} if none found.
* @throws IllegalStateException if enforceUniqueness is true and more than one matching field is found
*/
@Nullable
public static Field findField(Class<?> type, DescribedFieldFilter filter, boolean enforceUniqueness) {
Assert.notNull(type, "Type must not be null!");
Assert.notNull(filter, "Filter must not be null!");
Class<?> targetClass = type;
Field foundField = null;
while (targetClass != Object.class) {
for (Field field : targetClass.getDeclaredFields()) {
if (!filter.matches(field)) {
continue;
}
if (!enforceUniqueness) {
return field;
}
if (foundField != null && enforceUniqueness) {
throw new IllegalStateException(filter.getDescription());
}
foundField = field;
}
targetClass = targetClass.getSuperclass();
}
return foundField;
}
/**
* Finds the field of the given name on the given type.
*
* @param type must not be {@literal null}.
* @param name must not be {@literal null} or empty.
* @return
* @throws IllegalArgumentException in case the field can't be found.
*/
public static Field findRequiredField(Class<?> type, String name) {
Field result = org.springframework.util.ReflectionUtils.findField(type, name);
if (result == null) {
throw new IllegalArgumentException(String.format("Unable to find field %s on %s!", name, type));
}
return result;
}
/**
* Sets the given field on the given object to the given value. Will make sure the given field is accessible.
*
* @param field must not be {@literal null}.
* @param target must not be {@literal null}.
* @param value
*/
public static void setField(Field field, Object target, @Nullable Object value) {
org.springframework.util.ReflectionUtils.makeAccessible(field);
org.springframework.util.ReflectionUtils.setField(field, target, value);
}
/**
* Finds a constructor on the given type that matches the given constructor arguments.
*
* @param type must not be {@literal null}.
* @param constructorArguments must not be {@literal null}.
* @return a {@link Constructor} that is compatible with the given arguments.
*/
public static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) {
Assert.notNull(type, "Target type must not be null!");
Assert.notNull(constructorArguments, "Constructor arguments must not be null!");
return Arrays.stream(type.getDeclaredConstructors())//
.filter(constructor -> argumentsMatch(constructor.getParameterTypes(), constructorArguments))//
.findFirst();
}
/**
* Returns the method with the given name of the given class and parameter types.
*
* @param type must not be {@literal null}.
* @param name must not be {@literal null}.
* @param parameterTypes must not be {@literal null}.
* @return
* @throws IllegalArgumentException in case the method cannot be resolved.
*/
public static Method findRequiredMethod(Class<?> type, String name, Class<?>... parameterTypes) {
Method result = org.springframework.util.ReflectionUtils.findMethod(type, name, parameterTypes);
if (result == null) {
String parameterTypeNames = Arrays.stream(parameterTypes) //
.map(Object::toString) //
.collect(Collectors.joining(", "));
throw new IllegalArgumentException(
String.format("Unable to find method %s(%s)on %s!", name, parameterTypeNames, type));
}
return result;
}
/**
* Returns a {@link Stream} of the return and parameters types of the given {@link Method}.
*
* @param method must not be {@literal null}.
* @return
* @since 2.0
*/
public static Stream<Class<?>> returnTypeAndParameters(Method method) {
Assert.notNull(method, "Method must not be null!");
Stream<Class<?>> returnType = Stream.of(method.getReturnType());
Stream<Class<?>> parameterTypes = Arrays.stream(method.getParameterTypes());
return Stream.concat(returnType, parameterTypes);
}
/**
* Returns the {@link Method} with the given name and parameters declared on the given type, if available.
*
* @param type must not be {@literal null}.
* @param name must not be {@literal null} or empty.
* @param parameterTypes must not be {@literal null}.
* @return
* @since 2.0
*/
public static Optional<Method> getMethod(Class<?> type, String name, ResolvableType... parameterTypes) {
Assert.notNull(type, "Type must not be null!");
Assert.hasText(name, "Name must not be null or empty!");
Assert.notNull(parameterTypes, "Parameter types must not be null!");
List<Class<?>> collect = Arrays.stream(parameterTypes)//
.map(ResolvableType::getRawClass)//
.collect(Collectors.toList());
Method method = org.springframework.util.ReflectionUtils.findMethod(type, name,
collect.toArray(new Class<?>[collect.size()]));
return Optional.ofNullable(method)//
.filter(it -> IntStream.range(0, it.getParameterCount())//
.allMatch(index -> ResolvableType.forMethodParameter(it, index).equals(parameterTypes[index])));
}
private static boolean argumentsMatch(Class<?>[] parameterTypes, Object[] arguments) {
if (parameterTypes.length != arguments.length) {
return false;
}
int index = 0;
for (Class<?> argumentType : parameterTypes) {
Object argument = arguments[index];
// Reject nulls for primitives
if (argumentType.isPrimitive() && argument == null) {
return false;
}
// Type check if argument is not null
if (argument != null && !ClassUtils.isAssignableValue(argumentType, argument)) {
return false;
}
index++;
}
return true;
}
/**
* Return true if the specified class is a Kotlin one.
*
* @return {@literal true} if {@code type} is a Kotlin class.
* @since 2.0
*/
public static boolean isKotlinClass(Class<?> type) {
return KOTLIN_IS_PRESENT && Arrays.stream(type.getDeclaredAnnotations()) //
.map(Annotation::annotationType) //
.anyMatch(annotation -> annotation.getName().equals("kotlin.Metadata"));
}
}