Here you can find the source of invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
Parameter | Description |
---|---|
c | class that contains method |
obj | object to execute |
method | method to invoke |
paramClasses | classes of parameters |
params | parameters |
public static Object invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
//package com.java2s; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /**/*from w w w .ja v a 2s . com*/ * Invokes accessible method of an object. * * @param c class that contains method * @param obj object to execute * @param method method to invoke * @param paramClasses classes of parameters * @param params parameters */ public static Object invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method m = c.getMethod(method, paramClasses); return m.invoke(obj, params); } /** * Invokes static method. */ public static Object invoke(Class c, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method m = c.getMethod(method, paramClasses); return m.invoke(null, params); } /** * Invokes accessible method of an object. * * @param obj object * @param method name of the objects method * @param params method parameters * @param paramClasses method parameter types */ public static Object invoke(Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method m = obj.getClass().getMethod(method, paramClasses); return m.invoke(obj, params); } /** * Invokes accessible method of an object without specifying parameter types. * @param obj object * @param method method of an object * @param params method parameters */ public static Object invoke(Object obj, String method, Object... params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class[] paramClass = getClasses(params); return invoke(obj, method, paramClass, params); } public static Object invoke(Class c, Object obj, String method, Object... params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class[] paramClass = getClasses(params); return invoke(c, obj, method, paramClass, params); } /** * Invokes static method. */ public static Object invoke(Class c, String method, Object... params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class[] paramClass = getClasses(params); return invoke(c, null, method, paramClass, params); } /** * Returns classes from array of specified objects. */ public static Class[] getClasses(Object... objects) { if (objects == null) { return null; } Class[] result = new Class[objects.length]; for (int i = 0; i < objects.length; i++) { if (objects[i] != null) { result[i] = objects[i].getClass(); } } return result; } }