Java Method Call invoke(Object _o, String _method, Object... _args)

Here you can find the source of invoke(Object _o, String _method, Object... _args)

Description

Invokes a method.

License

Open Source License

Exception

Parameter Description
NoSuchMethodException if the method is not found
InvocationTargetException target is not defined
IllegalAccessException if the method is not private
IllegalArgumentException an exception

Declaration

@SuppressWarnings("unchecked")
public static <T extends Object> T invoke(Object _o, String _method, Object... _args)
        throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 <a href="mailto:daniel.williams@gmail.com">Daniel Williams</a>.
 * All rights reserved. This program, solace.common, and file, ReflectionUtil.java, and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html//from  www .  java2 s  .  c  o  m
 * 
 * Contributors:
 *     <a href="mailto:daniel.williams@gmail.com">Daniel Williams</a> - initial API and implementation
 ******************************************************************************/

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

public class Main {
    /**
     * Invokes a method. Pass in java.lang.Void if it is not to return anything.
     * 
     * @throws NoSuchMethodException
     *             if the method is not found
     * @throws InvocationTargetException
     *             target is not defined
     * @throws IllegalAccessException
     *             if the method is not private
     * @throws IllegalArgumentException
     */
    @SuppressWarnings("unchecked")
    public static <T extends Object> T invoke(Object _o, String _method, Object... _args)
            throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {

        ArrayList<Class<?>> inputs = new ArrayList<Class<?>>();
        for (Object o : _args)
            inputs.add(o.getClass());
        Method m = _o.getClass().getMethod(_method, inputs.toArray(new Class<?>[0]));

        return (T) m.invoke(_o, _args);
    }
}

Related

  1. invoke(final Object instance, final String method, final Object... parameters)
  2. invoke(final Object instance, final String methodName, final Class[] methodParams, final Object[] args)
  3. invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues)
  4. invoke(final Object target, final Method method, final Object... parameters)
  5. invoke(final Object target, final Method method, final Object[] params)
  6. invoke(Object bean, Method method, Object value)
  7. invoke(Object bean, Method method, Object value)
  8. invoke(Object bean, String methodName, Object args, Class parameterType)
  9. invoke(Object buffer, String methodName, Class[] paramTypes, Object... args)