summaryrefslogtreecommitdiff
path: root/dicore3/core/src/main/java/io/dico/dicore/Reflection.java
blob: 2452d754002bd9068d30ad1b764bc1b5670a5393 (plain)
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
package io.dico.dicore;

import io.dico.dicore.exceptions.ExceptionHandler;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * Reflective utilities
 */
@SuppressWarnings("unchecked")
public class Reflection {
    private static final ExceptionHandler exceptionHandler;
    private static final Field fieldModifiersField = restrictedSearchField(Field.class, "modifiers");
    private static Consumer<String> errorTarget;
    
    private Reflection() {
        
    }
    
    static {
        exceptionHandler = new ExceptionHandler() {
            @Override
            public void handle(Throwable ex) {
                handleGenericException(ex);
            }
            
            @Override
            public Object handleGenericException(Throwable ex, Object... args) {
                String action = args.length == 0 || !(args[0] instanceof String) ? "executing a reflective operation" : (String) args[0];
                ExceptionHandler.log(errorTarget, action, ex);
                return null;
            }
        };
        
        // don't use method reference here: the current reference in System.out would be cached.
        setErrorTarget(msg -> System.out.println(msg));
    }
    
    /**
     * Sets the output where ReflectiveOperationException's and similar are sent.
     * This defaults to {@link System#out}.
     *
     * @param target The new output
     * @throws NullPointerException if target is null
     */
    public static void setErrorTarget(Consumer<String> target) {
        errorTarget = Objects.requireNonNull(target);
    }
    
    /**
     * This search modifier tells the implementation that it should subsequently search superclasses for the field/method.
     * Using this modifier means a call to {@link #deepSearchField(Class, String)} will be used instead of {@link #restrictedSearchField(Class, String)}
     * and a call to {@link #deepSearchMethod(Class, String, Class[])} will be used instead of {@link #restrictedSearchMethod(Class, String, Class[])}
     */
    public static final int DEEP_SEARCH = 0x1;
    
    /**
     * This search modifier applies only to fields, and tells the implementation that a final modifier might be present on a found field, and that it should be removed.
     */
    public static final int REMOVE_FINAL = 0x2;
    
    /**
     * This search modifier applies only to methods, and tells the implementation that it should completely ignore parameter types and return the first method with a matching name
     * The implementation uses {@link Class#getDeclaredMethods()} instead of {@link Class#getDeclaredMethod(String, Class[])} if this modifier is set.
     */
    public static final int IGNORE_PARAMS = 0x2;

    /*
    ### FIELD METHODS ###
     */
    
    /**
     * Search a field of any accessibility within the class or any of its superclasses.
     * The first field with the given name that is found will be returned.
     * <p>
     * If a field is found and it is not accessible, this method attempts to make it accessible.
     * If a {@link SecurityException} is thrown in the process, that is ignored and the field will be returned nonetheless.
     * <p>
     * This method throws IllegalArgumentException if the Field is not found, because, in most cases, that should never happen,
     * and it should simplify debugging. In some cases, if you want to know if the field exists, you'll have to use try/catch for that.
     *
     * @param clazz     The lowest class in the ladder to start searching from
     * @param fieldName The name of the field
     *                  //@param fieldType the type of the field, or null if it can be any.
     * @return The field
     * @throws NullPointerException     if clazz is null or fieldName is null
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #restrictedSearchField(Class, String)
     */
    public static Field deepSearchField(Class<?> clazz, String fieldName/*, Class<?> fieldType*/) {
        Class<?> currentClass = clazz;
        Field result;
        do {
            // throws NPE if class or fieldName is null
            result = internalSearchField(clazz, fieldName);
            if (result != null) {
                return result;
            }
            currentClass = currentClass.getSuperclass();
        } while (currentClass != null);
        
        throw new IllegalArgumentException("field not found in " + clazz.getCanonicalName() + " and superclasses: " + fieldName);
    }
    
