Add early null check.
This commit is contained in:
parent
abb5a409f7
commit
72ad3cbc1a
|
@ -31,6 +31,7 @@ import java.util.Iterator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -201,32 +202,29 @@ public class MethodUtils {
|
||||||
* @throws IllegalAccessException if the requested method is not accessible via reflection
|
* @throws IllegalAccessException if the requested method is not accessible via reflection
|
||||||
* @since 3.5
|
* @since 3.5
|
||||||
*/
|
*/
|
||||||
public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName,
|
public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, Object[] args, Class<?>[] parameterTypes)
|
||||||
Object[] args, Class<?>[] parameterTypes)
|
|
||||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||||
|
Objects.requireNonNull(object, "object");
|
||||||
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
|
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
|
||||||
args = ArrayUtils.nullToEmpty(args);
|
args = ArrayUtils.nullToEmpty(args);
|
||||||
|
|
||||||
final String messagePrefix;
|
final String messagePrefix;
|
||||||
final Method method;
|
final Method method;
|
||||||
|
|
||||||
|
final Class<? extends Object> cls = object.getClass();
|
||||||
if (forceAccess) {
|
if (forceAccess) {
|
||||||
messagePrefix = "No such method: ";
|
messagePrefix = "No such method: ";
|
||||||
method = getMatchingMethod(object.getClass(),
|
method = getMatchingMethod(cls, methodName, parameterTypes);
|
||||||
methodName, parameterTypes);
|
|
||||||
if (method != null && !method.isAccessible()) {
|
if (method != null && !method.isAccessible()) {
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
messagePrefix = "No such accessible method: ";
|
messagePrefix = "No such accessible method: ";
|
||||||
method = getMatchingAccessibleMethod(object.getClass(),
|
method = getMatchingAccessibleMethod(cls, methodName, parameterTypes);
|
||||||
methodName, parameterTypes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
throw new NoSuchMethodException(messagePrefix
|
throw new NoSuchMethodException(messagePrefix + methodName + "() on object: " + cls.getName());
|
||||||
+ methodName + "() on object: "
|
|
||||||
+ object.getClass().getName());
|
|
||||||
}
|
}
|
||||||
args = toVarArgs(method, args);
|
args = toVarArgs(method, args);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue