Java Reflection Method Invoke invokeMethod(Object object, String name, Object... arguments)

Here you can find the source of invokeMethod(Object object, String name, Object... arguments)

Description

invoke Method

License

Apache License

Declaration

public static Object invokeMethod(Object object, String name, Object... arguments) throws SecurityException,
            NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static Object invokeMethod(Object object, String name, Object... arguments) throws SecurityException,
            NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Class<?> clazz = object.getClass();
        Class<?> argumentClasses[] = new Class<?>[arguments.length];
        for (int argumentId = 0; argumentId < arguments.length; argumentId++) {
            argumentClasses[argumentId] = arguments[argumentId].getClass();
        }/*w ww.j  a v  a  2  s  .  c om*/
        Method field = clazz.getDeclaredMethod(name, argumentClasses);
        boolean accessable = field.isAccessible();
        if (!accessable)
            field.setAccessible(true);
        Object value = field.invoke(object, arguments);
        if (!accessable)
            field.setAccessible(false);
        return value;
    }
}

Related

  1. invokeMethod(Object object, String methodName, List argList)
  2. invokeMethod(Object object, String methodName, Object arg, Class argType)
  3. invokeMethod(Object object, String methodName, Object... parameters)
  4. invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
  5. invokeMethod(Object object, String methodName, Object[] params, Object[] result)
  6. invokeMethod(Object object, String propertyName)
  7. invokeMethod(Object objectInstance, String methodToInvoke, Class[] parameterTypes, Object[] instanceParameters)
  8. invokeMethod(Object owner, String methodName)
  9. invokeMethod(Object owner, String methodName, Object[] args)