1 /*
2  * Copyright 2012-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.util;
17
18 import lombok.NonNull;
19 import lombok.RequiredArgsConstructor;
20 import lombok.experimental.UtilityClass;
21
22 import java.lang.annotation.Annotation;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.Method;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import java.util.stream.IntStream;
31 import java.util.stream.Stream;
32
33 import org.springframework.beans.BeanUtils;
34 import org.springframework.core.KotlinDetector;
35 import org.springframework.core.MethodParameter;
36 import org.springframework.core.ResolvableType;
37 import org.springframework.core.annotation.AnnotationUtils;
38 import org.springframework.lang.Nullable;
39 import org.springframework.util.Assert;
40 import org.springframework.util.ClassUtils;
41 import org.springframework.util.ReflectionUtils.FieldFilter;
42
43 /**
44  * Spring Data specific reflection utility methods and classes.
45  *
46  * @author Oliver Gierke
47  * @author Thomas Darimont
48  * @author Christoph Strobl
49  * @author Mark Paluch
50  * @since 1.5
51  */

52 @UtilityClass
53 public class ReflectionUtils {
54
55     /**
56      * Creates an instance of the class with the given fully qualified name or returns the given default instance if the
57      * class cannot be loaded or instantiated.
58      *
59      * @param classname the fully qualified class name to create an instance for.
60      * @param defaultInstance the instance to fall back to in case the given class cannot be loaded or instantiated.
61      * @return
62      */

63     @SuppressWarnings("unchecked")
64     public static <T> T createInstanceIfPresent(String classname, T defaultInstance) {
65
66         try {
67             Class<?> type = ClassUtils.forName(classname, ClassUtils.getDefaultClassLoader());
68             return (T) BeanUtils.instantiateClass(type);
69         } catch (Exception e) {
70             return defaultInstance;
71         }
72     }
73
74     /**
75      * Check whether the given {@code type} represents a void type such as {@code void}, {@link Void} or Kotlin
76      * {@code Unit}.
77      *
78      * @param type must not be {@literal null}.
79      * @return whether the given the type is a void type.
80      * @since 2.3.3
81      */

82     public static boolean isVoid(Class<?> type) {
83
84         if (type == Void.class || Void.TYPE == type) {
85             return true;
86         }
87
88         if (type.getName().equals("kotlin.Unit")) {
89             return true;
90         }
91
92         return false;
93     }
94
95     /**
96      * A {@link FieldFilter} that has a description.
97      *
98      * @author Oliver Gierke
99      */

100     public interface DescribedFieldFilter extends FieldFilter {
101
102         /**
103          * Returns the description of the field filter. Used in exceptions being thrown in case uniqueness shall be enforced
104          * on the field filter.
105          *
106          * @return
107          */

108         String getDescription();
109     }
110
111     /**
112      * A {@link FieldFilter} for a given annotation.
113      *
114      * @author Oliver Gierke
115      */

116     @RequiredArgsConstructor
117     public static class AnnotationFieldFilter implements DescribedFieldFilter {
118
119         private final @NonNull Class<? extends Annotation> annotationType;
120
121         /*
122          * (non-Javadoc)
123          * @see org.springframework.util.ReflectionUtils.FieldFilter#matches(java.lang.reflect.Field)
124          */

125         public boolean matches(Field field) {
126             return AnnotationUtils.getAnnotation(field, annotationType) != null;
127         }
128
129         /*
130          * (non-Javadoc)
131          * @see org.springframework.data.util.ReflectionUtils.DescribedFieldFilter#getDescription()
132          */

133         public String getDescription() {
134             return String.format("Annotation filter for %s", annotationType.getName());
135         }
136     }
137
138     /**
139      * Finds the first field on the given class matching the given {@link FieldFilter}.
140      *
141      * @param type must not be {@literal null}.
142      * @param filter must not be {@literal null}.
143      * @return the field matching the filter or {@literal null} in case no field could be found.
144      */

145     @Nullable
146     public static Field findField(Class<?> type, FieldFilter filter) {
147
148         return findField(type, new DescribedFieldFilter() {
149
150             public boolean matches(Field field) {
151                 return filter.matches(field);
152             }
153
154             public String getDescription() {
155                 return String.format("FieldFilter %s", filter.toString());
156             }
157         }, false);
158     }
159
160     /**
161      * Finds the field matching the given {@link DescribedFieldFilter}. Will make sure there's only one field matching the
162      * filter.
163      *
164      * @see #findField(Class, DescribedFieldFilter, boolean)
165      * @param type must not be {@literal null}.
166      * @param filter must not be {@literal null}.
167      * @return the field matching the given {@link DescribedFieldFilter} or {@literal nullif none found.
168      * @throws IllegalStateException in case more than one matching field is found
169      */

170     @Nullable
171     public static Field findField(Class<?> type, DescribedFieldFilter filter) {
172         return findField(type, filter, true);
173     }
174
175     /**
176      * Finds the field matching the given {@link DescribedFieldFilter}. Will make sure there's only one field matching the
177      * filter in case {@code enforceUniqueness} is {@literal true}.
178      *
179      * @param type must not be {@literal null}.
180      * @param filter must not be {@literal null}.
181      * @param enforceUniqueness whether to enforce uniqueness of the field
182      * @return the field matching the given {@link DescribedFieldFilter} or {@literal nullif none found.
183      * @throws IllegalStateException if enforceUniqueness is true and more than one matching field is found
184      */

185     @Nullable
186     public static Field findField(Class<?> type, DescribedFieldFilter filter, boolean enforceUniqueness) {
187
188         Assert.notNull(type, "Type must not be null!");
189         Assert.notNull(filter, "Filter must not be null!");
190
191         Class<?> targetClass = type;
192         Field foundField = null;
193
194         while (targetClass != Object.class) {
195
196             for (Field field : targetClass.getDeclaredFields()) {
197
198                 if (!filter.matches(field)) {
199                     continue;
200                 }
201
202                 if (!enforceUniqueness) {
203                     return field;
204                 }
205
206                 if (foundField != null && enforceUniqueness) {
207                     throw new IllegalStateException(filter.getDescription());
208                 }
209
210                 foundField = field;
211             }
212
213             targetClass = targetClass.getSuperclass();
214         }
215
216         return foundField;
217     }
218
219     /**
220      * Finds the field of the given name on the given type.
221      *
222      * @param type must not be {@literal null}.
223      * @param name must not be {@literal null} or empty.
224      * @return
225      * @throws IllegalArgumentException in case the field can't be found.
226      */

227     public static Field findRequiredField(Class<?> type, String name) {
228
229         Field result = org.springframework.util.ReflectionUtils.findField(type, name);
230
231         if (result == null) {
232             throw new IllegalArgumentException(String.format("Unable to find field %s on %s!", name, type));
233         }
234
235         return result;
236     }
237
238     /**
239      * Sets the given field on the given object to the given value. Will make sure the given field is accessible.
240      *
241      * @param field must not be {@literal null}.
242      * @param target must not be {@literal null}.
243      * @param value
244      */

245     public static void setField(Field field, Object target, @Nullable Object value) {
246
247         org.springframework.util.ReflectionUtils.makeAccessible(field);
248         org.springframework.util.ReflectionUtils.setField(field, target, value);
249     }
250
251     /**
252      * Finds a constructor on the given type that matches the given constructor arguments.
253      *
254      * @param type must not be {@literal null}.
255      * @param constructorArguments must not be {@literal null}.
256      * @return a {@link Constructor} that is compatible with the given arguments.
257      */

258     public static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) {
259
260         Assert.notNull(type, "Target type must not be null!");
261         Assert.notNull(constructorArguments, "Constructor arguments must not be null!");
262
263         return Arrays.stream(type.getDeclaredConstructors())//
264                 .filter(constructor -> argumentsMatch(constructor.getParameterTypes(), constructorArguments))//
265                 .findFirst();
266     }
267
268     /**
269      * Returns the method with the given name of the given class and parameter types.
270      *
271      * @param type must not be {@literal null}.
272      * @param name must not be {@literal null}.
273      * @param parameterTypes must not be {@literal null}.
274      * @return
275      * @throws IllegalArgumentException in case the method cannot be resolved.
276      */

