Java Reflection Method Invoke invoke(Method method, Object data, Object... arguments)

Here you can find the source of invoke(Method method, Object data, Object... arguments)

Description

Common method to invoke Method with reflection

License

Open Source License

Parameter

Parameter Description
method a parameter
data a parameter
arguments a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static Object invoke(Method method, Object data, Object... arguments) throws IOException 

Method Source Code


//package com.java2s;
/*// w w  w  .java2 s .c om
 * Lightmare, Lightweight embedded EJB container (works for stateless session beans) with JPA / Hibernate support
 *
 * Copyright (c) 2013, Levan Tsinadze, or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Main {
    /**
     * Common method to invoke {@link Method} with reflection
     * 
     * @param method
     * @param data
     * @param arguments
     * @return {@link Object}
     * @throws IOException
     */
    public static Object invoke(Method method, Object data, Object... arguments) throws IOException {

        Object value;

        try {
            value = method.invoke(data, arguments);
        } catch (IllegalAccessException ex) {
            throw new IOException(ex);
        } catch (IllegalArgumentException ex) {
            throw new IOException(ex);
        } catch (InvocationTargetException ex) {
            throw unwrap(ex);
        }

        return value;
    }

    /**
     * Gets target {@link Throwable} from passed
     * {@link InvocationTargetException} instance
     * 
     * @param ex
     * @return {@link IOException}
     */
    private static IOException unwrap(InvocationTargetException ex) {

        IOException exception;

        Throwable targetException = ex.getTargetException();
        if (targetException == null) {
            exception = new IOException(ex.getMessage(), ex);
        } else {
            exception = new IOException(targetException.getMessage(), targetException);
        }

        return exception;
    }
}

Related

  1. invoke(Method m, Object o, Object[] args)
  2. invoke(Method m, Object o, String msg, Object... parameters)
  3. invoke(Method m, Object target, Object... params)
  4. invoke(Method method)
  5. invoke(Method method)
  6. invoke(Method method, Object instance, Object... parameters)
  7. invoke(Method method, Object it, Object... args)
  8. invoke(Method method, Object javaBean, Object value)
  9. invoke(Method method, Object obj, Object... args)