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

ObjectinvokeMethodAndGetRecursively(Object object, String methodName, Object... args)
Invokes and returns result of method with given name and arguments of target object's class or one of it's superclasses.
Class clazz = object.getClass();
boolean ended = false;
do {
    for (Method m : clazz.getDeclaredMethods())
        if (m.getName().equalsIgnoreCase(methodName)) {
            ended = true;
            m.setAccessible(true);
            try {
...
ObjectinvokeMethodAndReturn(Class clazz, String method, Object object)
invoke Method And Return
Object value = null;
try {
    Method methodToInvoke = getMethod(clazz, method);
    methodToInvoke.setAccessible(true);
    value = methodToInvoke.invoke(object);
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
...
voidinvokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)
Do method from superclass by name.
Method method = object.getClass().getDeclaredMethod(name, typeArg);
method.setAccessible(true);
method.invoke(object, arg);
ObjectinvokeMethodByName(final Object obj, final String methodName, final Object[] args)
invoke Method By Name
Method method = getAccessibleMethodByName(obj, methodName);
if (method == null) {
    throw new IllegalArgumentException(
            "Could not find MethodDefine [" + methodName + "] on target [" + obj + "]");
try {
    return method.invoke(obj, args);
} catch (Exception e) {
...
ObjectinvokeMethodHandleException(Method method, Object... args)
Invokes given method with given arguments.
try {
    method.setAccessible(true);
    if (Modifier.isStatic(method.getModifiers())) {
        return method.invoke(null, args);
    } else {
        Object this_ = args[0];
        Object[] newArgs = new Object[args.length - 1];
        System.arraycopy(args, 1, newArgs, 0, args.length - 1);
...
ObjectinvokeMethodNoArgs(final Object obj, final String methodName, final Class... returnTypePreference)
Invoke a method without arguments, get the method matching the return types best, i.e.
final Class<?> objClass = obj.getClass();
Method methodFound = getMethodNoArgs(objClass, methodName, returnTypePreference);
if (methodFound == null) {
    methodFound = seekMethodNoArgs(objClass, methodName, returnTypePreference);
if (methodFound != null) {
    try {
        final Object res = methodFound.invoke(obj);
...
ObjectinvokeMethodNoArgs(Object target, String methodName)
invoke Method No Args
try {
    Method method = target.getClass().getDeclaredMethod(methodName);
    method.setAccessible(true);
    Object result = method.invoke(target);
    return result;
} catch (Exception e) {
    throw new RuntimeException(e);
ObjectinvokeMethodOfClass(Class target, Method method)
invoke Method Of Class
return invokeMethod(target.newInstance(), method, null);
voidinvokeMethodOnLoader(ClassLoader cl, String methodName, Object... params)
invoke Method On Loader
Class<?>[] paramClasses = new Class<?>[params.length];
for (int i = 0; i != params.length; i++) {
    paramClasses[i] = params[i].getClass();
Method method = cl.getClass().getMethod(methodName, paramClasses);
method.invoke(cl, params);
ObjectinvokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)
This method uses reflection to invoke the given Method on the passed in Object instance.
try {
    return method.invoke(objectToInvokeOn, args);
} catch (InvocationTargetException invocationException) {
    throw invocationException.getTargetException();