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

TinvokeMethod(java.lang.Object toObj, String tcMethodName, Class toResultClass)
invoke Method
try {
    Method loMethod = toObj.getClass().getDeclaredMethod(tcMethodName);
    loMethod.setAccessible(true);
    return (T) loMethod.invoke(toObj);
} catch (NoSuchMethodException ex) {
    throw new RuntimeException(ex);
} catch (SecurityException ex) {
    throw new RuntimeException(ex);
...
ObjectinvokeMethod(Method inMethod, Object inObject, Object[] inArgs)
invoke Method
final boolean isAccessible = inMethod.isAccessible();
if (!isAccessible) {
    inMethod.setAccessible(true);
try {
    return inMethod.invoke(inObject, inArgs);
} finally {
ObjectinvokeMethod(Method m, Object instance, Object[] args)
invoke Method
try {
    return m.invoke(instance, args);
} catch (Throwable t) {
    throw new IllegalStateException(t);
ObjectinvokeMethod(Method m, Object[] o)
attempt to invoke a Method with the given Object arguments, performing static method auto-detection and automatic array compression
Object obj;
Object[] args;
Class[] c = m.getParameterTypes();
int num = (o == null) ? 0 : o.length;
int len = -1;
int a = -1;
if (c != null) {
    len = c.length;
...
ObjectinvokeMethod(Method meth, String str)
invoke Method
try {
    return meth.invoke(null, str);
} catch (Throwable t) {
    System.err.println("Error in method invocation with argument " + str + " : " + t.getMessage());
    return null;
ObjectinvokeMethod(Method method, Class beanClass, Object element)
invoke Method
if (method != null && instanceOf(beanClass, element)) {
    try {
        return method.invoke(element, EMPTY_ARRAY);
    } catch (Exception e) {
        throw new RuntimeException(e);
return null;
...
ObjectinvokeMethod(Method method, Object bean, Object[] values)
This just catches and wraps IllegalArgumentException.
try {
    return method.invoke(bean, values);
} catch (IllegalArgumentException e) {
    throw new IllegalArgumentException("Cannot invoke " + method.getDeclaringClass().getName() + "."
            + method.getName() + " - " + e.getMessage());
ObjectinvokeMethod(Method method, Object instance, Object... parameters)
A helper method to invoke a method via reflection and wrap any exceptions as RuntimeException instances
if (method == null) {
    return null;
try {
    return method.invoke(instance, parameters);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
} catch (InvocationTargetException e) {
...
ObjectinvokeMethod(Method method, Object object)
invoke Method
return method.invoke(object, EMPTY_PARAMS);
ObjectinvokeMethod(Method method, Object object, Object... args)
invoke Method
if (args == null || args.length == 0) {
    return method.invoke(object);
List<Object> invokeArgs = new ArrayList<>();
for (Class<?> typeClass : method.getParameterTypes()) {
    for (Object arg : args) {
        if (arg == null) {
            invokeArgs.add(null);
...