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 f164995c9 Restrict MethodUtils static invocation to static overloads 
(#1881)
f164995c9 is described below

commit f164995c904a592b832e98d8073f1f9051578dd7
Author: Amar Ghugare <[email protected]>
AuthorDate: Wed Nov 18 18:34:38 2026 +0530

    Restrict MethodUtils static invocation to static overloads (#1881)
    
    Co-authored-by: Gary Gregory <[email protected]>
---
 .../apache/commons/lang3/reflect/MethodUtils.java  | 16 +++--
 .../commons/lang3/reflect/MethodUtilsTest.java     | 68 ++++++++++++++++++++++
 2 files changed, 79 insertions(+), 5 deletions(-)

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 cd916cc3a..efa329672 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -372,9 +372,14 @@ private static Method getInvokeMethod(final boolean 
forceAccess, final String me
      * @see SecurityManager#checkPermission
      */
     public static Method getMatchingAccessibleMethod(final Class<?> cls, final 
String methodName, final Class<?>... requestTypes) {
+        return getMatchingAccessibleMethod(cls, methodName, false, 
requestTypes);
+    }
+
+    private static Method getMatchingAccessibleMethod(final Class<?> cls, 
final String methodName, final boolean staticOnly,
+            final Class<?>... requestTypes) {
         final Class<?>[] actualTypes = ArrayUtils.nullToEmpty(requestTypes);
         final Method candidate = getMethodObject(cls, methodName, actualTypes);
-        if (candidate != null) {
+        if (candidate != null && (!staticOnly || 
MemberUtils.isStatic(candidate))) {
             // The exact match may be declared on a non-public class, so 
prefer the public
             // declaration the way the search below does; a static method 
hides, so it is kept.
             final Method accessibleCandidate = MemberUtils.isStatic(candidate) 
? null : getAccessibleMethod(cls, candidate);
@@ -383,7 +388,8 @@ public static Method getMatchingAccessibleMethod(final 
Class<?> cls, final Strin
         // search through all methods
         final Method[] methods = cls.getMethods();
         final List<Method> matchingMethods = Stream.of(methods)
-                .filter(method -> method.getName().equals(methodName) && 
MemberUtils.isMatchingMethod(method, actualTypes)).collect(Collectors.toList());
+                .filter(method -> method.getName().equals(methodName) && 
(!staticOnly || MemberUtils.isStatic(method))
+                        && MemberUtils.isMatchingMethod(method, 
actualTypes)).collect(Collectors.toList());
         // Sort methods by signature to force deterministic result
         matchingMethods.sort(METHOD_BY_SIGNATURE);
         Method bestMatch = null;
@@ -753,8 +759,8 @@ public static Object invokeExactStaticMethod(final Class<?> 
cls, final String me
     public static Object invokeExactStaticMethod(final Class<?> cls, final 
String methodName, final Object[] args, final Class<?>[] parameterTypes)
             throws NoSuchMethodException, IllegalAccessException, 
InvocationTargetException {
         final Class<?>[] paramTypes = ArrayUtils.nullToEmpty(parameterTypes);
-        final Method method = getAccessibleMethod(cls, methodName, 
ArrayUtils.nullToEmpty(paramTypes));
-        requireNonNull(method, cls, methodName, paramTypes);
+        final Method candidate = getAccessibleMethod(cls, methodName, 
ArrayUtils.nullToEmpty(paramTypes));
+        final Method method = requireNonNull(candidate != null && 
MemberUtils.isStatic(candidate) ? candidate : null, cls, methodName, 
paramTypes);
         return method.invoke(null, ArrayUtils.nullToEmpty(args));
     }
 
@@ -1053,7 +1059,7 @@ public static Object invokeStaticMethod(final Class<?> 
cls, final String methodN
     public static Object invokeStaticMethod(final Class<?> cls, final String 
methodName, final Object[] args, final Class<?>[] parameterTypes)
             throws NoSuchMethodException, IllegalAccessException, 
InvocationTargetException {
         final Class<?>[] paramTypes = ArrayUtils.nullToEmpty(parameterTypes);
-        final Method method = getMatchingAccessibleMethod(cls, methodName, 
paramTypes);
+        final Method method = getMatchingAccessibleMethod(cls, methodName, 
true, paramTypes);
         requireNonNull(method, cls, methodName, paramTypes);
         return method.invoke(null, toVarArgs(method, 
ArrayUtils.nullToEmpty(args)));
     }
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 c92910fe2..b2ff49abf 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -228,6 +228,46 @@ static String label() {
         }
     }
 
+    public static class StaticOverloadBean extends StaticOverloadParent {
+        public static String choose(final Number value) {
+            return "static";
+        }
+
+        public static String varargs(final Number... values) {
+            return "static varargs";
+        }
+
+        public static String widen(final long value) {
+            return "static long";
+        }
+
+        public String choose(final Integer value) {
+            return "instance";
+        }
+
+        public String inherited(final Integer value) {
+            return "instance";
+        }
+
+        public String instanceOnly(final Number value) {
+            return "instance";
+        }
+
+        public String varargs(final Integer... values) {
+            return "instance varargs";
+        }
+
+        public String widen(final int value) {
+            return "instance int";
+        }
+    }
+
+    public static class StaticOverloadParent {
+        public static String inherited(final Number value) {
+            return "inherited static";
+        }
+    }
+
     public static class StaticParent {
         public static String who() {
             return "parent";
@@ -1120,6 +1160,13 @@ void testInvokeExactStaticMethod() throws Exception {
         assertThrows(NoSuchMethodException.class, () -> 
MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", Boolean.TRUE));
     }
 
+    @Test
+    void testInvokeExactStaticMethodRejectsInstanceMethod() {
+        assertThrows(NoSuchMethodException.class, () -> 
MethodUtils.invokeExactStaticMethod(StaticOverloadBean.class, "choose", 
Integer.valueOf(1)));
+        assertThrows(NoSuchMethodException.class, () -> 
MethodUtils.invokeExactStaticMethod(StaticOverloadBean.class, "instanceOnly",
+                new Object[] { Integer.valueOf(1) }, new Class<?>[] { 
Number.class }));
+    }
+
     @Test
     void testInvokeJavaVarArgsOverloadingResolution() throws Exception {
         // Primitive wrappers
@@ -1408,6 +1455,27 @@ void testInvokeStaticMethod1PlusVarArgs() throws 
Exception {
         assertThrows(NoSuchMethodException.class, () -> 
MethodUtils.invokeStaticMethod(String.class, "format", (Object[]) null));
     }
 
+    @Test
+    void testInvokeStaticMethodIgnoresInstanceOverloads() throws Exception {
+        assertEquals("static", 
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "choose", 
Integer.valueOf(1)));
+        assertEquals("static", 
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "choose",
+                new Object[] { Integer.valueOf(1) }, new Class<?>[] { 
Integer.class }));
+        assertEquals("static long", 
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "widen", 
Short.valueOf((short) 1)));
+        assertEquals("static varargs", 
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "varargs", 
Integer.valueOf(1), Integer.valueOf(2)));
+        assertEquals("inherited static", 
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "inherited", 
Integer.valueOf(1)));
+        final StaticOverloadBean bean = new StaticOverloadBean();
+        assertEquals("instance", MethodUtils.invokeMethod(bean, "choose", 
Integer.valueOf(1)));
+        assertEquals("instance int", MethodUtils.invokeMethod(bean, "widen", 
Short.valueOf((short) 1)));
+        assertEquals("instance varargs", MethodUtils.invokeMethod(bean, 
"varargs", Integer.valueOf(1), Integer.valueOf(2)));
+    }
+
+    @Test
+    void testInvokeStaticMethodRejectsInstanceMethod() {
+        assertThrows(NoSuchMethodException.class, () -> 
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "instanceOnly", 
Integer.valueOf(1)));
+        assertThrows(NoSuchMethodException.class, () -> 
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "instanceOnly",
+                new Object[] { Integer.valueOf(1) }, new Class<?>[] { 
Number.class }));
+    }
+
     @Test
     void testInvokeStaticMethodOnNonPublicSubclass() throws Exception {
         // A static method hides rather than overrides, so the subclass's own 
declaration is invoked.

Reply via email to