-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathClassUtils.java
835 lines (774 loc) · 33.7 KB
/
ClassUtils.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/**
* Copyright (C) 2019-2023 Expedia, Inc.
*
* 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 com.expediagroup.transformer.utils;
import static java.lang.invoke.LambdaMetafactory.metafactory;
import static java.lang.invoke.MethodHandles.lookup;
import static java.lang.invoke.MethodHandles.privateLookupIn;
import static java.lang.invoke.MethodType.methodType;
import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isPrivate;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Collections.max;
import static java.util.Comparator.comparing;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;
import static java.util.Set.of;
import static java.util.stream.Collectors.toList;
import static com.expediagroup.transformer.constant.Filters.IS_FINAL_AND_NOT_STATIC_FIELD;
import static com.expediagroup.transformer.constant.Filters.IS_NOT_FINAL_AND_NOT_STATIC_FIELD;
import static com.expediagroup.transformer.constant.Filters.IS_NOT_FINAL_FIELD;
import static com.expediagroup.transformer.validator.Validator.notNull;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Currency;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;
import com.expediagroup.transformer.base.Defaults;
import com.expediagroup.transformer.cache.CacheManager;
import com.expediagroup.transformer.cache.CacheManagerFactory;
import com.expediagroup.transformer.constant.ClassType;
import com.expediagroup.transformer.error.InstanceCreationException;
import com.expediagroup.transformer.error.InvalidBeanException;
import com.expediagroup.transformer.error.MissingMethodException;
/**
* Reflection utils for Class objects.
*/
public final class ClassUtils {
/**
* Default method name used by a Builder for creating an object.
*/
public static final String BUILD_METHOD_NAME = "build";
/**
* Custom special types.
*/
public static final Set<Class<?>> CUSTOM_SPECIAL_TYPES = new HashSet<>();
/**
* Class nullability error message constant.
*/
private static final String CLAZZ_CANNOT_BE_NULL = "clazz cannot be null!";
/**
* CacheManager class {@link CacheManager}.
*/
private static final CacheManager CACHE_MANAGER = CacheManagerFactory.getCacheManager("classUtils");
/**
* Primitive types list.
*/
private static final Set<Class<?>> PRIMITIVE_TYPES = of(String.class, Boolean.class, Integer.class, Long.class,
Double.class, BigDecimal.class, BigInteger.class, Short.class, Float.class, Character.class, Byte.class, Void.class);
/**
* Special type list.
*/
private static final Set<Class<?>> SPECIAL_TYPES = of(Currency.class, Locale.class, Temporal.class, Date.class, Properties.class);
/**
* Method Handles lookup.
*/
private static final MethodHandles.Lookup METHOD_HANDLES_LOOKUP = lookup();
/**
* Reflection utils instance {@link ReflectionUtils}.
*/
private final ReflectionUtils reflectionUtils;
/**
* Default constructor.
*/
public ClassUtils() {
this.reflectionUtils = new ReflectionUtils();
}
/**
* Checks if an object is a primitive or special type.
* @param clazz the class to check
* @return true if is primitive or special type, false otherwise
*/
public boolean isPrimitiveOrSpecialType(final Class<?> clazz) {
if (isNull(clazz)) {
return false;
}
final String cacheKey = "isPrimitiveOrSpecial-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class).orElseGet(() -> {
final Boolean res = isPrimitiveType(clazz) || isSpecialType(clazz);
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Checks if an object is a special type.
* @param clazz the class to check
* @return true if is special type, false otherwise
*/
public boolean isPrimitiveType(final Class<?> clazz) {
final String cacheKey = "isPrimitive-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class).orElseGet(() -> {
final Boolean res = clazz.isPrimitive() || PRIMITIVE_TYPES.contains(clazz) || clazz.isEnum();
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Checks if an object is a primitive type array.
* @param clazz the class to check
* @return true if is primitive type array, false otherwise
*/
public boolean isPrimitiveTypeArray(final Class<?> clazz) {
final String cacheKey = "isPrimitiveTypeArray-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class).orElseGet(() -> {
final Boolean res = clazz.isArray() && isPrimitiveType(clazz.getComponentType());
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Checks if an object is a special type.
* The label "Special classes" refers to all objects that has to be copied without applying any special transformation.
* @param clazz the class to check
* @return true if is special type, false otherwise
*/
public boolean isSpecialType(final Class<?> clazz) {
return clazz.isSynthetic() || SPECIAL_TYPES.stream()
.anyMatch(specialTypeClazz -> clazz.equals(specialTypeClazz) || specialTypeClazz.isAssignableFrom(clazz))
|| isCustomSpecialType(clazz);
}
/**
* Checks if the given class is a custom provided special type.
* @param clazz the class to check
* @return true if is special type, false otherwise
*/
private boolean isCustomSpecialType(final Class<?> clazz) {
return CUSTOM_SPECIAL_TYPES.stream()
.anyMatch(customTypeClazz -> clazz.equals(customTypeClazz) || customTypeClazz.isAssignableFrom(clazz));
}
/**
* Checks if the given type is a {@link Double}.
* @param type the class to check
* @return true if is Double
*/
public static boolean isDouble(final Class<?> type) {
return Double.class.isAssignableFrom(type) || type == double.class;
}
/**
* Checks if the given type is a {@link Float}.
* @param type the class to check
* @return true if is Float
*/
public static boolean isFloat(final Class<?> type) {
return Float.class.isAssignableFrom(type) || type == float.class;
}
/**
* Checks if the given type is a {@link Long}.
* @param type the class to check
* @return true if is Long
*/
public static boolean isLong(final Class<?> type) {
return Long.class.isAssignableFrom(type) || type == long.class;
}
/**
* Checks if the given type is a {@link Short}.
* @param type the class to check
* @return true if is Short
*/
public static boolean isShort(final Class<?> type) {
return Short.class.isAssignableFrom(type) || type == short.class;
}
/**
* Checks if the given type is an {@link Integer}.
* @param type the class to check
* @return true if is Integer
*/
public static boolean isInt(final Class<?> type) {
return Integer.class.isAssignableFrom(type) || type == int.class;
}
/**
* Checks if the given type is a {@link Byte}.
* @param type the class to check
* @return true if is Byte
*/
public static boolean isByte(final Class<?> type) {
return Byte.class.isAssignableFrom(type) || type == byte.class;
}
/**
* Checks if the given type is a {@link Character}.
* @param type the class to check
* @return true if is Character
*/
public static boolean isChar(final Class<?> type) {
return Character.class.isAssignableFrom(type) || type == char.class;
}
/**
* Checks if the given type is a {@link Boolean}.
* @param type the class to check
* @return true if is Boolean
*/
public static boolean isBoolean(final Class<?> type) {
return Boolean.class.isAssignableFrom(type) || type == boolean.class;
}
/**
* Checks if the given type is a String.
* @param type the class to check
* @return true if is String
*/
public static boolean isString(final Class<?> type) {
return type == String.class;
}
/**
* Checks if the given type is a {@link BigInteger}.
* @param type the class to check
* @return true if is String
*/
public static boolean isBigInteger(final Class<?> type) {
return type == BigInteger.class;
}
/**
* Checks if the given type is a {@link BigDecimal}.
* @param type the class to check
* @return true if is String
*/
public static boolean isBigDecimal(final Class<?> type) {
return type == BigDecimal.class;
}
/**
* Checks if the given type is a byte[].
* @param type the class to check
* @return true if is String
*/
public static boolean isByteArray(final Class<?> type) {
return type == byte[].class;
}
/**
* Return the private final fields of a class.
* @param clazz class from which gets the field
* @return a list of private final fields.
*/
@SuppressWarnings("unchecked")
public List<Field> getPrivateFinalFields(final Class<?> clazz) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
final String cacheKey = "PrivateFinalFields-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, List.class).orElseGet(() -> {
final List<Field> res = new ArrayList<>();
if (hasSuperclass(clazz.getSuperclass())) {
res.addAll(getPrivateFinalFields(clazz.getSuperclass()));
}
res.addAll(stream(getDeclaredFields(clazz))
//.parallel()
.filter(IS_FINAL_AND_NOT_STATIC_FIELD)
.collect(toList()));
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Checks if a class has a clazz different from {@link Object}.
* @param clazz the class to check.
* @return true if the given class extends another class different from object
*/
private boolean hasSuperclass(final Class<?> clazz) {
return nonNull(clazz) && !clazz.equals(Object.class);
}
/**
* Return the total fields matching with the given predicate.
* @param clazz class from which gets the field
* @param predicate the condition that needs to match
* @return the total matching item.
*/
public int getTotalFields(final Class<?> clazz, final Predicate<? super Field> predicate) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
final String cacheKey = "TotalFields-" + clazz.getName() + '-' + predicate;
return CACHE_MANAGER.getFromCache(cacheKey, Integer.class).orElseGet(() -> {
List<Field> declaredFields = getDeclaredFields(clazz, true);
int res = ofNullable(predicate)
.map(filter -> (int) declaredFields.stream().filter(filter).count())
.orElseGet(declaredFields::size);
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Return the private fields of a class.
* @param clazz class from which gets the field
* @return a list of private final fields.
*/
public List<Field> getPrivateFields(final Class<?> clazz) {
return getPrivateFields(clazz, false);
}
/**
* Return the private fields of a class.
* @param clazz class from which gets the field
* @param skipFinal if true it skips the final fields otherwise all private fields are retrieved.
* @return a list of private fields.
*/
@SuppressWarnings("unchecked")
public List<Field> getPrivateFields(final Class<?> clazz, final boolean skipFinal) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
final String cacheKey = "PrivateFields-" + clazz.getName() + "-skipFinal-" + skipFinal;
return CACHE_MANAGER.getFromCache(cacheKey, List.class).orElseGet(() -> {
final List<Field> res = new ArrayList<>();
if (hasSuperclass(clazz.getSuperclass())) {
res.addAll(getPrivateFields(clazz.getSuperclass(), skipFinal));
}
res.addAll(stream(getDeclaredFields(clazz))
//.parallel()
.filter(field -> isPrivate(field.getModifiers())
&& (!skipFinal || !isFinal(field.getModifiers()))
&& !isStatic(field.getModifiers()))
.collect(toList()));
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Return the fields of a class.
* @param clazz class from which gets the field
* @param skipStatic if true it skips the static fields otherwise all private fields are retrieved.
* @return a list of class fields.
*/
@SuppressWarnings("unchecked")
public List<Field> getDeclaredFields(final Class<?> clazz, final boolean skipStatic) {
final String cacheKey = "DeclaredFields-" + clazz.getName() + "-skipStatic-" + skipStatic;
return CACHE_MANAGER.getFromCache(cacheKey, List.class).orElseGet(() -> {
final List<Field> res = new ArrayList<>();
if (hasSuperclass(clazz.getSuperclass())) {
res.addAll(getDeclaredFields(clazz.getSuperclass(), skipStatic));
}
stream(getDeclaredFields(clazz))
.filter(field -> !skipStatic || !isStatic(field.getModifiers()))
.forEach(field -> {
field.setAccessible(true);
res.add(field);
});
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Returns the class fields.
* @param clazz the class from which gets the field.
* @return a list of class fields
*/
private Field[] getDeclaredFields(final Class<?> clazz) {
final String cacheKey = "ClassDeclaredFields-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Field[].class).orElseGet(() -> {
Field[] res = clazz.getDeclaredFields();
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Returns the concrete class of a field.
* @param field the field for which the concrete class has to be retrieved.
* @param objectInstance the object instance.
* @param <T> the object instance class.
* @return the concrete class of a field
*/
public <T> Class<?> getFieldClass(final Field field, final T objectInstance) {
return getConcreteClass(field, reflectionUtils.getFieldValue(objectInstance, field));
}
/**
* Returns the concrete class of a field.
* @param field the field for which the concrete class has to be retrieved.
* @param fieldValue the field value.
* @return the concrete class of a field
*/
public Class<?> getConcreteClass(final Field field, final Object fieldValue) {
final String cacheKey = "ConcreteFieldClass-" + field.getName() + "-" + field.getDeclaringClass();
return CACHE_MANAGER.getFromCache(cacheKey, Class.class).orElseGet(() -> {
Class<?> concreteType = field.getType();
boolean isFieldValueNull = isNull(fieldValue);
if (field.getType().isInterface()) {
concreteType = isFieldValueNull ? Object.class : fieldValue.getClass();
}
if (!isFieldValueNull) {
CACHE_MANAGER.cacheObject(cacheKey, concreteType);
}
return concreteType;
});
}
/**
* Checks if the destination class has accessible constructor.
* @param targetClass the destination object class
* @param <K> the target object type
* @return true if the target class uses the builder pattern
*/
public <K> boolean hasAccessibleConstructors(final Class<K> targetClass) {
final String cacheKey = "HasAccessibleConstructors-" + targetClass.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class).orElseGet(() -> {
final boolean res = stream(targetClass.getDeclaredConstructors()).anyMatch(constructor -> isPublic(constructor.getModifiers()));
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Retrieves all classes defined into the given one.
* @param clazz class where we search for a nested class
* @return all classes defined into the given one
*/
public Class[] getDeclaredClasses(final Class<?> clazz) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
String cacheKey = "DeclaredClasses-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Class[].class).orElseGet(() -> {
Class[] declaredClasses = clazz.getDeclaredClasses();
CACHE_MANAGER.cacheObject(cacheKey, declaredClasses);
return declaredClasses;
});
}
/**
* Returns the builder class.
* @param targetClass the class where the builder should be searched
* @return the Builder class if available.
*/
@SuppressWarnings("unchecked")
public Optional<Class<?>> getBuilderClass(final Class<?> targetClass) {
String cacheKey = "BuilderClass-" + targetClass.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Optional.class).orElseGet(() -> {
Optional<Class> res = stream(getDeclaredClasses(targetClass))
.filter(nestedClass -> {
var hasBuildMethod = true;
try {
getBuildMethod(targetClass, nestedClass);
} catch (MissingMethodException e) {
hasBuildMethod = false;
}
return hasBuildMethod;
})
.findAny();
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Get build method inside the Builder class.
* @param parentClass the class containing the builder
* @param builderClass the builder class (see Builder Pattern)
* @return Builder build method if present
*/
public Method getBuildMethod(final Class<?> parentClass, final Class<?> builderClass) {
final String cacheKey = "BuildMethod-" + builderClass.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Method.class).orElseGet(() -> {
try {
var method = builderClass.getDeclaredMethod(BUILD_METHOD_NAME);
if (!method.getReturnType().equals(parentClass)) {
throw new MissingMethodException("Invalid " + BUILD_METHOD_NAME + " method definition. It must returns a: " + parentClass.getCanonicalName());
}
method.setAccessible(true);
CACHE_MANAGER.cacheObject(cacheKey, method);
return method;
} catch (NoSuchMethodException e) {
throw new MissingMethodException("No Builder " + BUILD_METHOD_NAME + " method defined for class: " + builderClass.getName() + ".");
}
});
}
/**
* Creates an instance of the given class invoking the given constructor.
* @param constructor the constructor to invoke.
* @param constructorArgs the constructor args.
* @param <T> the class object type.
* @return the object instance.
* @throws InstanceCreationException in case the object creation fails.
*/
@SuppressWarnings("unchecked")
public <T> T getInstance(final Constructor constructor, final Object... constructorArgs) {
try {
return (T) constructor.newInstance(constructorArgs);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new InstanceCreationException(e.getMessage(), e);
}
}
/**
* Retrieves the no args constructor.
* @param clazz the class from which gets the all arg constructor.
* @param <K> the object type
* @return the no args constructor
* @throws InvalidBeanException if no default constructor is available
*/
@SuppressWarnings("unchecked")
public <K> Supplier<K> getNoArgsConstructor(final Class<K> clazz) {
final String cacheKey = "NoArgsConstructor-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Supplier.class).orElseGet(() -> {
try {
var privateLookupIn = privateLookupIn(clazz, METHOD_HANDLES_LOOKUP);
MethodHandle mh = privateLookupIn.findConstructor(clazz, methodType(void.class));
Supplier<K> constructor = (Supplier<K>) metafactory(
privateLookupIn, "get", methodType(Supplier.class), mh.type().generic(), mh, mh.type()
).getTarget().invokeExact();
CACHE_MANAGER.cacheObject(cacheKey, constructor);
return constructor;
} catch (Throwable e) {
throw new InvalidBeanException("No default constructors available for class: " + clazz.getName());
}
});
}
/**
* Retrieves the all args constructor.
* @param clazz the class from which gets the all arg constructor.
* @param <K> the object type
* @return the all args constructor
*/
@SuppressWarnings("unchecked")
public <K> Constructor<K> getAllArgsConstructor(final Class<K> clazz) {
final String cacheKey = "AllArgsConstructor-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Constructor.class).orElseGet(() -> {
Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
final var constructor = max(asList(declaredConstructors), comparing(Constructor::getParameterCount));
constructor.setAccessible(true);
CACHE_MANAGER.cacheObject(cacheKey, constructor);
return constructor;
});
}
/**
* Gets all the constructor parameters.
* @param constructor the constructor.
* @return the constructor parameters
*/
public Parameter[] getConstructorParameters(final Constructor constructor) {
final String cacheKey = "ConstructorParams-" + constructor.getDeclaringClass().getName() + '-' + constructor.getParameterCount();
return CACHE_MANAGER.getFromCache(cacheKey, Parameter[].class).orElseGet(() -> {
final Parameter[] parameters = constructor.getParameters();
CACHE_MANAGER.cacheObject(cacheKey, parameters);
return parameters;
});
}
/**
* Checks that the class has a specific field.
* @param target the class where the field should be
* @param fieldName the field name to retrieve
* @return true if the field is available, false otherwise
*/
public boolean hasField(final Object target, final String fieldName) {
final String cacheKey = "ClassHasField-" + target.getClass().getName() + '-' + fieldName;
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class).orElseGet(() -> {
var hasField = false;
try {
target.getClass().getDeclaredField(fieldName);
hasField = true;
} catch (final NoSuchFieldException e) {
final Class<?> superclass = target.getClass().getSuperclass();
if (hasSuperclass(superclass)) {
hasField = hasField(superclass, fieldName);
}
}
CACHE_MANAGER.cacheObject(cacheKey, hasField);
return hasField;
});
}
/**
* Checks if a class has setter methods.
* @param clazz clazz the clazz containing the methods.
* @return true if has at least one setter method, false otherwise
*/
public boolean hasSetterMethods(final Class<?> clazz) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
final String cacheKey = "HasSetterMethods-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class)
.orElseGet(() -> {
final Boolean res = stream(getDeclaredMethods(clazz)).anyMatch(reflectionUtils::isSetter);
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Retrieves the declared methods for the given class.
* @param clazz class from which gets the methods
* @return the class declared methods.
*/
private Method[] getDeclaredMethods(final Class<?> clazz) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
final String cacheKey = "DeclaredMethods-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Method[].class)
.orElseGet(() -> {
final Method[] res = clazz.getDeclaredMethods();
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Checks if a class has any final field.
* @param clazz class from which gets the field
* @return true if it has private final field, false otherwise.
*/
public boolean hasFinalFields(final Class<?> clazz) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
return hasFieldsMatchingCondition(clazz, IS_FINAL_AND_NOT_STATIC_FIELD, "HasFinalNotStaticFields-");
}
/**
* Checks if a class has any public field.
* @param clazz class from which gets the field
* @return true if it has private final field, false otherwise.
*/
private boolean hasNotFinalFields(final Class<?> clazz) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
return hasFieldsMatchingCondition(clazz, IS_NOT_FINAL_AND_NOT_STATIC_FIELD, "HasNotFinalNotStaticFields-");
}
/**
* Checks if a class has any field matching the given condition.
* @param clazz class from which gets the field
* @param filterPredicate the filter to apply
* @param cacheKey the filter to apply
* @return true if it has private final field, false otherwise.
*/
private boolean hasFieldsMatchingCondition(final Class<?> clazz, final Predicate<Field> filterPredicate, final String cacheKey) {
return CACHE_MANAGER.getFromCache(cacheKey + clazz.getName(), Boolean.class).orElseGet(() -> {
boolean res = stream(getDeclaredFields(clazz))
//.parallel()
.anyMatch(filterPredicate);
if (!res && nonNull(clazz.getSuperclass()) && !clazz.getSuperclass().equals(Object.class)) {
Class<?> superclass = clazz.getSuperclass();
res = hasFieldsMatchingCondition(superclass, filterPredicate, cacheKey);
}
CACHE_MANAGER.cacheObject(cacheKey + clazz.getName(), res);
return res;
});
}
/**
* Checks if any of the class constructor's parameters are not annotated with the given class.
* @param constructor the constructor to check.
* @param annotationClass the annotation class to retrieve
* @return true if any of the parameter does not contains the annotation, false otherwise.
*/
public boolean allParameterAnnotatedWith(final Constructor constructor, final Class<? extends Annotation> annotationClass) {
final String cacheKey = "AllParameterAnnotatedWith-" + constructor.getDeclaringClass().getName() + '-' + annotationClass.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class).orElseGet(() -> {
final boolean notAllAnnotatedWith = stream(constructor.getParameters())
.allMatch(parameter -> nonNull(parameter.getAnnotation(annotationClass)));
CACHE_MANAGER.cacheObject(cacheKey, notAllAnnotatedWith);
return notAllAnnotatedWith;
});
}
/**
* Checks if the constructor's parameters names are defined.
* @param constructor the constructor to check.
* @return true if some parameters names are not defined, false otherwise.
*/
public boolean areParameterNamesAvailable(final Constructor constructor) {
final String cacheKey = "AreParameterNamesAvailable-" + constructor.getDeclaringClass().getName();
return CACHE_MANAGER.getFromCache(cacheKey, Boolean.class).orElseGet(() -> {
final boolean res = stream(getConstructorParameters(constructor))
.anyMatch(Parameter::isNamePresent);
CACHE_MANAGER.cacheObject(cacheKey, res);
return res;
});
}
/**
* Returns the class type.
* @param clazz the class to check
* @return the class type {@link ClassType}
*/
public ClassType getClassType(final Class<?> clazz) {
final String cacheKey = "ClassType-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, ClassType.class).orElseGet(() -> {
final ClassType classType;
boolean hasFinalFields = hasFinalFields(clazz);
if (!hasFinalFields) {
classType = ClassType.MUTABLE;
} else {
boolean hasNotFinalFields = hasNotFinalFields(clazz);
if (hasNotFinalFields) {
classType = ClassType.MIXED;
} else {
classType = ClassType.IMMUTABLE;
}
}
CACHE_MANAGER.cacheObject(cacheKey, classType);
return classType;
});
}
/**
* Retrieves all the setters method for the given class.
* @param clazz the class containing the methods.
* @return all the class setter methods
*/
public List<Method> getSetterMethods(final Class<?> clazz) {
return getMethods(clazz, "SetterMethods", reflectionUtils::isSetter);
}
/**
* Retrieves all the getters method for the given class.
* @param clazz the class containing the methods.
* @return all the class getter methods
*/
public List<Method> getGetterMethods(final Class<?> clazz) {
return getMethods(clazz, "GetterMethods", reflectionUtils::isGetter);
}
/**
* Retrieves all the class methods matching to the given filter.
* @param clazz the class containing the methods.
* @param cacheKeyPrefix the prefix to adopt for the cache key
* @param methodFilter the filter to apply to the retrieved methods
* @return the class methods matching the filter
*/
@SuppressWarnings("unchecked")
private List<Method> getMethods(final Class<?> clazz, final String cacheKeyPrefix, final Predicate<Method> methodFilter) {
notNull(clazz, CLAZZ_CANNOT_BE_NULL);
final String cacheKey = cacheKeyPrefix + "-" + clazz.getName();
return CACHE_MANAGER.getFromCache(cacheKey, List.class).orElseGet(() -> {
final List<Method> methods = new LinkedList<>();
if (hasSuperclass(clazz.getSuperclass())) {
methods.addAll(getMethods(clazz.getSuperclass(), cacheKeyPrefix, methodFilter));
}
methods.addAll(stream(getDeclaredMethods(clazz))
.filter(methodFilter)
.collect(toList()));
CACHE_MANAGER.cacheObject(cacheKey, methods);
return methods;
});
}
/**
* Gets the default value of a primitive type.
* @param objectType the primitive object class
* @return the default value of a primitive type
*/
public Object getDefaultTypeValue(final Class<?> objectType) {
final String cacheKey = "DefaultTypeValue-" + objectType.getName();
return CACHE_MANAGER.getFromCache(cacheKey, Object.class).orElseGet(() -> {
final Object defaultValue = isPrimitiveType(objectType) ? Defaults.defaultValue(objectType) : null;
CACHE_MANAGER.cacheObject(cacheKey, defaultValue);
return defaultValue;
});
}
/**
* Returns all the not final fields.
* @param clazz the class containing fields.
* @param skipStatic if true the static fields are skipped.
* @return a list containing all the not final fields.
*/
@SuppressWarnings("unchecked")
public List<Field> getNotFinalFields(final Class<?> clazz, final Boolean skipStatic) {
final String cacheKey = "NotFinalFields-" + clazz.getName() + "-" + skipStatic;
return CACHE_MANAGER.getFromCache(cacheKey, List.class).orElseGet(() -> {
List<Field> notFinalFields = getDeclaredFields(clazz, skipStatic)
.stream()
.filter(IS_NOT_FINAL_FIELD).collect(toList());
CACHE_MANAGER.cacheObject(cacheKey, notFinalFields);
return notFinalFields;
});
}
}