    /**
     * Search a field of any accessibility within the class, but not its superclasses.
     * <p>
     * If a field is found and it is not accessible, this method attempts to make it accessible.
     * If a {@link SecurityException} is thrown in the process, that is ignored and the field will be returned nonetheless.
     * <p>
     * This method throws IllegalArgumentException if the Field is not found, because, in most cases, that should never happen,
     * and it should simplify debugging. In some cases, if you want to know if the field exists, you'll have to use try/catch for that.
     *
     * @param clazz     The only class to search for the field
     * @param fieldName The name of the field
     * @return The field
     * @throws NullPointerException     if clazz or fieldName is null
     * @throws IllegalArgumentException if the field does not exist
     */
    public static Field restrictedSearchField(Class<?> clazz, String fieldName) {
        Field result = internalSearchField(clazz, fieldName);
        if (result == null) {
            throw new IllegalArgumentException("field not found in " + clazz.getCanonicalName() + ": " + fieldName);
        }
        return result;
    }
    
    /**
     * Searches for a field using the given search method.
     *
     * @param modifiers The modifiers for field search. Can have {@link #DEEP_SEARCH} and {@link #REMOVE_FINAL}
     * @param clazz     The class to search in/from
     * @param fieldName Name of the field
     * @return The field
     * @throws NullPointerException     if clazz or fieldName is null
     * @throws IllegalArgumentException if the field is not found
     */
    public static Field searchField(int modifiers, Class<?> clazz, String fieldName) {
        Field result;
        if ((modifiers & DEEP_SEARCH) != 0) {
            result = deepSearchField(clazz, fieldName);
        } else {
            result = restrictedSearchField(clazz, fieldName);
        }
        if ((modifiers & REMOVE_FINAL) != 0) {
            removeFinalModifier(result);
        }
        return result;
    }
    
    /**
     * @return The same as {@link #restrictedSearchField(Class, String)}, but returns null instead of throwing IllegalArgumentException
     * @see #restrictedSearchField(Class, String)
     */
    private static Field internalSearchField(Class<?> clazz, String fieldName) {
        Field result;
        try {
            // throws NullPointerException if either clazz or fieldName are null.
            result = clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException | SecurityException ex) {
            return null;
        }
        
        if (!result.isAccessible()) try {
            result.setAccessible(true);
        } catch (SecurityException ignored) {
            
        }
        
        return result;
    }
    
