Java Reflection Method Invoke invokeMethod(final Method method, final Object object)

Here you can find the source of invokeMethod(final Method method, final Object object)

Description

Invoke the givenMethod on a givenObject.

License

Open Source License

Declaration

public static Object invokeMethod(final Method method, final Object object)
        throws IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution./*w  w  w  .  j  av  a 2s  . c  om*/
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 *     08/23/2010-2.2 Michael O'Brien 
 *        - 323043: application.xml module ordering may cause weaving not to occur causing an NPE.
 *                       warn if expected "_persistence_*_vh" method not found
 *                       instead of throwing NPE during deploy validation.
 *     30/05/2012-2.4 Guy Pelletier
 *       - 354678: Temp classloader is still being used during metadata processing
 *
 ******************************************************************************/

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

public class Main {
    /**
     * Invoke the givenMethod on a givenObject. Assumes method does not take
     * parameters. Wrap in a doPrivileged block if necessary.
     */
    public static Object invokeMethod(final Method method, final Object object)
            throws IllegalAccessException, InvocationTargetException {
        return invokeMethod(method, object, (Object[]) null);
    }

    /**
     * Invoke the givenMethod on a givenObject using the array of parameters given.  Wrap in a doPrivileged block
     * if necessary.
     */
    public static Object invokeMethod(final Method method, final Object object, final Object[] parameters)
            throws IllegalAccessException, InvocationTargetException {
        // Ensure the method is accessible.
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        return method.invoke(object, parameters);
    }
}

Related

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