Java Method Call invoke(Class clazz, String methodName, Object instance, Class[] signature, Object... args)

Here you can find the source of invoke(Class clazz, String methodName, Object instance, Class[] signature, Object... args)

Description

Reflectively invokes a method

License

Open Source License

Parameter

Parameter Description
clazz The class defining the method to invoke
methodName The method to invoke
instance The object instance to invoke on
signature The method signature
args The method invocation arguments

Return

the return value of the invocation

Declaration

public static Object invoke(Class<?> clazz, String methodName, Object instance, Class<?>[] signature,
        Object... args) 

Method Source Code

//package com.java2s;
/**//w ww  .  j  a  v  a2s  .c o  m
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2014, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import java.lang.reflect.Method;

public class Main {
    /** A no param class signature const */
    public static final Class<?>[] NO_ARG_SIG = {};
    /** A no arg object invocation const */
    public static final Object[] NO_ARG_PARAM = {};

    /**
     * Reflectively invokes a method 
     * @param clazz The class defining the method to invoke
     * @param methodName The method to invoke
     * @param instance The object instance to invoke on
     * @param signature The method signature
     * @param args The method invocation arguments
     * @return the return value of the invocation
     */
    public static Object invoke(Class<?> clazz, String methodName, Object instance, Class<?>[] signature,
            Object... args) {
        boolean mod = false;
        Method m = null;
        try {
            try {
                m = clazz.getDeclaredMethod(methodName, signature == null ? NO_ARG_SIG : signature);
            } catch (NoSuchMethodException e) {
                m = clazz.getMethod(methodName, signature == null ? NO_ARG_SIG : signature);
            }
            if (!m.isAccessible()) {
                m.setAccessible(true);
                mod = true;
            }
            return m.invoke(instance, args);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            if (mod && m != null) {
                m.setAccessible(false);
            }
        }
    }

    /**
     * Reflectively invokes a static method 
     * @param clazz The class defining the method to invoke
     * @param methodName The method to invoke
     * @param signature The method signature
     * @param args The method invocation arguments
     * @return the return value of the invocation
     */
    public static Object invoke(Class<?> clazz, String methodName, Class<?>[] signature, Object... args) {
        return invoke(clazz, methodName, signature, args);
    }

    /**
     * Reflectively invokes a static and parameterless method 
     * @param clazz The class defining the method to invoke
     * @param methodName The method to invoke
     * @return the return value of the invocation
     */
    public static Object invoke(Class<?> clazz, String methodName) {
        return invoke(clazz, methodName, null, null, NO_ARG_PARAM);
    }

    /**
     * Reflectively invokes a parameterless method 
     * @param methodName The method to invoke
     * @param instance The object instance to invoke on
     * @return the return value of the invocation
     */
    public static Object invoke(Object instance, String methodName) {
        return invoke(instance.getClass(), methodName, instance, null, NO_ARG_PARAM);
    }

    /**
     * Reflectively invokes a parameterized method 
     * @param methodName The method to invoke
     * @param instance The object instance to invoke on
     * @param signature The method signature
     * @param args The method arguments
     * @return the return value of the invocation
     */
    public static Object invoke(Object instance, String methodName, Class<?>[] signature, Object... args) {
        return invoke(instance.getClass(), methodName, instance, signature, args);
    }
}

Related

  1. invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  2. invoke(Class targetClass, Object target, String methodName, Class paramType0, Object paramValue0)
  3. invoke(Class annotationClass, Object instance, Object[] parameters)
  4. invoke(Class clazz, Object obj, String methodName, Object... args)
  5. invoke(Class clazz, String methodName, Class[] parameterTypes, Object instance, Object[] args, Class returnType)
  6. invoke(Class cls, String methodName, Object... parameter)
  7. invoke(Class returnType, Method m, Object obj, Object... args)
  8. invoke(E e, String methodName)
  9. invoke(final Method m, final Object obj, final Object... args)