277     public static Method findRequiredMethod(Class<?> type, String name, Class<?>... parameterTypes) {
278
279         Method result = org.springframework.util.ReflectionUtils.findMethod(type, name, parameterTypes);
280
281         if (result == null) {
282
283             String parameterTypeNames = Arrays.stream(parameterTypes) //
284                     .map(Object::toString) //
285                     .collect(Collectors.joining(", "));
286
287             throw new IllegalArgumentException(
288                     String.format("Unable to find method %s(%s)on %s!", name, parameterTypeNames, type));
289         }
290
291         return result;
292     }
293
294     /**
295      * Returns a {@link Stream} of the return and parameters types of the given {@link Method}.
296      *
297      * @param method must not be {@literal null}.
298      * @return
299      * @since 2.0
300      */

301     public static Stream<Class<?>> returnTypeAndParameters(Method method) {
302
303         Assert.notNull(method, "Method must not be null!");
304
305         Stream<Class<?>> returnType = Stream.of(method.getReturnType());
306         Stream<Class<?>> parameterTypes = Arrays.stream(method.getParameterTypes());
307
308         return Stream.concat(returnType, parameterTypes);
309     }
310
311     /**
312      * Returns the {@link Method} with the given name and parameters declared on the given type, if available.
313      *
314      * @param type must not be {@literal null}.
315      * @param name must not be {@literal null} or empty.
316      * @param parameterTypes must not be {@literal null}.
317      * @return
318      * @since 2.0
319      */

