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 2022/03/07 16:18:54 UTC
[commons-lang] 01/04: Add early null check.
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 72ad3cbc1aabc9c0a3e0846f152172cdca9d42c1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Mar 7 11:00:24 2022 -0500
Add early null check.
---
.../org/apache/commons/lang3/reflect/MethodUtils.java | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 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 eff0ee8..591fc73 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -31,6 +31,7 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
@@ -201,32 +202,29 @@ public class MethodUtils {
* @throws IllegalAccessException if the requested method is not accessible via reflection
* @since 3.5
*/
- public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName,
- Object[] args, Class<?>[] parameterTypes)
- throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, Object[] args, Class<?>[] parameterTypes)
+ throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ Objects.requireNonNull(object, "object");
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
args = ArrayUtils.nullToEmpty(args);
final String messagePrefix;
final Method method;
+ final Class<? extends Object> cls = object.getClass();
if (forceAccess) {
messagePrefix = "No such method: ";
- method = getMatchingMethod(object.getClass(),
- methodName, parameterTypes);
+ method = getMatchingMethod(cls, methodName, parameterTypes);
if (method != null && !method.isAccessible()) {
method.setAccessible(true);
}
} else {
messagePrefix = "No such accessible method: ";
- method = getMatchingAccessibleMethod(object.getClass(),
- methodName, parameterTypes);
+ method = getMatchingAccessibleMethod(cls, methodName, parameterTypes);
}
if (method == null) {
- throw new NoSuchMethodException(messagePrefix
- + methodName + "() on object: "
- + object.getClass().getName());
+ throw new NoSuchMethodException(messagePrefix + methodName + "() on object: " + cls.getName());
}
args = toVarArgs(method, args);