Java Reflection Method Invoke invokeMethod(final Object obj, final Method method, final Object... args)

Here you can find the source of invokeMethod(final Object obj, final Method method, final Object... args)

Description

Invoke the method with the given argument on the given object.

License

Open Source License

Parameter

Parameter Description
obj - given object
method - given method
args - given arguments

Exception

Parameter Description
RuntimeException if there is a problem while invoking the method.

Return

the result of the method.

Declaration

@SuppressWarnings("unchecked")
public static <T> T invokeMethod(final Object obj, final Method method, final Object... args) 

Method Source Code

//package com.java2s;
/************************************************************************
 * This file is part of AdminCmd.                           
 *                                                      
 * AdminCmd is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by   
 * the Free Software Foundation, either version 3 of the License, or      
 * (at your option) any later version.                           
 *                                                      
 * AdminCmd is distributed in the hope that it will be useful,   
 * but WITHOUT ANY WARRANTY; without even the implied warranty of      
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 * GNU General Public License for more details.                     
 *                                                      
 * You should have received a copy of the GNU General Public License
 * along with AdminCmd.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/

import java.lang.reflect.Method;

public class Main {
    /**//from  w w w.j  a v  a2s  .  co m
     * Invoke the method with the given argument on the given object. Works also
     * for private method.
     * 
     * @param obj
     *            - given object
     * @param method
     *            - given method
     * @param args
     *            - given arguments
     * @return the result of the method.
     * @throws RuntimeException
     *             if there is a problem while invoking the method.
     */
    @SuppressWarnings("unchecked")
    public static <T> T invokeMethod(final Object obj, final Method method, final Object... args) {
        T result = null;
        method.setAccessible(true);
        try {
            result = (T) method.invoke(obj, args);
        } catch (final Exception e) {
            throw new RuntimeException("Can't invoke method " + method + " from " + obj, e);
        } finally {
            method.setAccessible(false);
        }
        return result;
    }
}

Related

  1. invokeMethod(Class returnClass, String methodName, Object ivokeObject, Object... objects)
  2. invokeMethod(final Method method, final Object instance, final Object... args)
  3. invokeMethod(final Method method, final Object object)
  4. invokeMethod(final Method method, final Object object, final Object[] args)
  5. invokeMethod(final Object instance, final String methodName, final Class[] parTypes, final Object[] parameters)
  6. invokeMethod(final Object obj, final String methodName, final boolean throwException)
  7. invokeMethod(final Object object, final String methodName, final Class[] parameterTypes, final Object[] parameters)
  8. invokeMethod(final Object object, final String methodName, final Class[] paramTypes, final Object[] parameters)
  9. invokeMethod(final Object p, final String methodName)