320     public static Optional<Method> getMethod(Class<?> type, String name, ResolvableType... parameterTypes) {
321
322         Assert.notNull(type, "Type must not be null!");
323         Assert.hasText(name, "Name must not be null or empty!");
324         Assert.notNull(parameterTypes, "Parameter types must not be null!");
325
326         List<Class<?>> collect = Arrays.stream(parameterTypes)//
327                 .map(ResolvableType::getRawClass)//
328                 .collect(Collectors.toList());
329
330         Method method = org.springframework.util.ReflectionUtils.findMethod(type, name,
331                 collect.toArray(new Class<?>[collect.size()]));
332
333         return Optional.ofNullable(method)//
334                 .filter(it -> IntStream.range(0, it.getParameterCount())//
335                         .allMatch(index -> ResolvableType.forMethodParameter(it, index).equals(parameterTypes[index])));
336     }
337
338     private static boolean argumentsMatch(Class<?>[] parameterTypes, Object[] arguments) {
339
340         if (parameterTypes.length != arguments.length) {
341             return false;
342         }
343
344         int index = 0;
345
346         for (Class<?> argumentType : parameterTypes) {
347
348             Object argument = arguments[index];
349
350             // Reject nulls for primitives
351             if (argumentType.isPrimitive() && argument == null) {
352                 return false;
353             }
354
355             // Type check if argument is not null
356             if (argument != null && !ClassUtils.isAssignableValue(argumentType, argument)) {
357                 return false;
358             }
359
360             index++;
361         }
362
363         return true;
364     }
365
366     /**
367      * Return {@literal trueif the specified class is a Kotlin one.
368      *
369      * @return {@literal trueif {@code type} is a Kotlin class.
370      * @since 2.0
371      * @deprecated since 2.3, use {@link KotlinDetector#isKotlinType(Class)} instead.
372      */

373     @Deprecated
374     public static boolean isKotlinClass(Class<?> type) {
375         return KotlinDetector.isKotlinType(type);
376     }
377
378     /**
379      * Return {@literal trueif the specified class is a supported Kotlin class. Currently supported are only regular
380      * Kotlin classes. Other class types (synthetic, SAM, lambdas) are not supported via reflection.
381      *
382      * @return {@literal trueif {@code type} is a supported Kotlin class.
383      * @since 2.0
384      * @deprecated since 2.3, use {@link KotlinReflectionUtils#isSupportedKotlinClass(Class)} instead.
385      */

386     @Deprecated
387     public static boolean isSupportedKotlinClass(Class<?> type) {
388         return KotlinReflectionUtils.isSupportedKotlinClass(type);
389     }
390
391     /**
392      * Returns {@literal} whether the given {@link MethodParameter} is nullable. Nullable parameters are reference types
393      * and ones that are defined in Kotlin as such.
394      *
395      * @return {@literal trueif {@link MethodParameter} is nullable.
396      * @since 2.0
397      */

398     public static boolean isNullable(MethodParameter parameter) {
399
400         if (isVoid(parameter.getParameterType())) {
401             return true;
402         }
403
404         if (isSupportedKotlinClass(parameter.getDeclaringClass())) {
405             return KotlinReflectionUtils.isNullable(parameter);
406         }
407
408         return !parameter.getParameterType().isPrimitive();
409     }
410
411     /**
412      * Get default value for a primitive type.
413      *
414      * @param type must not be {@literal null}.
415      * @return boxed primitive default value.
416      * @since 2.1
417      */

418     public static Object getPrimitiveDefault(Class<?> type) {
419
420         if (type == Byte.TYPE || type == Byte.class) {
421             return (byte) 0;
422         }
423
424         if (type == Short.TYPE || type == Short.class) {
425             return (short) 0;
426         }
427
428         if (type == Integer.TYPE || type == Integer.class) {
429             return 0;
430         }
431
432         if (type == Long.TYPE || type == Long.class) {
433             return 0L;
434         }
435
436         if (type == Float.TYPE || type == Float.class) {
437             return 0F;
438         }
439
440         if (type == Double.TYPE || type == Double.class) {
441             return 0D;
442         }
443
444         if (type == Character.TYPE || type == Character.class) {
445             return '\u0000';
446         }
447
448         if (type == Boolean.TYPE) {
449             return Boolean.FALSE;
450         }
451
452         throw new IllegalArgumentException(String.format("Primitive type %s not supported!", type));
453     }
454
455 }
456