1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.reflect;
18
19 import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNotSame;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertSame;
27 import static org.junit.jupiter.api.Assertions.assertThrows;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import static org.junit.jupiter.api.Assumptions.assumeTrue;
30
31 import java.awt.Color;
32 import java.lang.reflect.Method;
33 import java.lang.reflect.Modifier;
34 import java.lang.reflect.Type;
35 import java.nio.file.Files;
36 import java.nio.file.LinkOption;
37 import java.nio.file.Path;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.Date;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Map;
46
47 import org.apache.commons.lang3.AbstractLangTest;
48 import org.apache.commons.lang3.ArrayUtils;
49 import org.apache.commons.lang3.ClassUtils;
50 import org.apache.commons.lang3.ClassUtils.Interfaces;
51 import org.apache.commons.lang3.JavaVersion;
52 import org.apache.commons.lang3.SystemUtils;
53 import org.apache.commons.lang3.math.NumberUtils;
54 import org.apache.commons.lang3.mutable.Mutable;
55 import org.apache.commons.lang3.mutable.MutableObject;
56 import org.apache.commons.lang3.reflect.testbed.Annotated;
57 import org.apache.commons.lang3.reflect.testbed.GenericConsumer;
58 import org.apache.commons.lang3.reflect.testbed.GenericParent;
59 import org.apache.commons.lang3.reflect.testbed.PublicChild;
60 import org.apache.commons.lang3.reflect.testbed.PublicSubBeanOtherPackage;
61 import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild;
62 import org.apache.commons.lang3.tuple.ImmutablePair;
63 import org.junit.jupiter.api.BeforeEach;
64 import org.junit.jupiter.api.Test;
65 import org.junit.jupiter.params.ParameterizedTest;
66 import org.junit.jupiter.params.provider.ValueSource;
67
68
69
70
71 class MethodUtilsTest extends AbstractLangTest {
72
73 protected abstract static class AbstractGetMatchingMethod implements InterfaceGetMatchingMethod {
74 public abstract void testMethod5(Exception exception);
75 }
76
77 protected abstract static class AbstractGetMatchingMethod2 implements InterfaceGetMatchingMethod {
78 @Override
79 public void testMethod6() { }
80 }
81
82 public static class ChildObject extends ParentObject implements PackagePrivateEmptyInterface {
83 }
84
85 private static final class ConcreteGetMatchingMethod2 extends AbstractGetMatchingMethod2 { }
86
87 private static final class ConcreteGetMatchingMethod22 extends AbstractGetMatchingMethod2 {
88 @Override
89 public void testMethod6() { }
90 }
91
92 private static final class GetMatchingMethodClass {
93
94 public void testMethod() {
95 }
96
97 public void testMethod(final long aLong) {
98 }
99
100 public void testMethod(final Long aLong) {
101 }
102
103 public void testMethod2(final Color aColor) {
104 }
105
106 public void testMethod2(final long aLong) {
107 }
108
109 public void testMethod2(final Long aLong) {
110 }
111
112 public void testMethod3(final long aLong, final Long anotherLong) {
113 }
114
115 public void testMethod3(final Long aLong, final long anotherLong) {
116 }
117
118 public void testMethod3(final Long aLong, final Long anotherLong) {
119 }
120
121 public void testMethod4(final Color aColor1, final Color aColor2) {
122 }
123
124 public void testMethod4(final Long aLong, final Long anotherLong) {
125 }
126
127 public void testMethod5(final Integer anInteger) {
128 }
129
130 public void testMethod5(final Number aNumber) {
131 }
132 }
133
134 private static final class GetMatchingMethodImpl extends AbstractGetMatchingMethod {
135 @Override
136 public void testMethod5(final Exception exception) {
137 }
138 }
139
140 public static class GrandParentObject {
141 }
142 public static class InheritanceBean {
143 public void testOne(final GrandParentObject obj) {
144 }
145
146 public void testOne(final Object obj) {
147 }
148
149 public void testOne(final ParentObject obj) {
150 }
151
152 public void testTwo(final GrandParentObject obj) {
153 }
154
155 public void testTwo(final Object obj) {
156 }
157
158 public void testTwo(final PackagePrivateEmptyInterface obj) {
159 }
160 }
161
162 static class InstanceLabel implements StaticLabel {
163 public String label() {
164 return "instance";
165 }
166 }
167 interface InterfaceGetMatchingMethod {
168 default void testMethod6() {
169 }
170 }
171
172 private static final class MethodDescriptor {
173 final Class<?> declaringClass;
174 final String name;
175 final Type[] parameterTypes;
176
177 MethodDescriptor(final Class<?> declaringClass, final String name, final Type... parameterTypes) {
178 this.declaringClass = declaringClass;
179 this.name = name;
180 this.parameterTypes = parameterTypes;
181 }
182 }
183
184 interface PackagePrivateEmptyInterface {
185
186 }
187
188 public static class ParentObject extends GrandParentObject {
189
190 }
191
192 private interface PrivateEmptyInterface {
193
194 }
195
196 public static class PublicImpl1OfPackagePrivateEmptyInterface implements PackagePrivateEmptyInterface {
197
198 }
199
200 public static class PublicImpl2OfPackagePrivateEmptyInterface implements PackagePrivateEmptyInterface {
201
202 }
203
204 static class StaticChild extends StaticParent {
205 public static String who() {
206 return "child";
207 }
208 }
209
210 public interface StaticLabel {
211 static String label() {
212 return "interface";
213 }
214 }
215
216 public static class StaticParent {
217 public static String who() {
218 return "parent";
219 }
220 }
221
222 public static class TestBean {
223
224 public static String bar() {
225 return "bar()";
226 }
227
228 public static String bar(final double d) {
229 return "bar(double)";
230 }
231
232 public static String bar(final int i) {
233 return "bar(int)";
234 }
235
236 public static String bar(final Integer i) {
237 return "bar(Integer)";
238 }
239
240 public static String bar(final Integer i, final String... s) {
241 return "bar(Integer, String...)";
242 }
243
244 public static String bar(final long... s) {
245 return "bar(long...)";
246 }
247
248 public static String bar(final Object o) {
249 return "bar(Object)";
250 }
251
252 public static String bar(final String s) {
253 return "bar(String)";
254 }
255
256 public static String bar(final String... s) {
257 return "bar(String...)";
258 }
259
260
261
262 public static String numOverload(final Byte... args) {
263 return "Byte...";
264 }
265
266 public static String numOverload(final Double... args) {
267 return "Double...";
268 }
269
270 public static String numOverload(final Float... args) {
271 return "Float...";
272 }
273
274 public static String numOverload(final Integer... args) {
275 return "Integer...";
276 }
277
278 public static String numOverload(final Long... args) {
279 return "Long...";
280 }
281
282 public static String numOverload(final Number... args) {
283 return "Number...";
284 }
285
286 public static String numOverload(final Short... args) {
287 return "Short...";
288 }
289
290 public static void oneParameterStatic(final String s) {
291
292 }
293
294 public static String staticInt(final int intArg) {
295 return "static int";
296 }
297
298 public static String staticIntIntVarArg(final int intArg, final int... args) {
299 return "static int, int...";
300 }
301
302 public static String staticIntLongVarArg(final int intArg, final long... args) {
303 return "static int, long...";
304 }
305
306 public static String staticIntStringVarArg(final int intArg, final String... args) {
307 return "static int, String...";
308 }
309
310 public static String staticPackagePrivateEmptyInterface(final PackagePrivateEmptyInterface... args) {
311 return "static PackagePrivateEmptyInterface...";
312 }
313
314 public static String varOverload(final Boolean... args) {
315 return "Boolean...";
316 }
317
318
319
320 public static String varOverload(final Byte... args) {
321 return "Byte...";
322 }
323
324 public static String varOverload(final Character... args) {
325 return "Character...";
326 }
327
328 public static String varOverload(final Double... args) {
329 return "Double...";
330 }
331
332 public static String varOverload(final Float... args) {
333 return "Float...";
334 }
335
336 public static String varOverload(final Integer... args) {
337 return "Integer...";
338 }
339
340 public static String varOverload(final Long... args) {
341 return "Long...";
342 }
343
344 public static String varOverload(final Number... args) {
345 return "Number...";
346 }
347
348 public static String varOverload(final Object... args) {
349 return "Object...";
350 }
351
352 public static String varOverload(final Short... args) {
353 return "Short...";
354 }
355
356 public static String varOverload(final String... args) {
357 return "String...";
358 }
359
360 public static ImmutablePair<String, Object[]> varOverloadEchoStatic(final Number... args) {
361 return new ImmutablePair<>("Number...", args);
362 }
363
364 public static ImmutablePair<String, Object[]> varOverloadEchoStatic(final String... args) {
365 return new ImmutablePair<>("String...", args);
366 }
367
368 static void verify(final ImmutablePair<String, Object[]> a, final ImmutablePair<String, Object[]> b) {
369 assertEquals(a.getLeft(), b.getLeft());
370 assertArrayEquals(a.getRight(), b.getRight());
371 }
372
373 static void verify(final ImmutablePair<String, Object[]> a, final Object obj) {
374 @SuppressWarnings("unchecked")
375 final ImmutablePair<String, Object[]> pair = (ImmutablePair<String, Object[]>) obj;
376 verify(a, pair);
377 }
378
379 boolean unboxBooleanArray;
380
381 boolean unboxByteArray;
382
383 boolean unboxCharArray;
384
385 boolean unboxDoubleArray;
386
387 boolean unboxFloatArray;
388
389 boolean unboxIntArray;
390
391 boolean unboxLongArray;
392
393 boolean unboxShortArray;
394
395 public String foo() {
396 return "foo()";
397 }
398
399 public String foo(final double d) {
400 return "foo(double)";
401 }
402
403 public String foo(final int i) {
404 return "foo(int)";
405 }
406
407 public String foo(final Integer i) {
408 return "foo(Integer)";
409 }
410
411 public String foo(final Integer i, final String... s) {
412 return "foo(int, String...)";
413 }
414
415 public String foo(final long l) {
416 return "foo(long)";
417 }
418
419 public String foo(final long... l) {
420 return "foo(long...)";
421 }
422
423 public String foo(final Object o) {
424 return "foo(Object)";
425 }
426
427 public String foo(final Object... s) {
428 return "foo(Object...)";
429 }
430
431 public String foo(final String s) {
432 return "foo(String)";
433 }
434
435 public String foo(final String... s) {
436 return "foo(String...)";
437 }
438
439 public String intIntVarArg(final int intArg, final int... args) {
440 return "int, int...";
441 }
442
443 public String intLongVarArg(final int intArg, final long... args) {
444 return "int, long...";
445 }
446
447 public int intStringStringVarArg(final int first, final String second, final String... rest) {
448 return rest.length;
449 }
450
451 public String intStringVarArg(final int intArg, final String... args) {
452 return "int, String...";
453 }
454
455 public void oneParameter(final String s) {
456
457 }
458
459 public String packagePrivateEmptyInterface(final PackagePrivateEmptyInterface... args) {
460 return "PackagePrivateEmptyInterface...";
461 }
462
463 @SuppressWarnings("unused")
464 private String privateStringStuff() {
465 return "privateStringStuff()";
466 }
467
468 @SuppressWarnings("unused")
469 private String privateStringStuff(final double d) {
470 return "privateStringStuff(double)";
471 }
472
473 @SuppressWarnings("unused")
474 private String privateStringStuff(final int i) {
475 return "privateStringStuff(int)";
476 }
477
478 @SuppressWarnings("unused")
479 private String privateStringStuff(final Integer i) {
480 return "privateStringStuff(Integer)";
481 }
482
483 @SuppressWarnings("unused")
484 private String privateStringStuff(final Object s) {
485 return "privateStringStuff(Object)";
486 }
487
488 @SuppressWarnings("unused")
489 private String privateStringStuff(final String s) {
490 return "privateStringStuff(String)";
491 }
492
493 @SuppressWarnings("unused")
494 private void privateStuff() {
495 }
496
497 public boolean[] unboxing(final boolean... values) {
498 unboxBooleanArray = true;
499 return values;
500 }
501
502 public byte[] unboxing(final byte... values) {
503 unboxByteArray = true;
504 return values;
505 }
506
507 public char[] unboxing(final char... values) {
508 unboxCharArray = true;
509 return values;
510 }
511
512 public double[] unboxing(final double... values) {
513 unboxDoubleArray = true;
514 return values;
515 }
516
517 public float[] unboxing(final float... values) {
518 unboxFloatArray = true;
519 return values;
520 }
521
522 public int[] unboxing(final int... values) {
523 unboxIntArray = true;
524 return values;
525 }
526
527 public long[] unboxing(final long... values) {
528 unboxLongArray = true;
529 return values;
530 }
531
532 public short[] unboxing(final short... values) {
533 unboxShortArray = true;
534 return values;
535 }
536
537 public ImmutablePair<String, Object[]> varOverloadEcho(final Number... args) {
538 return new ImmutablePair<>("Number...", args);
539 }
540
541
542
543
544 public ImmutablePair<String, Object[]> varOverloadEcho(final String... args) {
545 return new ImmutablePair<>("String...", args);
546 }
547
548 }
549
550 static class TestBeanSubclass extends TestBean {
551 }
552
553 static class TestBeanWithInterfaces implements PrivateEmptyInterface {
554 public String foo() {
555 return "foo()";
556 }
557 }
558
559 private static class TestMutable implements Mutable<Object> {
560 @Override
561 public Object getValue() {
562 return null;
563 }
564
565 @Override
566 public void setValue(final Object value) {
567 }
568 }
569
570 private static final class TestMutableSubclass extends TestMutable {
571
572 }
573
574 private final Map<Class<?>, Class<?>[]> classCache = new HashMap<>();
575
576 private TestBean testBean;
577
578 private void expectMatchingAccessibleMethodParameterTypes(final Class<?> cls, final String methodName, final Class<?>[] requestTypes,
579 final Class<?>[] actualTypes) {
580 final Method m = MethodUtils.getMatchingAccessibleMethod(cls, methodName, requestTypes);
581 assertNotNull(m, "could not find any matches for " + methodName + " (" + (requestTypes == null ? null : toString(requestTypes)) + ")");
582 assertArrayEquals(actualTypes, m.getParameterTypes(), toString(m.getParameterTypes()) + " not equals " + toString(actualTypes));
583 }
584
585 @BeforeEach
586 public void setUp() {
587 testBean = new TestBean();
588 classCache.clear();
589 }
590
591 private Class<?>[] singletonArray(final Class<?> c) {
592 Class<?>[] result = classCache.get(c);
593 if (result == null) {
594 result = new Class[]{c};
595 classCache.put(c, result);
596 }
597 return result;
598 }
599
600 @Test
601 void testConstructor() throws Exception {
602 assertNotNull(MethodUtils.class.getConstructor().newInstance());
603 }
604
605 @Test
606 void testDistance() throws Exception {
607 final Method distanceMethod = MethodUtils.getMatchingMethod(MethodUtils.class, "distance", Class[].class, Class[].class);
608 distanceMethod.setAccessible(true);
609 assertEquals(-1, distanceMethod.invoke(null, new Class[] { String.class }, new Class[] { Date.class }));
610 assertEquals(0, distanceMethod.invoke(null, new Class[] { Date.class }, new Class[] { Date.class }));
611 assertEquals(1, distanceMethod.invoke(null, new Class[] { Integer.class }, new Class[] { ClassUtils.wrapperToPrimitive(Integer.class) }));
612 assertEquals(2, distanceMethod.invoke(null, new Class[] { Integer.class }, new Class[] { Object.class }));
613 distanceMethod.setAccessible(false);
614 }
615
616 @ParameterizedTest
617 @ValueSource(classes = {TestMutable.class, TestMutableSubclass.class})
618 void testGetAccessibleInterfaceMethod(final Class<?> clazz) throws Exception {
619 final Class<?>[][] p = {ArrayUtils.EMPTY_CLASS_ARRAY, null};
620 for (final Class<?>[] element : p) {
621 final Method method = clazz.getMethod("getValue", element);
622 final Method accessibleMethod = MethodUtils.getAccessibleMethod(method);
623 assertNotSame(accessibleMethod, method);
624 assertSame(Mutable.class, accessibleMethod.getDeclaringClass());
625 final Method accessibleMethod2 = MethodUtils.getAccessibleMethod(clazz, method);
626 assertNotSame(accessibleMethod2, method);
627 assertSame(Mutable.class, accessibleMethod2.getDeclaringClass());
628 }
629 }
630
631 @ParameterizedTest
632 @ValueSource(classes = {TestMutable.class, TestMutableSubclass.class})
633 void testGetAccessibleInterfaceMethodFromDescription(final Class<?> clazz) {
634 final Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null };
635 for (final Class<?>[] element : p) {
636 final Method accessibleMethod = MethodUtils.getAccessibleMethod(clazz, "getValue", element);
637 assertSame(Mutable.class, accessibleMethod.getDeclaringClass());
638 }
639 }
640
641 @Test
642 void testGetAccessibleMethodInaccessible() throws Exception {
643 assertNull(MethodUtils.getAccessibleMethod(TestBean.class.getDeclaredMethod("privateStuff")));
644 assertNull(MethodUtils.getAccessibleMethod(TestBean.class, TestBean.class.getDeclaredMethod("privateStuff")));
645 assertNull(MethodUtils.getAccessibleMethod(TestBeanSubclass.class, TestBean.class.getDeclaredMethod("privateStuff")));
646 }
647
648 @Test
649 void testGetAccessibleMethodPrivateInterface() throws Exception {
650 final Method expected = TestBeanWithInterfaces.class.getMethod("foo");
651 assertNotNull(expected);
652 final Method actual = MethodUtils.getAccessibleMethod(TestBeanWithInterfaces.class, "foo");
653 assertNull(actual);
654 }
655
656 @Test
657 void testGetAccessibleMethodPublicSub() throws Exception {
658
659 final int modifiers = PackageBean.class.getModifiers();
660 assertFalse(Modifier.isPrivate(modifiers));
661 assertFalse(Modifier.isProtected(modifiers));
662 assertFalse(Modifier.isPublic(modifiers));
663
664 new PublicSubBean().setBar("");
665
666 final PublicSubBean bean = new PublicSubBean();
667 assertEquals(bean.getFoo(), "This is foo", "Start value (foo)");
668 assertEquals(bean.getBar(), "This is bar", "Start value (bar)");
669 bean.setFoo("new foo");
670 bean.setBar("new bar");
671 assertEquals(bean.getFoo(), "new foo", "Set value (foo)");
672 assertEquals(bean.getBar(), "new bar", "Set value (bar)");
673
674
675 MethodUtils.invokeExactMethod(bean, "setFoo", "alpha");
676 assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)");
677 MethodUtils.invokeExactMethod(bean, "setBar", "beta");
678 assertEquals(bean.getBar(), "beta", "Set value (bar:2)");
679
680 Method method = MethodUtils.getAccessibleMethod(PublicSubBean.class, "setFoo", String.class);
681 assertNotNull(method, "getAccessibleMethod() setFoo is Null");
682 method.invoke(bean, "1111");
683 assertEquals("1111", bean.getFoo(), "Set value (foo:3)");
684
685 method = MethodUtils.getAccessibleMethod(PublicSubBean.class, "setBar", String.class);
686 assertNotNull(method, "getAccessibleMethod() setBar is Null");
687 method.invoke(bean, "2222");
688 assertEquals("2222", bean.getBar(), "Set value (bar:3)");
689 }
690
691 @Test
692 void testGetAccessibleMethodPublicSubOtherPackage() throws Exception {
693
694 final int modifiers = Class.forName("org.apache.commons.lang3.reflect.testbed.PackageBeanOtherPackage").getModifiers();
695 assertFalse(Modifier.isPrivate(modifiers));
696 assertFalse(Modifier.isProtected(modifiers));
697 assertFalse(Modifier.isPublic(modifiers));
698
699 new PublicSubBeanOtherPackage().setBar("");
700
701 final PublicSubBeanOtherPackage bean = new PublicSubBeanOtherPackage();
702 assertEquals(bean.getFoo(), "This is foo", "Start value (foo)");
703 assertEquals(bean.getBar(), "This is bar", "Start value (bar)");
704 bean.setFoo("new foo");
705 bean.setBar("new bar");
706 assertEquals(bean.getFoo(), "new foo", "Set value (foo)");
707 assertEquals(bean.getBar(), "new bar", "Set value (bar)");
708
709
710 MethodUtils.invokeExactMethod(bean, "setFoo", "alpha");
711 assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)");
712 MethodUtils.invokeExactMethod(bean, "setBar", "beta");
713 assertEquals(bean.getBar(), "beta", "Set value (bar:2)");
714
715 Method method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setFoo", String.class);
716 assertNotNull(method, "getAccessibleMethod() setFoo is Null");
717 method.invoke(bean, "1111");
718 assertEquals("1111", bean.getFoo(), "Set value (foo:3)");
719
720 method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setBar", String.class);
721 assertNotNull(method, "getAccessibleMethod() setBar is Null");
722 method.invoke(bean, "2222");
723 assertEquals("2222", bean.getBar(), "Set value (bar:3)");
724 }
725
726 @Test
727 void testGetAccessiblePublicMethod() throws Exception {
728 assertSame(MutableObject.class,
729 MethodUtils.getAccessibleMethod(MutableObject.class.getMethod("getValue", ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass());
730 assertSame(MutableObject.class, MethodUtils
731 .getAccessibleMethod(MutableObject.class, MutableObject.class.getMethod("getValue", ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass());
732 }
733
734 @Test
735 void testGetAccessiblePublicMethodFromDescription() {
736 assertSame(MutableObject.class, MethodUtils.getAccessibleMethod(MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY).getDeclaringClass());
737 }
738
739 @Test
740 void testGetAnnotationIllegalArgumentException1() {
741 assertNullPointerException(() -> MethodUtils.getAnnotation(FieldUtilsTest.class.getDeclaredMethods()[0], null, true, true));
742 }
743
744 @Test
745 void testGetAnnotationIllegalArgumentException2() {
746 assertNullPointerException(() -> MethodUtils.getAnnotation(null, Annotated.class, true, true));
747 }
748
749 @Test
750 void testGetAnnotationIllegalArgumentException3() {
751 assertNullPointerException(() -> MethodUtils.getAnnotation(null, null, true, true));
752 }
753
754 @Test
755 void testGetAnnotationNotSearchSupersAndNotIgnoreAccess() throws NoSuchMethodException {
756 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentNotAnnotatedMethod"), Annotated.class, false, false));
757 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("doIt"), Annotated.class, false, false));
758 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentProtectedAnnotatedMethod"), Annotated.class, false, false));
759 assertNull(MethodUtils.getAnnotation(PublicChild.class.getDeclaredMethod("privateAnnotatedMethod"), Annotated.class, false, false));
760 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"), Annotated.class, false, false));
761 }
762
763 @Test
764 void testGetAnnotationNotSearchSupersButIgnoreAccess() throws NoSuchMethodException {
765 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentNotAnnotatedMethod"), Annotated.class, false, true));
766 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("doIt"), Annotated.class, false, true));
767 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentProtectedAnnotatedMethod"), Annotated.class, false, true));
768 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getDeclaredMethod("privateAnnotatedMethod"), Annotated.class, false, true));
769 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"), Annotated.class, false, true));
770 }
771
772 @Test
773 void testGetAnnotationSearchSupersAndIgnoreAccess() throws NoSuchMethodException {
774 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentNotAnnotatedMethod"), Annotated.class, true, true));
775 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("doIt"), Annotated.class, true, true));
776 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentProtectedAnnotatedMethod"), Annotated.class, true, true));
777 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getDeclaredMethod("privateAnnotatedMethod"), Annotated.class, true, true));
778 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"), Annotated.class, true, true));
779 assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentNotAnnotatedMethod", String.class), Annotated.class, true, true));
780 assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentProtectedAnnotatedMethod", String.class), Annotated.class, true,
781 true));
782 assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getDeclaredMethod("privateAnnotatedMethod", String.class), Annotated.class, true,
783 true));
784 assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("publicAnnotatedMethod", String.class), Annotated.class, true, true));
785 }
786
787 @Test
788 void testGetAnnotationSearchSupersButNotIgnoreAccess() throws NoSuchMethodException {
789 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentNotAnnotatedMethod"), Annotated.class, true, false));
790 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("doIt"), Annotated.class, true, false));
791 assertNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("parentProtectedAnnotatedMethod"), Annotated.class, true, false));
792 assertNull(MethodUtils.getAnnotation(PublicChild.class.getDeclaredMethod("privateAnnotatedMethod"), Annotated.class, true, false));
793 assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"), Annotated.class, true, false));
794 assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentNotAnnotatedMethod", String.class), Annotated.class, true, false));
795 assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentProtectedAnnotatedMethod", String.class), Annotated.class, true,
796 false));
797 assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getDeclaredMethod("privateAnnotatedMethod", String.class), Annotated.class, true,
798 false));
799 assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("publicAnnotatedMethod", String.class), Annotated.class, true, false));
800 }
801
802 @Test
803 void testGetMatchingAccessibleMethod() {
804 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.EMPTY_CLASS_ARRAY);
805 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", null, ArrayUtils.EMPTY_CLASS_ARRAY);
806 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(String.class), singletonArray(String.class));
807 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Object.class), singletonArray(Object.class));
808 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Boolean.class), singletonArray(Object.class));
809 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Byte.class), singletonArray(Integer.TYPE));
810 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Byte.TYPE), singletonArray(Integer.TYPE));
811 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Short.class), singletonArray(Integer.TYPE));
812 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Short.TYPE), singletonArray(Integer.TYPE));
813 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Character.class), singletonArray(Integer.TYPE));
814 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Character.TYPE), singletonArray(Integer.TYPE));
815 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Integer.class), singletonArray(Integer.class));
816 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Integer.TYPE), singletonArray(Integer.TYPE));
817 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Long.class), singletonArray(Long.TYPE));
818 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Long.TYPE), singletonArray(Long.TYPE));
819 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Float.class), singletonArray(Double.TYPE));
820 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Float.TYPE), singletonArray(Double.TYPE));
821 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Double.class), singletonArray(Double.TYPE));
822 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Double.TYPE), singletonArray(Double.TYPE));
823 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", singletonArray(Double.TYPE), singletonArray(Double.TYPE));
824 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", new Class[] { String.class, String.class }, new Class[] { String[].class });
825 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo", new Class[] { Integer.TYPE, String.class, String.class },
826 new Class[] { Integer.class, String[].class });
827 expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testOne", singletonArray(ParentObject.class), singletonArray(ParentObject.class));
828 expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testOne", singletonArray(ChildObject.class), singletonArray(ParentObject.class));
829 expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testTwo", singletonArray(ParentObject.class),
830 singletonArray(GrandParentObject.class));
831 expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testTwo", singletonArray(ChildObject.class), singletonArray(PackagePrivateEmptyInterface.class));
832
833 expectMatchingAccessibleMethodParameterTypes(Files.class, "exists", singletonArray(Path.class), new Class[] { Path.class, LinkOption[].class });
834 }
835
836 @ParameterizedTest
837 @ValueSource(classes = {TestMutable.class, TestMutableSubclass.class})
838 void testGetMatchingAccessibleMethodOnNonPublicClass(final Class<?> clazz) {
839 assertSame(Mutable.class, MethodUtils.getMatchingAccessibleMethod(clazz, "getValue").getDeclaringClass());
840 assertSame(Mutable.class,
841 MethodUtils.getMatchingAccessibleMethod(clazz, "setValue", Object.class).getDeclaringClass());
842 }
843
844 @Test
845 void testGetMatchingAccessibleMethodOnNonPublicJdkClass() {
846 assertSame(List.class,
847 MethodUtils.getMatchingAccessibleMethod(Collections.emptyList().getClass(), "size").getDeclaringClass());
848 assertSame(List.class,
849 MethodUtils.getMatchingAccessibleMethod(Arrays.asList(1, 2).getClass(), "size").getDeclaringClass());
850 assertSame(Map.class,
851 MethodUtils.getMatchingAccessibleMethod(Collections.emptyMap().getClass(), "size").getDeclaringClass());
852 }
853
854 @Test
855 void testGetMatchingAccessibleMethodWithNoPublicDeclaration() {
856 assertSame(TestBeanWithInterfaces.class,
857 MethodUtils.getMatchingAccessibleMethod(TestBeanWithInterfaces.class, "foo").getDeclaringClass());
858 }
859
860 @Test
861 void testGetMatchingMethod() throws NoSuchMethodException {
862 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod"), GetMatchingMethodClass.class.getMethod("testMethod"));
863 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod", Long.TYPE),
864 GetMatchingMethodClass.class.getMethod("testMethod", Long.TYPE));
865 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod", Long.class),
866 GetMatchingMethodClass.class.getMethod("testMethod", Long.class));
867 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod", (Class<?>) null),
868 GetMatchingMethodClass.class.getMethod("testMethod", Long.class));
869 assertThrows(IllegalStateException.class, () -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod2", (Class<?>) null));
870 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", Long.TYPE, Long.class),
871 GetMatchingMethodClass.class.getMethod("testMethod3", Long.TYPE, Long.class));
872 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", Long.class, Long.TYPE),
873 GetMatchingMethodClass.class.getMethod("testMethod3", Long.class, Long.TYPE));
874 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", null, Long.TYPE),
875 GetMatchingMethodClass.class.getMethod("testMethod3", Long.class, Long.TYPE));
876 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", Long.TYPE, null),
877 GetMatchingMethodClass.class.getMethod("testMethod3", Long.TYPE, Long.class));
878 assertThrows(IllegalStateException.class, () -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod4", null, null));
879
880 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod5", Integer.TYPE),
881 GetMatchingMethodClass.class.getMethod("testMethod5", Integer.class));
882 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod5", Integer.class),
883 GetMatchingMethodClass.class.getMethod("testMethod5", Integer.class));
884 assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod5", RuntimeException.class),
885 GetMatchingMethodImpl.class.getMethod("testMethod5", Exception.class));
886 assertEquals(GetMatchingMethodImpl.class.getMethod("testMethod6"), MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod6"));
887 assertNullPointerException(() -> MethodUtils.getMatchingMethod(null, "testMethod5", RuntimeException.class));
888 Method testMethod6 = MethodUtils.getMatchingMethod(ConcreteGetMatchingMethod2.class, "testMethod6");
889 assertNotNull(testMethod6);
890 assertEquals(AbstractGetMatchingMethod2.class, testMethod6.getDeclaringClass());
891 testMethod6 = MethodUtils.getMatchingMethod(ConcreteGetMatchingMethod22.class, "testMethod6");
892 assertNotNull(testMethod6);
893 assertEquals(ConcreteGetMatchingMethod22.class, testMethod6.getDeclaringClass());
894 }
895
896 @Test
897 void testGetMethodObject() throws Exception {
898 assertEquals(MutableObject.class.getMethod("getValue", ArrayUtils.EMPTY_CLASS_ARRAY),
899 MethodUtils.getMethodObject(MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY));
900 assertNull(MethodUtils.getMethodObject(MutableObject.class, "does not exist, at all", ArrayUtils.EMPTY_CLASS_ARRAY));
901 assertNull(MethodUtils.getMethodObject(null, "does not exist, at all", ArrayUtils.EMPTY_CLASS_ARRAY));
902 assertNull(MethodUtils.getMethodObject(null, null, ArrayUtils.EMPTY_CLASS_ARRAY));
903 assertNull(MethodUtils.getMethodObject(MutableObject.class, null, ArrayUtils.EMPTY_CLASS_ARRAY));
904
905 assertNull(MethodUtils.getMethodObject(MutableObject.class, "getValue", new Class[] { null }));
906
907 assertNull(MethodUtils.getMethodObject(MutableObject.class, "equals", new Class[] { null }));
908 assertNull(MethodUtils.getMethodObject(MutableObject.class, "equals", new Class[] { String.class, null, String.class }));
909 }
910
911
912
913
914 @Test
915 @Annotated
916 public void testGetMethodsListWithAnnotation() throws NoSuchMethodException {
917 assertEquals(0, MethodUtils.getMethodsListWithAnnotation(Object.class, Annotated.class).size());
918 final List<Method> methodWithAnnotation = MethodUtils.getMethodsListWithAnnotation(MethodUtilsTest.class, Annotated.class);
919 assertEquals(2, methodWithAnnotation.size());
920 assertTrue(methodWithAnnotation.contains(MethodUtilsTest.class.getMethod("testGetMethodsWithAnnotation")));
921 assertTrue(methodWithAnnotation.contains(MethodUtilsTest.class.getMethod("testGetMethodsListWithAnnotation")));
922 }
923
924 @Test
925 void testGetMethodsListWithAnnotationNullPointerException1() {
926 assertNullPointerException(() -> MethodUtils.getMethodsListWithAnnotation(FieldUtilsTest.class, null));
927 }
928
929 @Test
930 void testGetMethodsListWithAnnotationNullPointerException2() {
931 assertNullPointerException(() -> MethodUtils.getMethodsListWithAnnotation(null, Annotated.class));
932 }
933
934 @Test
935 void testGetMethodsListWithAnnotationNullPointerException3() {
936 assertNullPointerException(() -> MethodUtils.getMethodsListWithAnnotation(null, null));
937 }
938
939 @Test
940 @Annotated
941 public void testGetMethodsWithAnnotation() throws NoSuchMethodException {
942 assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class));
943 final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(MethodUtilsTest.class, Annotated.class);
944 assertEquals(2, methodsWithAnnotation.length);
945 assertTrue(ArrayUtils.contains(methodsWithAnnotation, MethodUtilsTest.class.getMethod("testGetMethodsWithAnnotation")));
946 assertTrue(ArrayUtils.contains(methodsWithAnnotation, MethodUtilsTest.class.getMethod("testGetMethodsListWithAnnotation")));
947 }
948
949 @Test
950 void testGetMethodsWithAnnotationIllegalArgumentException1() {
951 assertNullPointerException(() -> MethodUtils.getMethodsWithAnnotation(FieldUtilsTest.class, null));
952 }
953
954 @Test
955 void testGetMethodsWithAnnotationIllegalArgumentException2() {
956 assertNullPointerException(() -> MethodUtils.getMethodsWithAnnotation(null, Annotated.class));
957 }
958
959 @Test
960 void testGetMethodsWithAnnotationIllegalArgumentException3() {
961 assertNullPointerException(() -> MethodUtils.getMethodsWithAnnotation(null, null));
962 }
963
964 @Test
965 void testGetMethodsWithAnnotationNotSearchSupersAndNotIgnoreAccess() {
966 assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, false, false));
967 final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, false, false);
968 assertEquals(1, methodsWithAnnotation.length);
969 assertEquals("PublicChild.publicAnnotatedMethod",
970 methodsWithAnnotation[0].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[0].getName());
971 }
972
973 @Test
974 void testGetMethodsWithAnnotationNotSearchSupersButIgnoreAccess() {
975 assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, false, true));
976 final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, false, true);
977 assertEquals(2, methodsWithAnnotation.length);
978 assertEquals("PublicChild", methodsWithAnnotation[0].getDeclaringClass().getSimpleName());
979 assertEquals("PublicChild", methodsWithAnnotation[1].getDeclaringClass().getSimpleName());
980 assertTrue(methodsWithAnnotation[0].getName().endsWith("AnnotatedMethod"));
981 assertTrue(methodsWithAnnotation[1].getName().endsWith("AnnotatedMethod"));
982 }
983
984 @Test
985 void testGetMethodsWithAnnotationSearchSupersAndIgnoreAccess() {
986 assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, true, true));
987 final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, true, true);
988 assertEquals(4, methodsWithAnnotation.length);
989 assertEquals("PublicChild", methodsWithAnnotation[0].getDeclaringClass().getSimpleName());
990 assertEquals("PublicChild", methodsWithAnnotation[1].getDeclaringClass().getSimpleName());
991 assertTrue(methodsWithAnnotation[0].getName().endsWith("AnnotatedMethod"));
992 assertTrue(methodsWithAnnotation[1].getName().endsWith("AnnotatedMethod"));
993 assertEquals("Foo.doIt", methodsWithAnnotation[2].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[2].getName());
994 assertEquals("Parent.parentProtectedAnnotatedMethod",
995 methodsWithAnnotation[3].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[3].getName());
996 }
997
998 @Test
999 void testGetMethodsWithAnnotationSearchSupersButNotIgnoreAccess() {
1000 assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, true, false));
1001 final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, true, false);
1002 assertEquals(2, methodsWithAnnotation.length);
1003 assertEquals("PublicChild.publicAnnotatedMethod",
1004 methodsWithAnnotation[0].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[0].getName());
1005 assertEquals("Foo.doIt", methodsWithAnnotation[1].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[1].getName());
1006 }
1007
1008 @Test
1009 void testGetOverrideHierarchyExcludingInterfaces() {
1010 final Method method = MethodUtils.getAccessibleMethod(StringParameterizedChild.class, "consume", String.class);
1011 final Iterator<MethodDescriptor> expected =
1012 Arrays.asList(new MethodDescriptor(StringParameterizedChild.class, "consume", String.class),
1013 new MethodDescriptor(GenericParent.class, "consume", GenericParent.class.getTypeParameters()[0]))
1014 .iterator();
1015 for (final Method m : MethodUtils.getOverrideHierarchy(method, Interfaces.EXCLUDE)) {
1016 assertTrue(expected.hasNext());
1017 final MethodDescriptor md = expected.next();
1018 assertEquals(md.declaringClass, m.getDeclaringClass());
1019 assertEquals(md.name, m.getName());
1020 assertEquals(md.parameterTypes.length, m.getParameterTypes().length);
1021 for (int i = 0; i < md.parameterTypes.length; i++) {
1022 assertTrue(TypeUtils.equals(md.parameterTypes[i], m.getGenericParameterTypes()[i]));
1023 }
1024 }
1025 assertFalse(expected.hasNext());
1026 }
1027
1028 @Test
1029 void testGetOverrideHierarchyIncludingInterfaces() {
1030 final Method method = MethodUtils.getAccessibleMethod(StringParameterizedChild.class, "consume", String.class);
1031 final Iterator<MethodDescriptor> expected =
1032 Arrays.asList(new MethodDescriptor(StringParameterizedChild.class, "consume", String.class),
1033 new MethodDescriptor(GenericParent.class, "consume", GenericParent.class.getTypeParameters()[0]),
1034 new MethodDescriptor(GenericConsumer.class, "consume", GenericConsumer.class.getTypeParameters()[0]))
1035 .iterator();
1036 for (final Method m : MethodUtils.getOverrideHierarchy(method, Interfaces.INCLUDE)) {
1037 assertTrue(expected.hasNext());
1038 final MethodDescriptor md = expected.next();
1039 assertEquals(md.declaringClass, m.getDeclaringClass());
1040 assertEquals(md.name, m.getName());
1041 assertEquals(md.parameterTypes.length, m.getParameterTypes().length);
1042 for (int i = 0; i < md.parameterTypes.length; i++) {
1043 assertTrue(TypeUtils.equals(md.parameterTypes[i], m.getGenericParameterTypes()[i]));
1044 }
1045 }
1046 assertFalse(expected.hasNext());
1047 }
1048
1049 @Test
1050 void testInvokeExactMethod() throws Exception {
1051 assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
1052 assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo"));
1053 assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", (Object[]) null));
1054 assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", null, null));
1055 assertEquals("foo(String)", MethodUtils.invokeExactMethod(testBean, "foo", ""));
1056 assertEquals("foo(Object)", MethodUtils.invokeExactMethod(testBean, "foo", new Object()));
1057 assertEquals("foo(Integer)", MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.INTEGER_ONE));
1058 assertEquals("foo(double)", MethodUtils.invokeExactMethod(testBean, "foo", new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE }));
1059 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.BYTE_ONE));
1060 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.LONG_ONE));
1061 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", Boolean.TRUE));
1062 assertThrows(NullPointerException.class, () -> MethodUtils.invokeExactMethod(null, "foo", NumberUtils.BYTE_ONE));
1063 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, null, NumberUtils.BYTE_ONE));
1064 assertThrows(NullPointerException.class,
1065 () -> MethodUtils.invokeExactMethod(null, "foo", new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE }));
1066 assertThrows(NoSuchMethodException.class,
1067 () -> MethodUtils.invokeExactMethod(testBean, null, new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE }));
1068 }
1069
1070 @Test
1071 void testInvokeExactStaticMethod() throws Exception {
1072 assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
1073 assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", (Object[]) null));
1074 assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", null, null));
1075 assertEquals("bar(String)", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", ""));
1076 assertEquals("bar(Object)", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", new Object()));
1077 assertEquals("bar(Integer)", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.INTEGER_ONE));
1078 assertEquals("bar(double)",
1079 MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE }));
1080 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.BYTE_ONE));
1081 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.LONG_ONE));
1082 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", Boolean.TRUE));
1083 }
1084
1085 @Test
1086 void testInvokeJavaVarArgsOverloadingResolution() throws Exception {
1087
1088 assertEquals("Byte...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (byte) 1, (byte) 2));
1089 assertEquals("Short...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (short) 1, (short) 2));
1090 assertEquals("Integer...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 2));
1091 assertEquals("Long...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1L, 2L));
1092 assertEquals("Float...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1f, 2f));
1093 assertEquals("Double...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1d, 2d));
1094 assertEquals("Boolean...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", true, false));
1095
1096 assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 1.1));
1097 assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 1L));
1098 assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1d, 1f));
1099 assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (short) 1, (byte) 1));
1100 assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "numOverload", ArrayUtils.EMPTY_OBJECT_ARRAY));
1101 assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "numOverload", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
1102 assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "numOverload", (Object[]) ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY));
1103
1104 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, "s"));
1105 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, true));
1106 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1.1, true));
1107 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 'c', true));
1108 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 'c'));
1109 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 'c', "s"));
1110 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", ArrayUtils.EMPTY_OBJECT_ARRAY));
1111 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
1112 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (Object[]) ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY));
1113 assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload"));
1114
1115 assertEquals("String...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", "a", "b"));
1116 assertEquals("Character...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 'a', 'b'));
1117 }
1118
1119 @Test
1120 void testInvokeMethod() throws Exception {
1121 assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
1122 assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo"));
1123 assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", (Object[]) null));
1124 assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", null, null));
1125 assertEquals("foo(String)", MethodUtils.invokeMethod(testBean, "foo", ""));
1126 assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", new Object()));
1127 assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", Boolean.TRUE));
1128 assertEquals("foo(Integer)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.INTEGER_ONE));
1129 assertEquals("foo(int)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.BYTE_ONE));
1130 assertEquals("foo(long)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.LONG_ONE));
1131 assertEquals("foo(double)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.DOUBLE_ONE));
1132 assertEquals("foo(String...)", MethodUtils.invokeMethod(testBean, "foo", "a", "b", "c"));
1133 assertEquals("foo(String...)", MethodUtils.invokeMethod(testBean, "foo", "a", "b", "c"));
1134 assertEquals("foo(int, String...)", MethodUtils.invokeMethod(testBean, "foo", 5, "a", "b", "c"));
1135 assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", 1L, 2L));
1136 assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", 1, 2));
1137 assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", (byte) 1, (byte) 2));
1138 assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", (short) 1, (short) 2));
1139 assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", (char) 1, (char) 2));
1140 TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y"));
1141 TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", 17, 23, 42));
1142 TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y"));
1143 TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", 17, 23, 42));
1144 assertNullPointerException(() -> MethodUtils.invokeMethod(null, "foo", 1, 2));
1145 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, null, 1, 2));
1146 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(new Object(), "nonExistent", new Object[] { "val" }, new Class<?>[] { null }));
1147 }
1148
1149 @Test
1150 void testInvokeMethod_VarArgsWithNullValues() throws Exception {
1151 assertEquals("String...", MethodUtils.invokeMethod(testBean, "varOverload", "a", null, "c"));
1152 assertEquals("String...", MethodUtils.invokeMethod(testBean, "varOverload", "a", "b", null));
1153 assertEquals("String...", MethodUtils.invokeMethod(testBean, "varOverload", new String[] { "a" }, new Class<?>[] { String.class }));
1154 assertThrows(NoSuchMethodException.class,
1155 () -> assertEquals("String...", MethodUtils.invokeMethod(testBean, "doesn't exist", new String[] { "a" }, new Class<?>[] { null })));
1156 }
1157
1158 @Test
1159 void testInvokeMethod1PlusVarArgs() throws Exception {
1160
1161 assertEquals("int, String...", MethodUtils.invokeMethod(testBean, "intStringVarArg", 1));
1162 assertEquals("int, String...", MethodUtils.invokeMethod(testBean, "intStringVarArg", 1, "s"));
1163 assertEquals("int, String...", MethodUtils.invokeMethod(testBean, "intStringVarArg", 1, "s1", "s2"));
1164 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intStringVarArg", 1, "s1", 5));
1165
1166 assertEquals("int, long...", MethodUtils.invokeMethod(testBean, "intLongVarArg", 1));
1167 assertEquals("int, long...", MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, 2L));
1168 assertEquals("int, long...", MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, 2L, 3L));
1169 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, "s1", 5));
1170
1171 assertEquals("int, int...", MethodUtils.invokeMethod(testBean, "intIntVarArg", 1));
1172 assertEquals("int, int...", MethodUtils.invokeMethod(testBean, "intIntVarArg", 1, 2));
1173 assertEquals("int, int...", MethodUtils.invokeMethod(testBean, "intIntVarArg", 1, 2, 3));
1174 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, "s1", 5));
1175
1176 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intStringVarArg"));
1177 assertNull(MethodUtils.getMatchingAccessibleMethod(TestBean.class, "intStringVarArg"));
1178 }
1179
1180 @Test
1181 void testInvokeMethod2PlusVarArgs() throws Exception {
1182 assertNull(MethodUtils.getMatchingAccessibleMethod(TestBean.class, "intStringStringVarArg", Integer.class));
1183 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intStringStringVarArg", 1));
1184 assertEquals(0, MethodUtils.invokeMethod(testBean, "intStringStringVarArg", 1, "second"));
1185 assertEquals(1, MethodUtils.invokeMethod(testBean, "intStringStringVarArg", 1, "second", "rest"));
1186 }
1187
1188 @Test
1189 void testInvokeMethodForceAccessNoArgs() throws Exception {
1190 assertEquals("privateStringStuff()", MethodUtils.invokeMethod(testBean, true, "privateStringStuff"));
1191 }
1192
1193 @Test
1194 void testInvokeMethodForceAccessWithArgs() throws Exception {
1195 assertEquals("privateStringStuff(Integer)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", 5));
1196 assertEquals("privateStringStuff(double)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", 5.0d));
1197 assertEquals("privateStringStuff(String)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", "Hi There"));
1198 assertEquals("privateStringStuff(Object)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", new Date()));
1199 assertNullPointerException(() -> MethodUtils.invokeMethod(null, true, "privateStringStuff", "Hi There"));
1200 assertNullPointerException(() -> MethodUtils.invokeMethod(testBean, true, null, "Hi There"));
1201 }
1202
1203 @Test
1204 void testInvokeMethodIgnoresPrivateInterfaceMethod() throws Exception {
1205
1206 assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
1207 final Class<?> labels = Class.forName("org.apache.commons.lang3.reflect.testbed9.PrivateInterfaceLabels");
1208 final Object bean = labels.getMethod("newBean").invoke(null);
1209 assertEquals("bean", MethodUtils.invokeMethod(bean, "label"));
1210 assertNull(MethodUtils.getAccessibleMethod(bean.getClass(), "label"));
1211 }
1212
1213 @Test
1214 void testInvokeMethodIgnoresStaticInterfaceMethod() throws Exception {
1215 assertSame(InstanceLabel.class, MethodUtils.getMatchingAccessibleMethod(InstanceLabel.class, "label").getDeclaringClass());
1216 assertEquals("instance", MethodUtils.invokeMethod(new InstanceLabel(), "label"));
1217 assertNull(MethodUtils.getAccessibleMethod(InstanceLabel.class, "label"));
1218 }
1219
1220 @Test
1221 void testInvokeMethodOnNonPublicClass() throws Exception {
1222 assertEquals(0, MethodUtils.invokeMethod(Collections.emptyList(), "size"));
1223 assertEquals(2, MethodUtils.invokeMethod(Arrays.asList(1, 2), "size"));
1224 assertEquals(0, MethodUtils.invokeMethod(Collections.emptyMap(), "size"));
1225 assertEquals(0, MethodUtils.invokeMethod(Collections.unmodifiableList(new ArrayList<>()), "size"));
1226 assertNull(MethodUtils.invokeMethod(new TestMutable(), "getValue"));
1227 }
1228
1229 @Test
1230 void testInvokeMethodVarArgsNotUniqueResolvable() throws Exception {
1231 assertEquals("Boolean...", MethodUtils.invokeMethod(testBean, "varOverload", new Object[] { null }));
1232 assertEquals("Object...", MethodUtils.invokeMethod(testBean, "varOverload", (Object[]) null));
1233 }
1234
1235 @Test
1236 void testInvokeMethodVarArgsOfInterface() throws Exception {
1237
1238 assertEquals("PackagePrivateEmptyInterface...", MethodUtils.invokeMethod(testBean, "packagePrivateEmptyInterface",
1239 new PublicImpl1OfPackagePrivateEmptyInterface(), new PublicImpl2OfPackagePrivateEmptyInterface()));
1240 assertEquals("PackagePrivateEmptyInterface...", MethodUtils.invokeMethod(testBean, "packagePrivateEmptyInterface", new PackagePrivateEmptyInterface() {
1241
1242 }, new PackagePrivateEmptyInterface() {
1243
1244 }));
1245 }
1246
1247 @Test
1248 void testInvokeMethodVarArgsUnboxingBooleanArray() throws Exception {
1249 final TestBean testBean = new TestBean();
1250 final boolean[] actual = (boolean[]) MethodUtils.invokeMethod(testBean, "unboxing", Boolean.TRUE, Boolean.FALSE);
1251 assertArrayEquals(new boolean[] { true, false }, actual);
1252 assertTrue(testBean.unboxBooleanArray);
1253 }
1254
1255 @Test
1256 void testInvokeMethodVarArgsUnboxingByteArray() throws Exception {
1257 final TestBean testBean = new TestBean();
1258 final byte[] actual = (byte[]) MethodUtils.invokeMethod(testBean, "unboxing", Byte.valueOf((byte) 1), Byte.valueOf((byte) 2));
1259 assertArrayEquals(new byte[] { 1, 2 }, actual);
1260 assertTrue(testBean.unboxByteArray);
1261 }
1262
1263 @Test
1264 void testInvokeMethodVarArgsUnboxingCharArray() throws Exception {
1265 final TestBean testBean = new TestBean();
1266 final char[] actual = (char[]) MethodUtils.invokeMethod(testBean, "unboxing", Character.valueOf((char) 1), Character.valueOf((char) 2));
1267 assertArrayEquals(new char[] { 1, 2 }, actual);
1268 assertTrue(testBean.unboxCharArray);
1269 }
1270
1271 @Test
1272 void testInvokeMethodVarArgsUnboxingDoubleArray() throws Exception {
1273 final TestBean testBean = new TestBean();
1274 final double[] actual = (double[]) MethodUtils.invokeMethod(testBean, "unboxing", Double.valueOf(1), Double.valueOf(2));
1275 assertArrayEquals(new double[] { 1, 2 }, actual);
1276 assertTrue(testBean.unboxDoubleArray);
1277 }
1278
1279 @Test
1280 void testInvokeMethodVarArgsUnboxingFloatArray() throws Exception {
1281 final TestBean testBean = new TestBean();
1282 final float[] actual = (float[]) MethodUtils.invokeMethod(testBean, "unboxing", Float.valueOf(1), Float.valueOf(2));
1283 assertArrayEquals(new float[] { 1, 2 }, actual);
1284 assertTrue(testBean.unboxFloatArray);
1285 }
1286
1287 @Test
1288 void testInvokeMethodVarArgsUnboxingIntArray() throws Exception {
1289 final TestBean testBean = new TestBean();
1290 final int[] actual = (int[]) MethodUtils.invokeMethod(testBean, "unboxing", Integer.valueOf(1), Integer.valueOf(2));
1291 assertArrayEquals(new int[] { 1, 2 }, actual);
1292 assertTrue(testBean.unboxIntArray);
1293 }
1294
1295 @Test
1296 void testInvokeMethodVarArgsUnboxingLongArray() throws Exception {
1297 final TestBean testBean = new TestBean();
1298 final long[] actual = (long[]) MethodUtils.invokeMethod(testBean, "unboxing", Long.valueOf(1), Long.valueOf(2));
1299 assertArrayEquals(new long[] { 1, 2 }, actual);
1300 assertTrue(testBean.unboxLongArray);
1301 }
1302
1303 @Test
1304 void testInvokeMethodVarArgsUnboxingShortArray() throws Exception {
1305 final TestBean testBean = new TestBean();
1306 final short[] actual = (short[]) MethodUtils.invokeMethod(testBean, "unboxing", Short.valueOf((short) 1), Short.valueOf((short) 2));
1307 assertArrayEquals(new short[] { 1, 2 }, actual);
1308 assertTrue(testBean.unboxShortArray);
1309 }
1310
1311 @Test
1312 void testInvokeStaticMethod() throws Exception {
1313 assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar"));
1314 assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
1315 assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (Object[]) null));
1316 assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar", null, null));
1317 assertEquals("bar(String)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", ""));
1318 assertEquals("bar(Object)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", new Object()));
1319 assertEquals("bar(Object)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", Boolean.TRUE));
1320 assertEquals("bar(Integer)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.INTEGER_ONE));
1321 assertEquals("bar(int)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.BYTE_ONE));
1322 assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", NumberUtils.BYTE_ONE));
1323 assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", NumberUtils.SHORT_ONE));
1324 assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", NumberUtils.INTEGER_ONE));
1325 assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", 'a'));
1326 assertEquals("bar(double)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.DOUBLE_ONE));
1327 assertEquals("bar(String...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", "a", "b"));
1328 assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", 1L, 2L));
1329 assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (byte) 1, (byte) 2));
1330 assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (short) 1, (short) 2));
1331 assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", 1, 2));
1332 assertEquals("bar(Integer, String...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.INTEGER_ONE, "a", "b"));
1333
1334 assertThrows(NoSuchMethodException.class,
1335 () -> assertEquals("bar(Integer, String...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.SHORT_ONE, "a", "b")));
1336 TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }),
1337 MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", "x", "y"));
1338 TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }),
1339 MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", 17, 23, 42));
1340 TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }),
1341 MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", "x", "y"));
1342 TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }),
1343 MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", 17, 23, 42));
1344 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(TestBean.class, "does_not_exist"));
1345 }
1346
1347 @Test
1348 void testInvokeStaticMethod1PlusVarArgs() throws Exception {
1349
1350 assertEquals("static int, String...", MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1));
1351 assertEquals("static int, String...", MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1, "s"));
1352 assertEquals("static int, String...", MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1, "s1", "s2"));
1353 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1, "s1", 5));
1354
1355 assertEquals("static int, long...", MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1));
1356 assertEquals("static int, long...", MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1, 2L));
1357 assertEquals("static int, long...", MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1, 2L, 3L));
1358 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1, "s1", 5));
1359
1360 assertEquals("static int, int...", MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1));
1361 assertEquals("static int, int...", MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1, 2));
1362 assertEquals("static int, int...", MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1, 2, 3));
1363 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1, "s1", 5));
1364
1365 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg"));
1366 assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(String.class, "format"));
1367 }
1368
1369 @Test
1370 void testInvokeStaticMethodOnNonPublicSubclass() throws Exception {
1371
1372 assertSame(StaticChild.class, MethodUtils.getMatchingAccessibleMethod(StaticChild.class, "who").getDeclaringClass());
1373 assertEquals("child", MethodUtils.invokeStaticMethod(StaticChild.class, "who"));
1374 }
1375
1376 @Test
1377 void testInvokeStaticMethodVarArgsOfInterface() throws Exception {
1378
1379 assertEquals("static PackagePrivateEmptyInterface...", MethodUtils.invokeStaticMethod(TestBean.class, "staticPackagePrivateEmptyInterface",
1380 new PublicImpl1OfPackagePrivateEmptyInterface(), new PublicImpl2OfPackagePrivateEmptyInterface()));
1381 assertEquals("static PackagePrivateEmptyInterface...",
1382 MethodUtils.invokeStaticMethod(TestBean.class, "staticPackagePrivateEmptyInterface", new PackagePrivateEmptyInterface() {
1383
1384 }, new PackagePrivateEmptyInterface() {
1385
1386 }));
1387 }
1388
1389 @Test
1390 void testNullArgument() {
1391 expectMatchingAccessibleMethodParameterTypes(TestBean.class, "oneParameter", singletonArray(null), singletonArray(String.class));
1392 }
1393
1394 @Test
1395 void testVarargsOverloadingResolution() {
1396
1397
1398
1399 assertEquals("Byte...", TestBean.varOverload((byte) 1, (byte) 2));
1400 assertEquals("Short...", TestBean.varOverload((short) 1, (short) 2));
1401 assertEquals("Integer...", TestBean.varOverload(1, 2));
1402 assertEquals("Long...", TestBean.varOverload(1L, 2L));
1403 assertEquals("Float...", TestBean.varOverload(1f, 2f));
1404 assertEquals("Double...", TestBean.varOverload(1d, 2d));
1405 assertEquals("Character...", TestBean.varOverload('a', 'b'));
1406 assertEquals("String...", TestBean.varOverload("a", "b"));
1407 assertEquals("Boolean...", TestBean.varOverload(true, false));
1408 assertEquals("Object...", TestBean.varOverload(1, "s"));
1409 assertEquals("Object...", TestBean.varOverload(1, true));
1410 assertEquals("Object...", TestBean.varOverload(1.1, true));
1411 assertEquals("Object...", TestBean.varOverload('c', true));
1412 assertEquals("Number...", TestBean.varOverload(1, 1.1));
1413 assertEquals("Number...", TestBean.varOverload(1, 1L));
1414 assertEquals("Number...", TestBean.varOverload(1d, 1f));
1415 assertEquals("Number...", TestBean.varOverload((short) 1, (byte) 1));
1416 assertEquals("Object...", TestBean.varOverload(1, 'c'));
1417 assertEquals("Object...", TestBean.varOverload('c', "s"));
1418 }
1419
1420 private String toString(final Class<?>[] c) {
1421 return Arrays.asList(c).toString();
1422 }
1423 }