-
Notifications
You must be signed in to change notification settings - Fork 197
/
MethodUtils.java
1323 lines (1240 loc) · 51.3 KB
/
MethodUtils.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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.commons.beanutils2;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.WeakHashMap;
import org.apache.commons.lang3.SystemProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* <p>Utility reflection methods focused on methods in general rather than properties in particular.</p>
*
* <h2>Known Limitations</h2>
* <h3>Accessing Public Methods In A Default Access Superclass</h3>
* <p>There is an issue when invoking public methods contained in a default access superclass.
* Reflection locates these methods fine and correctly assigns them as public.
* However, an {@code IllegalAccessException} is thrown if the method is invoked.</p>
*
* <p>{@code MethodUtils} contains a workaround for this situation.
* It will attempt to call {@code setAccessible} on this method.
* If this call succeeds, then the method can be invoked as normal.
* This call will only succeed when the application has sufficient security privileges.
* If this call fails then a warning will be logged and the method may fail.</p>
*/
public class MethodUtils {
/**
* Represents the key to looking up a Method by reflection.
*/
private static final class MethodDescriptor {
private final Class<?> cls;
private final String methodName;
private final Class<?>[] paramTypes;
private final boolean exact;
private final int hashCode;
/**
* The sole constructor.
*
* @param cls the class to reflect, must not be null
* @param methodName the method name to obtain
* @param paramTypes the array of classes representing the parameter types
* @param exact whether the match has to be exact.
*/
public MethodDescriptor(final Class<?> cls, final String methodName, Class<?>[] paramTypes,
final boolean exact) {
Objects.requireNonNull(cls, "cls");
Objects.requireNonNull(methodName, "methodName");
if (paramTypes == null) {
paramTypes = BeanUtils.EMPTY_CLASS_ARRAY;
}
this.cls = cls;
this.methodName = methodName;
this.paramTypes = paramTypes;
this.exact= exact;
this.hashCode = methodName.length();
}
/**
* Checks for equality.
* @param obj object to be tested for equality
* @return true, if the object describes the same Method.
*/
@Override
public boolean equals(final Object obj) {
if (!(obj instanceof MethodDescriptor)) {
return false;
}
final MethodDescriptor md = (MethodDescriptor)obj;
return exact == md.exact &&
methodName.equals(md.methodName) &&
cls.equals(md.cls) &&
Arrays.equals(paramTypes, md.paramTypes);
}
/**
* Returns the string length of method name. I.e. if the
* hash codes are different, the objects are different. If the
* hash codes are the same, need to use the equals method to
* determine equality.
* @return the string length of method name.
*/
@Override
public int hashCode() {
return hashCode;
}
}
private static final Log LOG = LogFactory.getLog(MethodUtils.class);
/**
* Only log warning about accessibility work around once.
* <p>
* Note that this is broken when this class is deployed via a shared
* classloader in a container, as the warning message will be emitted
* only once, not once per webapp. However making the warning appear
* once per webapp means having a map keyed by context classloader
* which introduces nasty memory-leak problems. As this warning is
* really optional we can ignore this problem; only one of the webapps
* will get the warning in its logs but that should be good enough.
*/
private static boolean loggedAccessibleWarning;
/**
* Indicates whether methods should be cached for improved performance.
* <p>
* Note that when this class is deployed via a shared classloader in
* a container, this will affect all webapps. However making this
* configurable per webapp would mean having a map keyed by context classloader
* which may introduce memory-leak problems.
*/
private static boolean CACHE_METHODS = true;
/**
* Stores a cache of MethodDescriptor -> Method in a WeakHashMap.
* <p>
* The keys into this map only ever exist as temporary variables within
* methods of this class, and are never exposed to users of this class.
* This means that the WeakHashMap is used only as a mechanism for
* limiting the size of the cache, that is, a way to tell the garbage collector
* that the contents of the cache can be completely garbage-collected
* whenever it needs the memory. Whether this is a good approach to
* this problem is doubtful; something like the commons-collections
* LRUMap may be more appropriate (though of course selecting an
* appropriate size is an issue).
* <p>
* This static variable is safe even when this code is deployed via a
* shared classloader because it is keyed via a MethodDescriptor object
* which has a Class as one of its members and that member is used in
* the MethodDescriptor.equals method. So two components that load the same
* class via different class loaders will generate non-equal MethodDescriptor
* objects and hence end up with different entries in the map.
*/
private static final Map<MethodDescriptor, Reference<Method>> cache = Collections
.synchronizedMap(new WeakHashMap<>());
/**
* Add a method to the cache.
*
* @param md The method descriptor
* @param method The method to cache
*/
private static void cacheMethod(final MethodDescriptor md, final Method method) {
if (CACHE_METHODS && method != null) {
cache.put(md, new WeakReference<>(method));
}
}
/**
* Clear the method cache.
* @return the number of cached methods cleared
* @since 1.8.0
*/
public static synchronized int clearCache() {
final int size = cache.size();
cache.clear();
return size;
}
/**
* <p>Return an accessible method (that is, one that can be invoked via
* reflection) that implements the specified Method. If no such method
* can be found, return {@code null}.</p>
*
* @param clazz The class of the object
* @param method The method that we wish to call
* @return The accessible method
* @since 1.8.0
*/
public static Method getAccessibleMethod(Class<?> clazz, Method method) {
// Make sure we have a method to check
if (method == null) {
return null;
}
// If the requested method is not public we cannot call it
if (!Modifier.isPublic(method.getModifiers())) {
return null;
}
boolean sameClass = true;
if (clazz == null) {
clazz = method.getDeclaringClass();
} else {
if (!method.getDeclaringClass().isAssignableFrom(clazz)) {
throw new IllegalArgumentException(clazz.getName() +
" is not assignable from " + method.getDeclaringClass().getName());
}
sameClass = clazz.equals(method.getDeclaringClass());
}
// If the class is public, we are done
if (Modifier.isPublic(clazz.getModifiers())) {
if (!sameClass && !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
setMethodAccessible(method); // Default access superclass workaround
}
return method;
}
final String methodName = method.getName();
final Class<?>[] parameterTypes = method.getParameterTypes();
// Check the implemented interfaces and subinterfaces
method =
getAccessibleMethodFromInterfaceNest(clazz,
methodName,
parameterTypes);
// Check the superclass chain
if (method == null) {
method = getAccessibleMethodFromSuperclass(clazz,
methodName,
parameterTypes);
}
return method;
}
/**
* <p>Return an accessible method (that is, one that can be invoked via
* reflection) with given name and a single parameter. If no such method
* can be found, return {@code null}.
* Basically, a convenience wrapper that constructs a {@code Class}
* array for you.</p>
*
* @param clazz get method from this class
* @param methodName get method with this name
* @param parameterType taking this type of parameter
* @return The accessible method
*/
public static Method getAccessibleMethod(
final Class<?> clazz,
final String methodName,
final Class<?> parameterType) {
final Class<?>[] parameterTypes = {parameterType};
return getAccessibleMethod(clazz, methodName, parameterTypes);
}
/**
* <p>Return an accessible method (that is, one that can be invoked via
* reflection) with given name and parameters. If no such method
* can be found, return {@code null}.
* This is just a convenient wrapper for
* {@link #getAccessibleMethod(Method method)}.</p>
*
* @param clazz get method from this class
* @param methodName get method with this name
* @param parameterTypes with these parameters types
* @return The accessible method
*/
public static Method getAccessibleMethod(
final Class<?> clazz,
final String methodName,
final Class<?>[] parameterTypes) {
try {
final MethodDescriptor md = new MethodDescriptor(clazz, methodName, parameterTypes, true);
// Check the cache first
Method method = getCachedMethod(md);
if (method != null) {
return method;
}
method = getAccessibleMethod
(clazz, clazz.getMethod(methodName, parameterTypes));
cacheMethod(md, method);
return method;
} catch (final NoSuchMethodException e) {
return null;
}
}
/**
* <p>Return an accessible method (that is, one that can be invoked via
* reflection) that implements the specified Method. If no such method
* can be found, return {@code null}.</p>
*
* @param method The method that we wish to call
* @return The accessible method
*/
public static Method getAccessibleMethod(final Method method) {
// Make sure we have a method to check
if (method == null) {
return null;
}
return getAccessibleMethod(method.getDeclaringClass(), method);
}
/**
* <p>Return an accessible method (that is, one that can be invoked via
* reflection) that implements the specified method, by scanning through
* all implemented interfaces and subinterfaces. If no such method
* can be found, return {@code null}.</p>
*
* <p> There isn't any good reason why this method must be private.
* It is because there doesn't seem any reason why other classes should
* call this rather than the higher level methods.</p>
*
* @param clazz Parent class for the interfaces to be checked
* @param methodName Method name of the method we wish to call
* @param parameterTypes The parameter type signatures
*/
private static Method getAccessibleMethodFromInterfaceNest
(Class<?> clazz, final String methodName, final Class<?>[] parameterTypes) {
Method method = null;
// Search up the superclass chain
for (; clazz != null; clazz = clazz.getSuperclass()) {
// Check the implemented interfaces of the parent class
final Class<?>[] interfaces = clazz.getInterfaces();
for (final Class<?> anInterface : interfaces) {
// Is this interface public?
if (!Modifier.isPublic(anInterface.getModifiers())) {
continue;
}
// Does the method exist on this interface?
try {
method = anInterface.getDeclaredMethod(methodName,
parameterTypes);
} catch (final NoSuchMethodException e) {
/* Swallow, if no method is found after the loop then this
* method returns null.
*/
}
if (method != null) {
return method;
}
// Recursively check our parent interfaces
method =
getAccessibleMethodFromInterfaceNest(anInterface,
methodName,
parameterTypes);
if (method != null) {
return method;
}
}
}
// We did not find anything
return null;
}
/**
* <p>Return an accessible method (that is, one that can be invoked via
* reflection) by scanning through the superclasses. If no such method
* can be found, return {@code null}.</p>
*
* @param clazz Class to be checked
* @param methodName Method name of the method we wish to call
* @param parameterTypes The parameter type signatures
*/
private static Method getAccessibleMethodFromSuperclass
(final Class<?> clazz, final String methodName, final Class<?>[] parameterTypes) {
Class<?> parentClazz = clazz.getSuperclass();
while (parentClazz != null) {
if (Modifier.isPublic(parentClazz.getModifiers())) {
try {
return parentClazz.getMethod(methodName, parameterTypes);
} catch (final NoSuchMethodException e) {
return null;
}
}
parentClazz = parentClazz.getSuperclass();
}
return null;
}
/**
* Gets the method from the cache, if present.
*
* @param md The method descriptor
* @return The cached method
*/
private static Method getCachedMethod(final MethodDescriptor md) {
if (CACHE_METHODS) {
final Reference<Method> methodRef = cache.get(md);
if (methodRef != null) {
return methodRef.get();
}
}
return null;
}
/**
* <p>Find an accessible method that matches the given name and has compatible parameters.
* Compatible parameters mean that every method parameter is assignable from
* the given parameters.
* In other words, it finds a method with the given name
* that will take the parameters given.</p>
*
* <p>This method is slightly indeterministic since it loops
* through methods names and return the first matching method.</p>
*
* <p>This method is used by
* {@link
* #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}.
*
* <p>This method can match primitive parameter by passing in wrapper classes.
* For example, a {@code Boolean</code> will match a primitive <code>boolean}
* parameter.
*
* @param clazz find method in this class
* @param methodName find method with this name
* @param parameterTypes find method with compatible parameters
* @return The accessible method
*/
public static Method getMatchingAccessibleMethod(
final Class<?> clazz,
final String methodName,
final Class<?>[] parameterTypes) {
// trace logging
if (LOG.isTraceEnabled()) {
LOG.trace("Matching name=" + methodName + " on " + clazz);
}
final MethodDescriptor md = new MethodDescriptor(clazz, methodName, parameterTypes, false);
// see if we can find the method directly
// most of the time this works and it's much faster
try {
// Check the cache first
Method method = getCachedMethod(md);
if (method != null) {
return method;
}
method = clazz.getMethod(methodName, parameterTypes);
if (LOG.isTraceEnabled()) {
LOG.trace("Found straight match: " + method);
LOG.trace("isPublic:" + Modifier.isPublic(method.getModifiers()));
}
setMethodAccessible(method); // Default access superclass workaround
cacheMethod(md, method);
return method;
} catch (final NoSuchMethodException e) { /* SWALLOW */ }
// search through all methods
final int paramSize = parameterTypes.length;
Method bestMatch = null;
final Method[] methods = clazz.getMethods();
float bestMatchCost = Float.MAX_VALUE;
float myCost = Float.MAX_VALUE;
for (final Method method2 : methods) {
if (method2.getName().equals(methodName)) {
// log some trace information
if (LOG.isTraceEnabled()) {
LOG.trace("Found matching name:");
LOG.trace(method2);
}
// compare parameters
final Class<?>[] methodsParams = method2.getParameterTypes();
final int methodParamSize = methodsParams.length;
if (methodParamSize == paramSize) {
boolean match = true;
for (int n = 0 ; n < methodParamSize; n++) {
if (LOG.isTraceEnabled()) {
LOG.trace("Param=" + parameterTypes[n].getName());
LOG.trace("Method=" + methodsParams[n].getName());
}
if (!isAssignmentCompatible(methodsParams[n], parameterTypes[n])) {
if (LOG.isTraceEnabled()) {
LOG.trace(methodsParams[n] + " is not assignable from "
+ parameterTypes[n]);
}
match = false;
break;
}
}
if (match) {
// get accessible version of method
final Method method = getAccessibleMethod(clazz, method2);
if (method != null) {
if (LOG.isTraceEnabled()) {
LOG.trace(method + " accessible version of "
+ method2);
}
setMethodAccessible(method); // Default access superclass workaround
myCost = getTotalTransformationCost(parameterTypes, method.getParameterTypes());
if ( myCost < bestMatchCost ) {
bestMatch = method;
bestMatchCost = myCost;
}
}
LOG.trace("Couldn't find accessible method.");
}
}
}
}
if (bestMatch != null) {
cacheMethod(md, bestMatch);
} else {
// didn't find a match
LOG.trace("No match found.");
}
return bestMatch;
}
/**
* Gets the number of steps required needed to turn the source class into the
* destination class. This represents the number of steps in the object hierarchy
* graph.
* @param srcClass The source class
* @param destClass The destination class
* @return The cost of transforming an object
*/
private static float getObjectTransformationCost(Class<?> srcClass, final Class<?> destClass) {
float cost = 0.0f;
while (srcClass != null && !destClass.equals(srcClass)) {
if (destClass.isPrimitive()) {
final Class<?> destClassWrapperClazz = getPrimitiveWrapper(destClass);
if (destClassWrapperClazz != null && destClassWrapperClazz.equals(srcClass)) {
cost += 0.25f;
break;
}
}
if (destClass.isInterface() && isAssignmentCompatible(destClass, srcClass)) {
// slight penalty for interface match.
// we still want an exact match to override an interface match, but
// an interface match should override anything where we have to get a
// superclass.
cost += 0.25f;
break;
}
cost++;
srcClass = srcClass.getSuperclass();
}
/*
* If the destination class is null, we've traveled all the way up to
* an Object match. We'll penalize this by adding 1.5 to the cost.
*/
if (srcClass == null) {
cost += 1.5f;
}
return cost;
}
/**
* Gets the class for the primitive type corresponding to the primitive wrapper class given.
* For example, an instance of {@code Boolean.class</code> returns a <code>boolean.class}.
* @param wrapperType the
* @return the primitive type class corresponding to the given wrapper class,
* null if no match is found
*/
public static Class<?> getPrimitiveType(final Class<?> wrapperType) {
// does anyone know a better strategy than comparing names?
if (Boolean.class.equals(wrapperType)) {
return boolean.class;
}
if (Float.class.equals(wrapperType)) {
return float.class;
}
if (Long.class.equals(wrapperType)) {
return long.class;
}
if (Integer.class.equals(wrapperType)) {
return int.class;
}
if (Short.class.equals(wrapperType)) {
return short.class;
}
if (Byte.class.equals(wrapperType)) {
return byte.class;
}
if (Double.class.equals(wrapperType)) {
return double.class;
}
if (Character.class.equals(wrapperType)) {
return char.class;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Not a known primitive wrapper class: " + wrapperType);
}
return null;
}
/**
* Gets the wrapper object class for the given primitive type class.
* For example, passing {@code boolean.class</code> returns <code>Boolean.class}
* @param primitiveType the primitive type class for which a match is to be found
* @return the wrapper type associated with the given primitive
* or null if no match is found
*/
public static Class<?> getPrimitiveWrapper(final Class<?> primitiveType) {
// does anyone know a better strategy than comparing names?
if (boolean.class.equals(primitiveType)) {
return Boolean.class;
}
if (float.class.equals(primitiveType)) {
return Float.class;
}
if (long.class.equals(primitiveType)) {
return Long.class;
}
if (int.class.equals(primitiveType)) {
return Integer.class;
}
if (short.class.equals(primitiveType)) {
return Short.class;
}
if (byte.class.equals(primitiveType)) {
return Byte.class;
}
if (double.class.equals(primitiveType)) {
return Double.class;
}
if (char.class.equals(primitiveType)) {
return Character.class;
}
return null;
}
/**
* Returns the sum of the object transformation cost for each class in the source
* argument list.
* @param srcArgs The source arguments
* @param destArgs The destination arguments
* @return The total transformation cost
*/
private static float getTotalTransformationCost(final Class<?>[] srcArgs, final Class<?>[] destArgs) {
float totalCost = 0.0f;
for (int i = 0; i < srcArgs.length; i++) {
Class<?> srcClass, destClass;
srcClass = srcArgs[i];
destClass = destArgs[i];
totalCost += getObjectTransformationCost(srcClass, destClass);
}
return totalCost;
}
/**
* <p>Invoke a method whose parameter type matches exactly the object
* type.</p>
*
* <p> This is a convenient wrapper for
* {@link #invokeExactMethod(Object object,String methodName,Object [] args)}.
* </p>
*
* @param object invoke method on this object
* @param methodName get method with this name
* @param arg use this argument. May be null (this will result in calling the
* parameterless method with name {@code methodName}).
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
*/
public static Object invokeExactMethod(
final Object object,
final String methodName,
final Object arg)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
final Object[] args = toArray(arg);
return invokeExactMethod(object, methodName, args);
}
/**
* <p>Invoke a method whose parameter types match exactly the object
* types.</p>
*
* <p> This uses reflection to invoke the method obtained from a call to
* {@code getAccessibleMethod()}.</p>
*
* @param object invoke method on this object
* @param methodName get method with this name
* @param args use these arguments - treat null as empty array (passing null will
* result in calling the parameterless method with name {@code methodName}).
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
*/
public static Object invokeExactMethod(
final Object object,
final String methodName,
Object[] args)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
if (args == null) {
args = BeanUtils.EMPTY_OBJECT_ARRAY;
}
final int arguments = args.length;
final Class<?>[] parameterTypes = new Class[arguments];
for (int i = 0; i < arguments; i++) {
parameterTypes[i] = args[i].getClass();
}
return invokeExactMethod(object, methodName, args, parameterTypes);
}
/**
* <p>Invoke a method whose parameter types match exactly the parameter
* types given.</p>
*
* <p>This uses reflection to invoke the method obtained from a call to
* {@code getAccessibleMethod()}.</p>
*
* @param object invoke method on this object
* @param methodName get method with this name
* @param args use these arguments - treat null as empty array (passing null will
* result in calling the parameterless method with name {@code methodName}).
* @param parameterTypes match these parameters - treat null as empty array
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
*/
public static Object invokeExactMethod(
final Object object,
final String methodName,
Object[] args,
Class<?>[] parameterTypes)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
if (args == null) {
args = BeanUtils.EMPTY_OBJECT_ARRAY;
}
if (parameterTypes == null) {
parameterTypes = BeanUtils.EMPTY_CLASS_ARRAY;
}
final Method method = getAccessibleMethod(
object.getClass(),
methodName,
parameterTypes);
if (method == null) {
throw new NoSuchMethodException("No such accessible method: " +
methodName + "() on object: " + object.getClass().getName());
}
return method.invoke(object, args);
}
/**
* <p>Invoke a static method whose parameter type matches exactly the object
* type.</p>
*
* <p> This is a convenient wrapper for
* {@link #invokeExactStaticMethod(Class objectClass,String methodName,Object [] args)}.
* </p>
*
* @param objectClass invoke static method on this class
* @param methodName get method with this name
* @param arg use this argument. May be null (this will result in calling the
* parameterless method with name {@code methodName}).
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
* @since 1.8.0
*/
public static Object invokeExactStaticMethod(
final Class<?> objectClass,
final String methodName,
final Object arg)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
final Object[] args = toArray(arg);
return invokeExactStaticMethod (objectClass, methodName, args);
}
/**
* <p>Invoke a static method whose parameter types match exactly the object
* types.</p>
*
* <p> This uses reflection to invoke the method obtained from a call to
* {@link #getAccessibleMethod(Class, String, Class[])}.</p>
*
* @param objectClass invoke static method on this class
* @param methodName get method with this name
* @param args use these arguments - treat null as empty array (passing null will
* result in calling the parameterless method with name {@code methodName}).
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
* @since 1.8.0
*/
public static Object invokeExactStaticMethod(
final Class<?> objectClass,
final String methodName,
Object[] args)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
if (args == null) {
args = BeanUtils.EMPTY_OBJECT_ARRAY;
}
final int arguments = args.length;
final Class<?>[] parameterTypes = new Class[arguments];
for (int i = 0; i < arguments; i++) {
parameterTypes[i] = args[i].getClass();
}
return invokeExactStaticMethod(objectClass, methodName, args, parameterTypes);
}
/**
* <p>Invoke a static method whose parameter types match exactly the parameter
* types given.</p>
*
* <p>This uses reflection to invoke the method obtained from a call to
* {@link #getAccessibleMethod(Class, String, Class[])}.</p>
*
* @param objectClass invoke static method on this class
* @param methodName get method with this name
* @param args use these arguments - treat null as empty array (passing null will
* result in calling the parameterless method with name {@code methodName}).
* @param parameterTypes match these parameters - treat null as empty array
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
* @since 1.8.0
*/
public static Object invokeExactStaticMethod(
final Class<?> objectClass,
final String methodName,
Object[] args,
Class<?>[] parameterTypes)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
if (args == null) {
args = BeanUtils.EMPTY_OBJECT_ARRAY;
}
if (parameterTypes == null) {
parameterTypes = BeanUtils.EMPTY_CLASS_ARRAY;
}
final Method method = getAccessibleMethod(
objectClass,
methodName,
parameterTypes);
if (method == null) {
throw new NoSuchMethodException("No such accessible method: " +
methodName + "() on class: " + objectClass.getName());
}
return method.invoke(null, args);
}
/**
* <p>Invoke a named method whose parameter type matches the object type.</p>
*
* <p>The behavior of this method is less deterministic
* than {@code invokeExactMethod()}.
* It loops through all methods with names that match
* and then executes the first it finds with compatible parameters.</p>
*
* <p>This method supports calls to methods taking primitive parameters
* via passing in wrapping classes. So, for example, a {@code Boolean} class
* would match a {@code boolean} primitive.</p>
*
* <p> This is a convenient wrapper for
* {@link #invokeMethod(Object object,String methodName,Object [] args)}.
* </p>
*
* @param object invoke method on this object
* @param methodName get method with this name
* @param arg use this argument. May be null (this will result in calling the
* parameterless method with name {@code methodName}).
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
*/
public static Object invokeMethod(
final Object object,
final String methodName,
final Object arg)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
final Object[] args = toArray(arg);
return invokeMethod(object, methodName, args);
}
/**
* <p>Invoke a named method whose parameter type matches the object type.</p>
*
* <p>The behavior of this method is less deterministic
* than {@link #invokeExactMethod(Object object,String methodName,Object [] args)}.
* It loops through all methods with names that match
* and then executes the first it finds with compatible parameters.</p>
*
* <p>This method supports calls to methods taking primitive parameters
* via passing in wrapping classes. So, for example, a {@code Boolean} class
* would match a {@code boolean} primitive.</p>
*
* <p> This is a convenient wrapper for
* {@link #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}.
* </p>
*
* @param object invoke method on this object
* @param methodName get method with this name
* @param args use these arguments - treat null as empty array (passing null will
* result in calling the parameterless method with name {@code methodName}).
* @return The value returned by the invoked method
*
* @throws NoSuchMethodException if there is no such accessible method
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
*/
public static Object invokeMethod(
final Object object,
final String methodName,
Object[] args)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
if (args == null) {
args = BeanUtils.EMPTY_OBJECT_ARRAY;
}
final int arguments = args.length;
final Class<?>[] parameterTypes = new Class[arguments];
for (int i = 0; i < arguments; i++) {
parameterTypes[i] = args[i].getClass();
}
return invokeMethod(object, methodName, args, parameterTypes);
}
/**
* <p>Invoke a named method whose parameter type matches the object type.</p>
*
* <p>The behavior of this method is less deterministic
* than {@link
* #invokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}.