    /**
     * Attempts to remove existing final modifier of the given field
     * This method should always return true.
     *
     * @param field The field whose final modifier to remove
     * @return true if the field most definitely has no final modifier after this call
     * @throws NullPointerException if field is null
     */
    public static boolean removeFinalModifier(Field field) {
        Objects.requireNonNull(field);
        try {
            int modifiers = (int) fieldModifiersField.get(field);
            if (modifiers != (modifiers &= ~Modifier.FINAL)) {
                fieldModifiersField.set(field, modifiers);
            }
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
    
    /**
     * Gets field value of the field named fieldName and the given instance
     * To find the field, {@link #deepSearchField(Class, String)} is used (DEEP search method).
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param instance  The instance whose field value to get
     * @param fieldName the name of the field
     * @param <T>       The expected/known field type
     * @return The field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #deepSearchField(Class, String)
     * @see #getFieldValue(Class, String, Object)
     */
    public static <T> T getFieldValue(Object instance, String fieldName) {
        return getFieldValue(deepSearchField(instance.getClass(), fieldName), instance);
    }
    
    /**
     * Gets field value of the field named fieldName and the given instance
     * To find the field, {@link #restrictedSearchField(Class, String)} is used (RESTRICTED search method).
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param clazz     The class to search for the field
     * @param instance  The instance whose field value to get
     * @param fieldName the name of the field
     * @param <T>       The expected/known field type
     * @return The field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #restrictedSearchField(Class, String)
     * @see #getFieldValue(Field, Object)
     */
    public static <T> T getFieldValue(Class<?> clazz, String fieldName, Object instance) {
        return getFieldValue(restrictedSearchField(clazz, fieldName), instance);
    }
    
    /**
     * Gets field value of the field named fieldName and the given instance
     * To find the field, {@link #searchField(int, Class, String)} is used.
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param modifiers The modifiers for field search. Can have {@link #DEEP_SEARCH} and {@link #REMOVE_FINAL}
     * @param clazz     The class to search for the field
     * @param instance  The instance whose field value to get
     * @param fieldName the name of the field
     * @param <T>       The expected/known field type
     * @return The field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #searchField(int, Class, String)
     * @see #getFieldValue(Field, Object)
     */
    public static <T> T getFieldValue(int modifiers, Class<?> clazz, String fieldName, Object instance) {
        return getFieldValue(searchField(modifiers, clazz, fieldName), instance);
    }
    
    /**
     * Gets field value of the given field and the given instance
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param field    the field
     * @param instance The instance whose field value to get
     * @param <T>      The expected/known field type
     * @return The field value
     */
    public static <T> T getFieldValue(Field field, Object instance) {
        return exceptionHandler.supplySafe(() -> (T) field.get(instance));
    }
    
    /**
     * Gets static field value of the field named fieldName
     * To find the field, {@link #restrictedSearchField(Class, String)} is used (RESTRICTED search method).
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param clazz     The class to search for the field
     * @param fieldName the name of the field
     * @param <T>       The expected/known field type
     * @return The field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #restrictedSearchField(Class, String)
     * @see #getStaticFieldValue(Field)
     */
    public static <T> T getStaticFieldValue(Class<?> clazz, String fieldName) {
        return getStaticFieldValue(restrictedSearchField(clazz, fieldName));
    }
    
    /**
     * Gets static field value of the field named fieldName
     * To find the field, {@link #searchField(int, Class, String)} is used.
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param modifiers The modifiers for field search. Can have {@link #DEEP_SEARCH} and {@link #REMOVE_FINAL}
     * @param clazz     The class to search for the field
     * @param fieldName the name of the field
     * @param <T>       The expected/known field type
     * @return The field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #deepSearchField(Class, String)
     * @see #getStaticFieldValue(Field)
     */
    public static <T> T getStaticFieldValue(int modifiers, Class<?> clazz, String fieldName) {
        return getStaticFieldValue(searchField(modifiers, clazz, fieldName));
    }
    
    /**
     * Gets static field value
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     * <p>
     * Equivalent to the call {@code getFieldValue(field, (Object) null)}
     *
     * @param field the field
     * @param <T>   The expected/known field type
     * @return The field value
     * @see #getFieldValue(Field, Object)
     */
    public static <T> T getStaticFieldValue(Field field) {
        return getFieldValue(field, (Object) null);
    }
    
    /**
     * Sets field value of the field named fieldName and the given instance
     * To find the field, {@link #deepSearchField(Class, String)} is used (DEEP search method).
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param instance  The instance whose field value to set
     * @param fieldName the name of the field
     * @param newValue  the new field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #deepSearchField(Class, String)
     * @see #setFieldValue(Class, String, Object, Object)
     */
    public static void setFieldValue(Object instance, String fieldName, Object newValue) {
        setFieldValue(deepSearchField(instance.getClass(), fieldName), instance, newValue);
    }
    
    /**
     * Sets field value of the field named fieldName and the given instance
     * To find the field, {@link #restrictedSearchField(Class, String)} is used (RESTRICTED search method).
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param clazz     The class to search for the field
     * @param fieldName the name of the field
     * @param instance  The field owner
     * @param newValue  The new field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #restrictedSearchField(Class, String)
     * @see #setFieldValue(Field, Object, Object)
     */
    public static void setFieldValue(Class<?> clazz, String fieldName, Object instance, Object newValue) {
        setFieldValue(restrictedSearchField(clazz, fieldName), instance, newValue);
    }
    
    /**
     * Sets field value of the field named fieldName and the given instance
     * To find the field, {@link #searchField(int, Class, String)} is used.
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param modifiers The modifiers for field search. Can have {@link #DEEP_SEARCH} and {@link #REMOVE_FINAL}
     * @param clazz     The class to search for the field
     * @param instance  The instance whose field value to set
     * @param fieldName the name of the field
     * @param newValue  The new field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #searchField(int, Class, String)
     * @see #setFieldValue(Field, Object, Object)
     */
    public static void setFieldValue(int modifiers, Class<?> clazz, String fieldName, Object instance, Object newValue) {
        setFieldValue(searchField(modifiers, clazz, fieldName), instance, newValue);
    }
    
    /**
     * Sets a field value
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param field    The field
     * @param instance The field owner
     * @param newValue The new field value
     */
    public static void setFieldValue(Field field, Object instance, Object newValue) {
        exceptionHandler.runSafe(() -> field.set(instance, newValue));
    }
    
    /**
     * Sets static field value of the field name fieldName
     * To find the field, {@link #restrictedSearchField(Class, String)} is used (RESTRICTED search method).
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param clazz     The class to search for the field
     * @param fieldName the name of the field
     * @param newValue  The new field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #restrictedSearchField(Class, String)
     * @see #setStaticFieldValue(Field, Object)
     */
    public static void setStaticFieldValue(Class<?> clazz, String fieldName, Object newValue) {
        setStaticFieldValue(restrictedSearchField(clazz, fieldName), newValue);
    }
    
    /**
     * Sets static field value of the field named fieldName
     * To find the field, {@link #searchField(int, Class, String)} is used.
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param modifiers The modifiers for field search. Can have {@link #DEEP_SEARCH} and {@link #REMOVE_FINAL}
     * @param clazz     The class to search for the field
     * @param fieldName the name of the field
     * @param newValue  The new field value
     * @throws IllegalArgumentException if the field doesn't exist
     * @see #searchField(int, Class, String)
     * @see #setStaticFieldValue(Field, Object)
     */
    public static void setStaticFieldValue(int modifiers, Class<?> clazz, String fieldName, Object newValue) {
        setStaticFieldValue(searchField(modifiers, clazz, fieldName), newValue);
    }
    
    /**
     * Sets a static field value
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param field    The field
     * @param newValue The new field value
     */
    public static void setStaticFieldValue(Field field, Object newValue) {
        setFieldValue(field, (Object) null, newValue);
    }
    
    /*
    ### METHOD METHODS ###
     */
    
    /**
     * Search a method of any accessibility within the class or any of its superclasses.
     * The first method with the given name that is found will be returned.
     * <p>
     * If a method is found and it is not accessible, this method attempts to make it accessible.
     * If a {@link SecurityException} is thrown in the process, that is ignored and the method will be returned nonetheless.
     * <p>
     * This method throws IllegalArgumentException if the Method is not found, because, in most cases, that should never happen,
     * and it should simplify debugging. In some cases, if you want to know if the method exists, you'll have to use try/catch for that.
     *
     * @param clazz          The lowest class in the ladder to start searching from
     * @param methodName     The name of the method
     * @param parameterTypes the parameter types of the sought method.
     * @return The method
     * @throws NullPointerException     if clazz is null or methodName is null
     * @throws IllegalArgumentException if the method doesn't exist
     * @see #restrictedSearchMethod(Class, String, Class[])
     */
    public static Method deepSearchMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        return deepSearchMethod(0, clazz, methodName, parameterTypes);
    }
    
    /**
     * Search a method of any accessibility within the class or any of its superclasses.
     * The first method with the given name that is found will be returned.
     * <p>
     * If a method is found and it is not accessible, this method attempts to make it accessible.
     * If a {@link SecurityException} is thrown in the process, that is ignored and the method will be returned nonetheless.
     * <p>
     * This method throws IllegalArgumentException if the Method is not found, because, in most cases, that should never happen,
     * and it should simplify debugging. In some cases, if you want to know if the method exists, you'll have to use try/catch for that.
     *
     * @param modifiers      The modifiers for method search. Can have {@link #IGNORE_PARAMS}
     * @param clazz          The lowest class in the ladder to start searching from
     * @param methodName     The name of the method
     * @param parameterTypes the parameter types of the sought method.
     * @return The method
     * @throws NullPointerException     if clazz is null or methodName is null
     * @throws IllegalArgumentException if the method doesn't exist
     * @see #restrictedSearchMethod(Class, String, Class[])
     */
    public static Method deepSearchMethod(int modifiers, Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        Class<?> currentClass = clazz;
        Method result;
        do {
            // throws NPE if class or methodName is null
            result = internalSearchMethod(modifiers, currentClass, methodName, parameterTypes);
            if (result != null) {
                return result;
            }
            currentClass = currentClass.getSuperclass();
        } while (currentClass != null);
        
        throw new IllegalArgumentException("method not found in " + clazz.getCanonicalName() + " and superclasses: " + methodName);
    }
    
    /**
     * Search a method of any accessibility within the class, but not its superclasses.
     * <p>
     * If a method is found and it is not accessible, this method attempts to make it accessible.
     * If a {@link SecurityException} is thrown in the process, that is ignored and the method will be returned nonetheless.
     * <p>
     * This method throws IllegalArgumentException if the Method is not found, because, in most cases, that should never happen,
     * and it should simplify debugging. In some cases, if you want to know if the method exists, you'll have to use try/catch for that.
     *
     * @param clazz          The only class to search for the method
     * @param methodName     The name of the method
     * @param parameterTypes the parameter types of the sought method.
     * @return The method
     * @throws NullPointerException     if clazz or methodName is null
     * @throws IllegalArgumentException if the method does not exist
     */
    public static Method restrictedSearchMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        return restrictedSearchMethod(0, clazz, methodName, parameterTypes);
    }
    
