Here you can find the source of invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)
Parameter | Description |
---|---|
method | the Method to invoke |
objectToInvokeOn | the Object to invoke the Method on |
args | the arguments to the Method |
Parameter | Description |
---|---|
Throwable | an exception |
static Object invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args) throws IllegalAccessException, Throwable
//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(); } } }