Java Utililty Methods Reflection Method Invoke

List of utility methods to do Reflection Method Invoke

Description

The list of methods to do Reflection Method Invoke are organized into topic(s).

Method

ObjectinvokeMethod(Object source, String method, Collection arguments)
invoke Method
Collection<Class<?>> argumentClasses = new ArrayList<Class<?>>();
for (Object argument : arguments) {
    argumentClasses.add(argument.getClass());
if (source == null)
    return null;
for (Method m : source.getClass().getMethods()) {
    if (m.getName().equalsIgnoreCase(method)) {
...
ObjectinvokeMethod(Object sourceObject, String methodName, Object... arguments)
invoke Method
Object returnValue;
Method[] methodArray = sourceObject.getClass().getDeclaredMethods();
Method metodo = null;
for (Method m : methodArray) {
    if (m.getName().equals(methodName)) {
        metodo = m;
if (metodo == null) {
    throw new RuntimeException("Couldn't find methos.");
metodo.setAccessible(true);
try {
    returnValue = metodo.invoke(sourceObject, arguments);
} catch (Exception e) {
    throw new RuntimeException("Couldn't invoke method: " + methodName, e);
return returnValue;
ObjectinvokeMethod(Object target, Class clazz, String methodName, Class[] types, Object[] args)
Invoke method.
final Method m = findMethod(clazz, methodName, types);
try {
    return m.invoke(target, args);
} catch (Exception e) {
    throw new RuntimeException(e);
ObjectinvokeMethod(Object target, Method method)
invoke Method
if (method.isAccessible()) {
    return method.invoke(target);
} else {
    method.setAccessible(true);
    Object rtn = method.invoke(target);
    method.setAccessible(false);
    return rtn;
ObjectinvokeMethod(Object target, Method method)
invoke Method
return invokeMethod(target, method, null);
ObjectinvokeMethod(Object target, Method method, Object... args)
invoke Method
try {
    return method.invoke(target, args);
} catch (Exception e) {
    throw new RuntimeException(
            String.format("target: %s method: %s args: %s", target, method, Arrays.toString(args)), e);
voidinvokeMethod(Object target, Method method, Object[] args)
invoke Method
try {
    method.setAccessible(true);
    method.invoke(target, args);
} catch (IllegalAccessException ex) {
    throw new RuntimeException(
            "Unable to invoke method '" + method.getName() + "' for class " + target.getClass().getName(),
            ex);
} catch (IllegalArgumentException ex) {
...
ObjectinvokeMethod(Object target, String method)
invokes the given method to the given target using reflection.
return getMethodInvoker(method).apply(target);
ObjectinvokeMethod(Object target, String methodName, Object... parameters)
invoke Method
try {
    final Method method = extractMethod(target, methodName);
    method.setAccessible(true);
    return method.invoke(target, parameters);
} catch (Exception e) {
    throw new RuntimeException("Could not invoke method.", e);
ObjectinvokeMethod(Object target, String methodName, Object[] arguments)
invoke Method
Class targetClass = target.getClass();
Class[] argumentTypes = new Class[arguments.length];
for (int i = 0; i < argumentTypes.length; i++) {
    argumentTypes[i] = arguments[i].getClass();
try {
    Method method = targetClass.getMethod(methodName, argumentTypes);
    return method.invoke(target, arguments);
...