    /**
     * Search a method of any accessibility within the class, but not its superclasses.
     * <p>
     * If a method is found and it is not accessible, this method attempts to make it accessible.
     * If a {@link SecurityException} is thrown in the process, that is ignored and the method will be returned nonetheless.
     * <p>
     * This method throws IllegalArgumentException if the Method is not found, because, in most cases, that should never happen,
     * and it should simplify debugging. In some cases, if you want to know if the method exists, you'll have to use try/catch for that.
     *
     * @param modifiers      The modifiers for method search. Can have {@link #IGNORE_PARAMS}
     * @param clazz          The only class to search for the method
     * @param methodName     The name of the method
     * @param parameterTypes the parameter types of the sought method.
     * @return The method
     * @throws NullPointerException     if clazz or methodName is null
     * @throws IllegalArgumentException if the method does not exist
     */
    public static Method restrictedSearchMethod(int modifiers, Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        Method result = internalSearchMethod(modifiers, clazz, methodName, parameterTypes);
        if (result == null) {
            throw new IllegalArgumentException("method not found in " + clazz.getCanonicalName() + ": " + methodName);
        }
        return result;
    }
    
    /**
     * Searches for a method using the given search method.
     * <p>
     * If a method is found and it is not accessible, this method attempts to make it accessible.
     * If a {@link SecurityException} is thrown in the process, that is ignored and the method will be returned nonetheless.
     * <p>
     * This method throws IllegalArgumentException if the Method is not found, because, in most cases, that should never happen,
     * and it should simplify debugging. In some cases, if you want to know if the method exists, you'll have to use try/catch for that.
     *
     * @param modifiers      The modifiers for method search. Can have {@link #DEEP_SEARCH} and {@link #IGNORE_PARAMS}
     * @param clazz          The class to search in/from
     * @param methodName     Name of the method
     * @param parameterTypes the parameter types of the sought method.
     * @return The method
     * @throws NullPointerException     if clazz or methodName is null
     * @throws IllegalArgumentException if the method is not found
     */
    public static Method searchMethod(int modifiers, Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        if ((modifiers & DEEP_SEARCH) != 0) {
            return deepSearchMethod(modifiers, clazz, methodName, parameterTypes);
        } else {
            return restrictedSearchMethod(modifiers, clazz, methodName, parameterTypes);
        }
    }
    
