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

Objectinvoke(Method method, Object obj, Object... args)
Invoke the method event if not accessible.
boolean accessible = method.isAccessible();
if (accessible) {
    return method.invoke(obj, args);
method.setAccessible(true);
try {
    return method.invoke(obj, args);
} finally {
...
Objectinvoke(Method method, Object object, Object... args)
Invoke.
try {
    return method.invoke(object, args);
} catch (final InvocationTargetException e) {
    if (e.getCause() instanceof Exception) {
        throw (Exception) e.getCause();
    } else if (e.getCause() instanceof Error) {
        throw (Error) e.getCause();
    } else {
...
Objectinvoke(Method method, Object object, Object... arguments)
invoke
Object result = null;
try {
    if (!method.isAccessible()) {
        method.setAccessible(true);
    result = method.invoke(object, arguments);
} catch (Exception ignored) {
    System.err.printf("%s method[arguments : %s] can't be invoked in object[%s]!\n", method.getName(),
...
Objectinvoke(Method method, Object object, Object... arguments)
invoke
return execute(() -> {
    method.setAccessible(true);
    return method.invoke(object, arguments);
});
Tinvoke(Method method, Object object, Object... parameters)
Invoke a method on an object and return whatever it returns.
try {
    return (T) method.invoke(object, parameters);
} catch (IllegalArgumentException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
} catch (InvocationTargetException e) {
    if (e.getTargetException() instanceof IOException)
...
Objectinvoke(Method method, Object object, Object[] args, Class exceptionToThrow)
invoke
try {
    return method.invoke(object, args);
} catch (IllegalAccessException e) {
    throw handleException(exceptionToThrow, e);
} catch (InvocationTargetException e) {
    throw handleException(exceptionToThrow, e);
Tinvoke(Method method, Object target, Object... args)
invoke
try {
    return (T) method.invoke(target, args);
} catch (Exception ex) {
    throw new IllegalArgumentException("Cannot invoke method " + method, ex);
Objectinvoke(Method method, Object target, Object... arguments)
Invoke a method of the provided target with the provided arguments.
try {
    return method.invoke(target, arguments);
} catch (Exception e) {
    throw new RuntimeException(e);
Objectinvoke(Method method, T instance, Object... params)
invoke
try {
    if (params == null) {
        return method.invoke(instance);
    } else {
        return method.invoke(instance, params);
} catch (Exception e) {
    return null;
...
Tinvoke(MethodHandle methodHandle, Object... params)
Wraps any non-runtime exceptions with a runtime exception
try {
    return (T) methodHandle.invokeWithArguments(params);
} catch (RuntimeException e) {
    throw e;
} catch (Throwable throwable) {
    throw new RuntimeException(throwable);