Java Utililty Methods Reflection Method Invoke

List of utility methods to do Reflection Method Invoke

Description

The list of methods to do Reflection Method Invoke are organized into topic(s).

Method

ObjectinvokeMethod(Object obj, String method, Object... args)
invoke Method
Class<?>[] parameterTypes = new Class<?>[args.length];
if (args.length > 0)
    for (int i = 0; i < args.length; i++)
        parameterTypes[i] = args[i].getClass();
Method m = obj.getClass().getMethod(method, parameterTypes);
if (!m.isAccessible())
    m.setAccessible(true);
return m.invoke(obj, args);
...
voidinvokeMethod(Object obj, String methodName, Object... args)
Invokes a method (even private methods).
try {
    Class[] arglist = null;
    if (args != null) {
        arglist = new Class[args.length];
        for (int i = 0; i < arglist.length; i++) {
            arglist[i] = args[i].getClass();
    Method m = obj.getClass().getDeclaredMethod(methodName, arglist);
    AccessibleObject[] ao = { m };
    AccessibleObject.setAccessible(ao, true);
    m.invoke(obj, args);
} catch (Exception ex) {
    ex.printStackTrace();
ObjectinvokeMethod(Object obj, String methodName, Object... args)
invoke Method
try {
    Method m;
    if (args != null) {
        Class<?>[] argClasses = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++)
            argClasses[i] = args[i].getClass();
        m = obj.getClass().getMethod(methodName, argClasses);
        return m.invoke(obj, args);
...
voidinvokeMethod(Object obj, String methodname, Object... args)
invoke Method
Class<?> clazz = obj.getClass();
do {
    try {
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getName().equals(methodname) && method.getParameterTypes().length == args.length) {
                method.setAccessible(true);
                method.invoke(obj, args);
    } catch (Throwable t) {
} while ((clazz = clazz.getSuperclass()) != null);
ObjectinvokeMethod(Object obj, String methodName, Object... params)
Get and invoke the target method from the given object with given parameters
Method m;
try {
    m = obj.getClass().getMethod(methodName, getParameterTypes(params));
    m.setAccessible(true);
    return m.invoke(obj, params);
} catch (NoSuchMethodException e) {
    throw new UnsupportedOperationException("Cannot find specified method " + methodName, e);
} catch (IllegalAccessException e) {
...
ObjectinvokeMethod(Object obj, String methodName, Object[] params, Class[] paramTypes)
invoke Method
Class clazz;
if (obj instanceof Class) {
    clazz = (Class) obj;
} else {
    clazz = obj.getClass();
try {
    Method method = clazz.getDeclaredMethod(methodName, paramTypes);
...
ObjectinvokeMethod(Object object, Method method, Object... args)
invoke Method
method.setAccessible(true);
return method.invoke(object, args);
ObjectinvokeMethod(Object object, String methodName, Class[] parameterTypes, Object[] parameters)
Invoke a method.
Method method = object.getClass().getMethod(methodName, parameterTypes);
return method.invoke(object, parameters);
ObjectinvokeMethod(Object object, String methodName, Class[] signature, Object[] parameters)
invoke Method
Class<?> clazz = object.getClass();
try {
    Method method = clazz.getDeclaredMethod(methodName, signature);
    return method.invoke(object, parameters);
} catch (Exception e) {
    e.printStackTrace();
return null;
...
ObjectinvokeMethod(Object object, String methodName, List argList)
invoke Method
Method method = findMethod(object, methodName, argList);
if (method == null) {
    throw new NoSuchMethodException(
            "No method " + methodName + " for arguments " + debugDumpArgList(argList) + " in " + object);
Object[] args = argList.toArray();
if (method.isVarArgs()) {
    Class<?> parameterTypeClass = method.getParameterTypes()[0];
...