    /**
     * @return The same as {@link #restrictedSearchMethod(Class, String, Class[]) }, but returns null instead of throwing IllegalArgumentException
     * @see #restrictedSearchMethod(Class, String, Class[])
     */
    private static Method internalSearchMethod(int modifiers, Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        Method result = null;
        
        if ((modifiers & IGNORE_PARAMS) != 0) {
            
            // throws NullPointerException if either clazz or methodName are null.
            methodName = methodName.intern();
            for (Method method : clazz.getDeclaredMethods()) {
                // all method names are interned. Identity comparison is much faster.
                if (method.getName() == methodName) {
                    result = method;
                    break;
                }
            }
            
            if (result == null) {
                return null;
            }
            
        } else {
            
            try {
                // throws NullPointerException if either clazz or methodName are null.
                result = clazz.getDeclaredMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException | SecurityException ex) {
                return null;
            }
            
        }
        
        if (!result.isAccessible()) try {
            result.setAccessible(true);
        } catch (SecurityException ignored) {
            
        }
        
        return result;
    }
    
    /**
     * Invokes the method named methodName with the given instance and arguments
     * To find the method, {@link #searchMethod(int, Class, String, Class[])} is used with no type parameters,
     * modifiers {@link #DEEP_SEARCH} and {@link #IGNORE_PARAMS}, and the class {@link Object#getClass() instance.getClass()}
     * <p>
     * To search the method with type parameters, you should search the method using {@link #searchMethod(int, Class, String, Class[])} or similar,
     * and call {@link #invokeMethod(Method, Object, Object...)}
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param methodName Name of the method
     * @param instance   The instance to invoke the method on
     * @param args       The arguments to use in the method call
     * @param <T>        The expected/known method return type
     * @return The result of calling the method
     * @throws NullPointerException     if instance or methodName is null
     * @throws IllegalArgumentException if the method is not found
     * @see #invokeMethod(Method, Object, Object...)
     */
    public static <T> T invokeMethod(Object instance, String methodName, Object... args) {
        return invokeMethod(searchMethod(DEEP_SEARCH | IGNORE_PARAMS, instance.getClass(), methodName), instance, args);
    }
    
