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

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

commit c290aebffa83b382a24d74117755904017f82b7c
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri May 24 10:13:23 2024 -0400

    Add and use MethodUtils.getMethodObject(Class<?>, String, Class<?>...)
    
    Javadoc
---
 src/changes/changes.xml                            |  1 +
 .../commons/lang3/exception/ExceptionUtils.java    |  9 +---
 .../apache/commons/lang3/reflect/MethodUtils.java  | 55 ++++++++++++----------
 .../commons/lang3/reflect/MethodUtilsTest.java     |  8 ++++
 4 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c0b589429..a49c7b705 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -64,6 +64,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add 
org.apache.commons.lang3.SystemProperties.getLineSeparator(Supplier&lt;String&gt;).</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add 
org.apache.commons.lang3.SystemProperties.getJavaSpecificationVersion(Supplier&lt;String&gt;).</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add SystemProperties constants and methods for system properties as of 
Java 22.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add MethodUtils.getMethodObject(Class, String, Class...).</action>
     <!-- FIX -->
     <action                   type="fix" dev="ggregory" due-to="Miklós Karakó, 
Gary Gregory">Improve Javadoc in ExceptionUtils #1136.</action>
     <action                   type="fix" dev="ggregory" due-to="Saiharshith 
Karuneegar Ramesh, Gary Gregory">Fixed two non-deterministic tests in 
EnumUtilsTest.java #1131.</action>
diff --git 
a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java 
b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
index c1087b109..a458fcd1a 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -32,6 +32,7 @@ import java.util.stream.Stream;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.ClassUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
 
 /**
  * Provides utilities for manipulating and examining
@@ -247,13 +248,7 @@ public class ExceptionUtils {
     // TODO: Remove in Lang 4
     private static Throwable getCauseUsingMethodName(final Throwable 
throwable, final String methodName) {
         if (methodName != null) {
-            Method method = null;
-            try {
-                method = throwable.getClass().getMethod(methodName);
-            } catch (final NoSuchMethodException | SecurityException ignored) {
-                // exception ignored
-            }
-
+            Method method = MethodUtils.getMethodObject(throwable.getClass(), 
methodName);
             if (method != null && 
Throwable.class.isAssignableFrom(method.getReturnType())) {
                 try {
                     return (Throwable) method.invoke(throwable);
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 361c50824..fe620c2de 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -96,24 +96,16 @@ public class MethodUtils {
     }
 
     /**
-     * Gets an accessible method (that is, one that can be invoked via
-     * reflection) with given name and parameters. If no such method
-     * can be found, return {@code null}.
-     * This is just a convenience wrapper for
-     * {@link #getAccessibleMethod(Method)}.
+     * Gets an accessible method (that is, one that can be invoked via 
reflection) with given name and parameters. If no such method can be found, 
return
+     * {@code null}. This is just a convenience wrapper for {@link 
#getAccessibleMethod(Method)}.
      *
-     * @param cls get method from this class
-     * @param methodName get method with this name
+     * @param cls            get method from this class
+     * @param methodName     get method with this name
      * @param parameterTypes with these parameters types
      * @return The accessible method
      */
-    public static Method getAccessibleMethod(final Class<?> cls, final String 
methodName,
-        final Class<?>... parameterTypes) {
-        try {
-            return getAccessibleMethod(cls.getMethod(methodName, 
parameterTypes));
-        } catch (final NoSuchMethodException e) {
-            return null;
-        }
+    public static Method getAccessibleMethod(final Class<?> cls, final String 
methodName, final Class<?>... parameterTypes) {
+        return getAccessibleMethod(getMethodObject(cls, methodName, 
parameterTypes));
     }
 
     /**
@@ -121,7 +113,7 @@ public class MethodUtils {
      * reflection) that implements the specified Method. If no such method
      * can be found, return {@code null}.
      *
-     * @param method The method that we wish to call
+     * @param method The method that we wish to call, may be null.
      * @return The accessible method
      */
     public static Method getAccessibleMethod(Method method) {
@@ -211,11 +203,7 @@ public class MethodUtils {
         Class<?> parentClass = cls.getSuperclass();
         while (parentClass != null) {
             if (ClassUtils.isPublic(parentClass)) {
-                try {
-                    return parentClass.getMethod(methodName, parameterTypes);
-                } catch (final NoSuchMethodException e) {
-                    return null;
-                }
+                return getMethodObject(parentClass, methodName, 
parameterTypes);
             }
             parentClass = parentClass.getSuperclass();
         }
@@ -267,7 +255,7 @@ public class MethodUtils {
      * @param <A>
      *            the annotation type
      * @param method
-     *            the {@link Method} to query
+     *            the {@link Method} to query, may be null.
      * @param annotationCls
      *            the {@link Annotation} to check if is present on the method
      * @param searchSupers
@@ -332,10 +320,9 @@ public class MethodUtils {
      */
     public static Method getMatchingAccessibleMethod(final Class<?> cls,
         final String methodName, final Class<?>... parameterTypes) {
-        try {
-            return 
MemberUtils.setAccessibleWorkaround(cls.getMethod(methodName, parameterTypes));
-        } catch (final NoSuchMethodException ignored) {
-            // Swallow the exception
+        final Method candidate = getMethodObject(cls, methodName, 
parameterTypes);
+        if (candidate != null) {
+            return MemberUtils.setAccessibleWorkaround(candidate);
         }
         // search through all methods
         final Method[] methods = cls.getMethods();
@@ -437,6 +424,24 @@ public class MethodUtils {
         );
     }
 
+    /**
+     * Gets a Method or null if a {@link Class#getMethod(String, Class...) 
documented} exception is thrown.
+     *
+     * @param cls Receiver for {@link Class#getMethod(String, Class...)}.
+     * @param name the name of the method
+     * @param parameterTypes the list of parameters
+     * @return a Method or null.
+     * @since 3.15.0
+     * @see Class#getMethod(String, Class...)
+     */
+    public static Method getMethodObject(final Class<?> cls, final String 
name, final Class<?>... parameterTypes) {
+        try {
+            return cls.getMethod(name, parameterTypes);
+        } catch (final NoSuchMethodException | SecurityException e) {
+            return null;
+        }
+    }
+
     /**
      * Gets all class level public methods of the given class that are 
annotated with the given annotation.
      * @param cls
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 9709f7b9d..bc3ce272e 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -71,6 +71,7 @@ public class MethodUtilsTest extends AbstractLangTest {
     }
 
     private static final class GetMatchingMethodClass {
+
         public void testMethod() {
         }
 
@@ -685,6 +686,13 @@ public class MethodUtilsTest extends AbstractLangTest {
                 () -> MethodUtils.getMatchingMethod(null, "testMethod5", 
RuntimeException.class));
     }
 
+    @Test
+    public void testGetMethodObject() throws Exception {
+        assertEquals(MutableObject.class.getMethod("getValue", 
ArrayUtils.EMPTY_CLASS_ARRAY),
+                MethodUtils.getMethodObject(MutableObject.class, "getValue", 
ArrayUtils.EMPTY_CLASS_ARRAY));
+        assertNull(MethodUtils.getMethodObject(MutableObject.class, "does not 
exist, at all", ArrayUtils.EMPTY_CLASS_ARRAY));
+    }
+
     @Test
     @Annotated
     public void testGetMethodsListWithAnnotation() throws 
NoSuchMethodException {

Reply via email to