Java Reflection Method Invoke invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)

Here you can find the source of invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)

Description

This method uses reflection to invoke the given Method on the passed in Object instance.

License

Open Source License

Parameter

Parameter Description
method the Method to invoke
objectToInvokeOn the Object to invoke the Method on
args the arguments to the Method

Exception

Parameter Description
Throwable an exception

Return

an Object representing the result of the Method call

Declaration

static Object invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)
        throws IllegalAccessException, Throwable 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010, 2015 Oracle./*from w w w .j ava  2  s. c o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 * and the Apache License v2.0 is available at 
 *     http://www.opensource.org/licenses/apache2.0.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Bob Nettleton (Oracle) - Initial Reference Implementation
 ******************************************************************************/

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

public class Main {
    /**
     * This method uses reflection to invoke the given Method
     * on the passed in Object instance.  This method also
     * catches the InvocationTargeException, in order to always
     * re-throw the original exception from the Method invocation. 
     * 
     * @param method the Method to invoke
     * @param objectToInvokeOn the Object to invoke the Method on
     * @param args the arguments to the Method
     * @return an Object representing the result of the Method call
     * @throws Throwable
     */
    static Object invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)
            throws IllegalAccessException, Throwable {
        try {
            return method.invoke(objectToInvokeOn, args);
        } catch (InvocationTargetException invocationException) {
            throw invocationException.getTargetException();
        }
    }
}

Related

  1. invokeMethodHandleException(Method method, Object... args)
  2. invokeMethodNoArgs(final Object obj, final String methodName, final Class... returnTypePreference)
  3. invokeMethodNoArgs(Object target, String methodName)
  4. invokeMethodOfClass(Class target, Method method)
  5. invokeMethodOnLoader(ClassLoader cl, String methodName, Object... params)
  6. invokeMethodOnObject(Object target, String methodName)
  7. invokeMethodQuietly(Object receiver, Method method, Object... args)
  8. invokeMethods(final Object target, final List methods)
  9. invokeMethodsWithAnnotation(final Class annotation, final Object instance, final Object... args)