Here you can find the source of invoke(Class> clazz, String methodName, Object instance, Class>[] signature, Object... args)
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 |
public static Object invoke(Class<?> clazz, String methodName, Object instance, Class<?>[] signature, Object... args)
//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); } }