You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/06/23 18:35:39 UTC

(commons-lang) branch master updated: fix getMatchingMethod false ambiguity for boxed primitive args (#1727)

This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new aae7f0e61 fix getMatchingMethod false ambiguity for boxed primitive args (#1727)
aae7f0e61 is described below

commit aae7f0e61c51040dbdc7556f9eca5786eb9f63bd
Author: alhuda <50...@users.noreply.github.com>
AuthorDate: Wed Jun 24 00:05:33 2026 +0530

    fix getMatchingMethod false ambiguity for boxed primitive args (#1727)
---
 .../java/org/apache/commons/lang3/reflect/MethodUtils.java    |  5 +++++
 .../org/apache/commons/lang3/reflect/MethodUtilsTest.java     | 11 +++++++++++
 2 files changed, 16 insertions(+)

diff --git a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
index a3b734380..a7bd60f67 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -90,7 +90,12 @@ private static int distance(final Class<?>[] fromClassArray, final Class<?>[] to
                 continue;
             }
             if (ClassUtils.isAssignable(aClass, toClass, true) && !ClassUtils.isAssignable(aClass, toClass, false)) {
+                // Autoboxing/unboxing conversion. When a primitive is boxed, rank the exact
+                // wrapper ahead of any of its supertypes so the most specific overload wins.
                 answer++;
+                if (aClass.isPrimitive() && !ClassUtils.primitiveToWrapper(aClass).equals(toClass)) {
+                    answer += 2;
+                }
             } else {
                 answer += 2;
             }
diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
index d6c250fde..111054ed3 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -118,6 +118,12 @@ public void testMethod4(final Color aColor1, final Color aColor2) {
 
         public void testMethod4(final Long aLong, final Long anotherLong) {
         }
+
+        public void testMethod5(final Integer anInteger) {
+        }
+
+        public void testMethod5(final Number aNumber) {
+        }
     }
 
     private static final class GetMatchingMethodImpl extends AbstractGetMatchingMethod {
@@ -813,6 +819,11 @@ void testGetMatchingMethod() throws NoSuchMethodException {
         assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", Long.TYPE, null),
                 GetMatchingMethodClass.class.getMethod("testMethod3", Long.TYPE, Long.class));
         assertThrows(IllegalStateException.class, () -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod4", null, null));
+        // A primitive argument boxes to the exact wrapper overload, not to one of its supertypes.
+        assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod5", Integer.TYPE),
+                GetMatchingMethodClass.class.getMethod("testMethod5", Integer.class));
+        assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod5", Integer.class),
+                GetMatchingMethodClass.class.getMethod("testMethod5", Integer.class));
         assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod5", RuntimeException.class),
                 GetMatchingMethodImpl.class.getMethod("testMethod5", Exception.class));
         assertEquals(GetMatchingMethodImpl.class.getMethod("testMethod6"), MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod6"));