    /**
     * Invokes the method named methodName with the given instance and arguments
     * To find the method, {@link #searchMethod(int, Class, String, Class[])} is used with no type parameters,
     * as well as the modifier {@link #IGNORE_PARAMS}
     * <p>
     * To search the method with type parameters, you should search the method using {@link #searchMethod(int, Class, String, Class[])} or similar,
     * and call {@link #invokeMethod(Method, Object, Object...)}
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param clazz      The class to search in/from
     * @param methodName Name of the method
     * @param instance   The instance to invoke the method on
     * @param args       The arguments to use in the method call
     * @param <T>        The expected/known method return type
     * @return The result of calling the method
     * @throws NullPointerException     if clazz or methodName is null
     * @throws IllegalArgumentException if the method is not found
     * @see #invokeMethod(Method, Object, Object...)
     */
    public static <T> T invokeMethod(Class<?> clazz, String methodName, Object instance, Object... args) {
        return invokeMethod(searchMethod(IGNORE_PARAMS, clazz, methodName), instance, args);
    }
    
    /**
     * Invokes the method named methodName with the given instance and arguments
     * To find the method, {@link #searchMethod(int, Class, String, Class[])} is used with no type parameters.
     * For this search, the result of calling {@link Object#getClass() instance.getClass()} is used.
     * <p>
     * To search the method with type parameters, you should search the method using {@link #searchMethod(int, Class, String, Class[])} or similar,
     * and call {@link #invokeMethod(Method, Object, Object...)}
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param modifiers  The modifiers for method search. Can have {@link #DEEP_SEARCH} and {@link #IGNORE_PARAMS}
     * @param methodName Name of the method
     * @param instance   The instance to invoke the method on
     * @param args       The arguments to use in the method call
     * @param <T>        The expected/known method return type
     * @return The result of calling the method
     * @throws NullPointerException     if instance or methodName is null
     * @throws IllegalArgumentException if the method is not found
     * @see #invokeMethod(Method, Object, Object...)
     */
    public static <T> T invokeMethod(int modifiers, Object instance, String methodName, Object... args) {
        return invokeMethod(searchMethod(modifiers, instance.getClass(), methodName), instance, args);
    }
    
