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 1af74dd83 Restrict MethodUtils static invocation to static overloads
(#1881)
1af74dd83 is described below
commit 1af74dd8373965b28f876520265a1a24183ffb71
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 18 08:10:40 2026 -0500
Restrict MethodUtils static invocation to static overloads (#1881)
Clarify MethodUtils static invocation contracts and tests
Document static-only overload selection and NoSuchMethodException
behavior.
Remove inapplicable instance-receiver exception clauses.
Add coverage for null arguments, zero-argument instance-only methods,
and
successful exact static lookup with competing overloads. Record the
behavior change from #1881 in the 3.22.1 changelog.
Assisted-By: OpenAI Codex
---
src/changes/changes.xml | 1 +
.../apache/commons/lang3/reflect/MethodUtils.java | 69 +++++++++++-----------
.../commons/lang3/reflect/MethodUtilsTest.java | 29 +++++++--
3 files changed, 59 insertions(+), 40 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4e459d26e..532387408 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -101,6 +101,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Bound speculative initial capacity in Strings.replace by the possible
match count and a 20% growth limit, avoiding premature memory exhaustion for
large replacements.</action>
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Correct Strings Javadoc for case sensitivity, null ordering, prefix
and suffix matching, and search bounds.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory,
Amar Ghugare">Preserve interrupt status in BackgroundInitializer.isInitialized
(#1879).</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory,
Amar Ghugare">Restrict MethodUtils.invokeStaticMethod to static overloads; both
static invocation variants now throw NoSuchMethodException instead of
NullPointerException when only an instance method matches (#1881).</action>
<!-- ADD -->
<!-- UPDATE -->
</release>
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 efa329672..649bf1caa 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -353,28 +353,6 @@ private static Method getInvokeMethod(final boolean
forceAccess, final String me
return method;
}
- /**
- * Gets 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>
- * This method is used by {@link #invokeMethod(Object object, String
methodName, Object[] args, Class[] parameterTypes)}.
- * </p>
- * <p>
- * This method can match primitive parameter by passing in wrapper
classes. For example, a {@link Boolean} will match a primitive {@code boolean}
parameter.
- * </p>
- *
- * @param cls find method in this class.
- * @param methodName find method with this name.
- * @param requestTypes The parameter types to match, {@code null} treated
as empty.
- * @return The accessible method or null.
- * @throws SecurityException Thrown if an underlying accessible object's
method denies the request.
- * @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);
@@ -416,6 +394,28 @@ private static Method getMatchingAccessibleMethod(final
Class<?> cls, final Stri
return bestMatch;
}
+ /**
+ * Gets 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>
+ * This method is used by {@link #invokeMethod(Object object, String
methodName, Object[] args, Class[] parameterTypes)}.
+ * </p>
+ * <p>
+ * This method can match primitive parameter by passing in wrapper
classes. For example, a {@link Boolean} will match a primitive {@code boolean}
parameter.
+ * </p>
+ *
+ * @param cls find method in this class.
+ * @param methodName find method with this name.
+ * @param requestTypes The parameter types to match, {@code null} treated
as empty.
+ * @return The accessible method or null.
+ * @throws SecurityException Thrown if an underlying accessible object's
method denies the request.
+ * @see SecurityManager#checkPermission
+ */
+ public static Method getMatchingAccessibleMethod(final Class<?> cls, final
String methodName, final Class<?>... requestTypes) {
+ return getMatchingAccessibleMethod(cls, methodName, false,
requestTypes);
+ }
+
/**
* Gets a method whether or not it's accessible. If no such method can be
found, return {@code null}.
*
@@ -702,19 +702,18 @@ public static Object invokeExactMethod(final Object
object, final String methodN
*
* <p>
* This uses reflection to invoke the method obtained from a call to
{@link #getAccessibleMethod(Class, String, Class[])}.
+ * The method must be static; a compatible overload with a different
signature is not considered.
* </p>
*
* @param cls invoke static method on this class.
* @param methodName get method with this name.
* @param args use these arguments - treat {@code null} as empty
array.
* @return The value returned by the invoked method.
- * @throws NoSuchMethodException Thrown if there is no such
accessible method.
+ * @throws NoSuchMethodException Thrown if there is no accessible
static method with the exact parameter types.
* @throws IllegalAccessException Thrown if this found {@code Method}
is enforcing Java language access control and the underlying method is
* inaccessible.
* @throws IllegalArgumentException Thrown if:
* <ul>
- * <li>the found {@code Method} is an
instance method and the specified {@code object} argument is not an instance of
- * the class or interface declaring
the underlying method (or of a subclass or interface implementor);</li>
* <li>the number of actual and formal
parameters differ;</li>
* <li>an unwrapping conversion for
primitive arguments fails; or</li>
* <li>after possible unwrapping, a
parameter value can't be converted to the corresponding formal parameter type
by a
@@ -734,6 +733,7 @@ public static Object invokeExactStaticMethod(final Class<?>
cls, final String me
*
* <p>
* This uses reflection to invoke the method obtained from a call to
{@link #getAccessibleMethod(Class, String, Class[])}.
+ * The method must be static; a compatible overload with a different
signature is not considered.
* </p>
*
* @param cls invoke static method on this class.
@@ -741,13 +741,11 @@ public static Object invokeExactStaticMethod(final
Class<?> cls, final String me
* @param args use these arguments - treat {@code null} as empty
array.
* @param parameterTypes match these parameters - treat {@code null} as
empty array.
* @return The value returned by the invoked method.
- * @throws NoSuchMethodException Thrown if there is no such
accessible method.
+ * @throws NoSuchMethodException Thrown if there is no accessible
static method with the exact parameter types.
* @throws IllegalAccessException Thrown if this found {@code Method}
is enforcing Java language access control and the underlying method is
* inaccessible.
* @throws IllegalArgumentException Thrown if:
* <ul>
- * <li>the found {@code Method} is an
instance method and the specified {@code object} argument is not an instance of
- * the class or interface declaring
the underlying method (or of a subclass or interface implementor);</li>
* <li>the number of actual and formal
parameters differ;</li>
* <li>an unwrapping conversion for
primitive arguments fails; or</li>
* <li>after possible unwrapping, a
parameter value can't be converted to the corresponding formal parameter type
by a
@@ -985,13 +983,15 @@ public static Object invokeMethod(final Object object,
final String methodName,
* Invokes a named {@code static} method whose parameter type matches the
object type.
*
* <p>
- * This method delegates the method search to {@link
#getMatchingAccessibleMethod(Class, String, Class[])}.
+ * This method uses the matching rules of {@link
#getMatchingAccessibleMethod(Class, String, Class[])}, but considers only
static methods.
* </p>
+ *
* <p>
* This method supports calls to methods taking primitive parameters
* via passing in wrapping classes. So, for example, a {@link Boolean}
class
* would match a {@code boolean} primitive.
* </p>
+ *
* <p>
* This is a convenient wrapper for
* {@link #invokeStaticMethod(Class, String, Object[], Class[])}.
@@ -1001,13 +1001,11 @@ public static Object invokeMethod(final Object object,
final String methodName,
* @param methodName get method with this name.
* @param args use these arguments - treat {@code null} as empty array.
* @return The value returned by the invoked method.
- * @throws NoSuchMethodException Thrown if there is no such
accessible method.
+ * @throws NoSuchMethodException Thrown if there is no matching
accessible static method.
* @throws IllegalAccessException Thrown if this found {@code Method}
is enforcing Java language access control and the underlying method is
* inaccessible.
* @throws IllegalArgumentException Thrown if:
* <ul>
- * <li>the found {@code Method} is an
instance method and the specified {@code object} argument is not an instance of
- * the class or interface declaring
the underlying method (or of a subclass or interface implementor);</li>
* <li>the number of actual and formal
parameters differ;</li>
* <li>an unwrapping conversion for
primitive arguments fails; or</li>
* <li>after possible unwrapping, a
parameter value can't be converted to the corresponding formal parameter type
by a
@@ -1027,8 +1025,9 @@ public static Object invokeStaticMethod(final Class<?>
cls, final String methodN
* Invokes a named {@code static} method whose parameter type matches the
object type.
*
* <p>
- * This method delegates the method search to {@link
#getMatchingAccessibleMethod(Class, String, Class[])}.
+ * This method uses the matching rules of {@link
#getMatchingAccessibleMethod(Class, String, Class[])}, but considers only
static methods.
* </p>
+ *
* <p>
* This method supports calls to methods taking primitive parameters
* via passing in wrapping classes. So, for example, a {@link Boolean}
class
@@ -1040,13 +1039,11 @@ public static Object invokeStaticMethod(final Class<?>
cls, final String methodN
* @param args use these arguments - treat {@code null} as empty array.
* @param parameterTypes match these parameters - treat {@code null} as
empty array.
* @return The value returned by the invoked method.
- * @throws NoSuchMethodException Thrown if there is no such
accessible method.
+ * @throws NoSuchMethodException Thrown if there is no matching
accessible static method.
* @throws IllegalAccessException Thrown if this found {@code Method}
is enforcing Java language access control and the underlying method is
* inaccessible.
* @throws IllegalArgumentException Thrown if:
* <ul>
- * <li>the found {@code Method} is an
instance method and the specified {@code object} argument is not an instance of
- * the class or interface declaring
the underlying method (or of a subclass or interface implementor);</li>
* <li>the number of actual and formal
parameters differ;</li>
* <li>an unwrapping conversion for
primitive arguments fails; or</li>
* <li>after possible unwrapping, a
parameter value can't be converted to the corresponding formal parameter type
by a
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 b2ff49abf..da5d1a1ba 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -249,6 +249,10 @@ public String inherited(final Integer value) {
return "instance";
}
+ public String instanceOnly() {
+ return "instance";
+ }
+
public String instanceOnly(final Number value) {
return "instance";
}
@@ -1163,10 +1167,19 @@ void testInvokeExactStaticMethod() throws Exception {
@Test
void testInvokeExactStaticMethodRejectsInstanceMethod() {
assertThrows(NoSuchMethodException.class, () ->
MethodUtils.invokeExactStaticMethod(StaticOverloadBean.class, "choose",
Integer.valueOf(1)));
+ assertThrows(NoSuchMethodException.class, () ->
MethodUtils.invokeExactStaticMethod(StaticOverloadBean.class, "instanceOnly"));
assertThrows(NoSuchMethodException.class, () ->
MethodUtils.invokeExactStaticMethod(StaticOverloadBean.class, "instanceOnly",
new Object[] { Integer.valueOf(1) }, new Class<?>[] {
Number.class }));
}
+ @Test
+ void testInvokeExactStaticMethodSelectsStaticOverload() throws Exception {
+ assertEquals("static",
MethodUtils.invokeExactStaticMethod(StaticOverloadBean.class, "choose",
+ new Object[] { Integer.valueOf(1) }, new Class<?>[] {
Number.class }));
+ assertEquals("static",
MethodUtils.invokeExactStaticMethod(StaticOverloadBean.class, "choose",
+ new Object[] { null }, new Class<?>[] { Number.class }));
+ }
+
@Test
void testInvokeJavaVarArgsOverloadingResolution() throws Exception {
// Primitive wrappers
@@ -1470,10 +1483,10 @@ void testInvokeStaticMethodIgnoresInstanceOverloads()
throws Exception {
}
@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 }));
+ void testInvokeStaticMethodIgnoresInstanceOverloadsWithNullArgument()
throws Exception {
+ assertEquals("static",
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "choose", new Object[]
{ null }));
+ assertEquals("static",
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "choose",
+ new Object[] { null }, new Class<?>[] { Integer.class }));
}
@Test
@@ -1483,6 +1496,14 @@ void testInvokeStaticMethodOnNonPublicSubclass() throws
Exception {
assertEquals("child",
MethodUtils.invokeStaticMethod(StaticChild.class, "who"));
}
+ @Test
+ void testInvokeStaticMethodRejectsInstanceMethod() {
+ assertThrows(NoSuchMethodException.class, () ->
MethodUtils.invokeStaticMethod(StaticOverloadBean.class, "instanceOnly"));
+ 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 testInvokeStaticMethodVarArgsOfInterface() throws Exception {
// staticPackagePrivateEmptyInterface