    /**
     * Invokes the method named methodName with the given instance and arguments
     * To find the method, {@link #searchMethod(int, Class, String, Class[])} is used with no type parameters.
     * <p>
     * To search the method with type parameters, you should search the method using {@link #searchMethod(int, Class, String, Class[])} or similar,
     * and call {@link #invokeMethod(Method, Object, Object...)}
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param modifiers  The modifiers for method search. Can have {@link #DEEP_SEARCH} and {@link #IGNORE_PARAMS}
     * @param clazz      The class to search in/from
     * @param methodName Name of the method
     * @param instance   The instance to invoke the method on
     * @param args       The arguments to use in the method call
     * @param <T>        The expected/known method return type
     * @return The result of calling the method
     * @throws NullPointerException     if clazz or methodName is null
     * @throws IllegalArgumentException if the method is not found
     * @see #invokeMethod(Method, Object, Object...)
     */
    public static <T> T invokeMethod(int modifiers, Class<?> clazz, String methodName, Object instance, Object... args) {
        return invokeMethod(searchMethod(modifiers, clazz, methodName), instance, args);
    }
    
    /**
     * Invokes the method with the given instance and arguments
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param method   The method to invoke
     * @param instance The instance to invoke the method on
     * @param args     The arguments to use in the method call
     * @param <T>      The expected/known method return type
     * @return The result of calling the method
     */
    public static <T> T invokeMethod(Method method, Object instance, Object... args) {
        return exceptionHandler.supplySafe(() -> (T) method.invoke(instance, args));
    }
    
    /**
     * Invokes the static method named methodName with the given arguments
     * To find the method, {@link #searchMethod(int, Class, String, Class[])} is used with no type parameters,
     * as well as the modifier {@link #IGNORE_PARAMS}
     * <p>
     * To search the method with type parameters, you should search the method using {@link #searchMethod(int, Class, String, Class[])} or similar,
     * and call {@link #invokeMethod(Method, Object, Object...)}
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param clazz      The class to search in/from
     * @param methodName Name of the method
     * @param args       The arguments to use in the method call
     * @param <T>        The expected/known method return type
     * @return The result of calling the method
     * @throws NullPointerException     if clazz or methodName is null
     * @throws IllegalArgumentException if the method is not found
     * @see #invokeStaticMethod(Method, Object...)
     */
    public static <T> T invokeStaticMethod(Class<?> clazz, String methodName, Object... args) {
        return invokeStaticMethod(searchMethod(IGNORE_PARAMS, clazz, methodName), args);
    }
    
    /**
     * Invokes the static method named methodName with the given arguments
     * To find the method, {@link #searchMethod(int, Class, String, Class[])} is used with no type parameters.
     * <p>
     * To search the method with type parameters, you should search the method using {@link #searchMethod(int, Class, String, Class[])} or similar,
     * and call {@link #invokeMethod(Method, Object, Object...)}
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param modifiers  The modifiers for method search. Can have {@link #DEEP_SEARCH} and {@link #IGNORE_PARAMS}
     * @param clazz      The class to search in/from
     * @param methodName Name of the method
     * @param args       The arguments to use in the method call
     * @param <T>        The expected/known method return type
     * @return The result of calling the method
     * @throws NullPointerException     if clazz or methodName is null
     * @throws IllegalArgumentException if the method is not found
     * @see #invokeStaticMethod(Method, Object...)
     */
    public static <T> T invokeStaticMethod(int modifiers, Class<?> clazz, String methodName, Object... args) {
        return invokeStaticMethod(searchMethod(modifiers, clazz, methodName), args);
    }
    
    /**
     * Invokes the static method with the given arguments
     * <p>
     * If a {@link ReflectiveOperationException} occurs, this is printed to {@link System#out}
     *
     * @param method The method to invoke
     * @param args   The arguments to use in the method call
     * @param <T>    The expected/known method return type
     * @return The result of calling the method
     * @see #invokeMethod(Method, Object, Object...)
     */
    public static <T> T invokeStaticMethod(Method method, Object... args) {
        return invokeMethod(method, (Object) null, args);
    }
    
}