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

ObjectinvokeMethodOnObject(Object target, String methodName)
Invoke an arbitrary method that takes no arguments.
try {
    Class<?> cls = target.getClass();
    while (cls != null) {
        Method[] allMethods = cls.getDeclaredMethods();
        for (Method meth : allMethods) {
            if (meth.getName().equals(methodName) && meth.getParameterTypes().length == 0) {
                meth.setAccessible(true);
                return meth.invoke(target, (Object[]) null);
...
ObjectinvokeMethodQuietly(Object receiver, Method method, Object... args)
invoke Method Quietly
try {
    return method.invoke(receiver, args);
} catch (IllegalAccessException ex) {
    return null;
} catch (InvocationTargetException ex) {
    return null;
ListinvokeMethods(final Object target, final List methods)
Invoke the following list of methods (with no parameter) and return the result in a List .
final List<Object> results = new ArrayList<>(methods.size());
for (final Method method : methods) {
    results.add(method.invoke(target, (Object[]) null));
return results;
voidinvokeMethodsWithAnnotation(final Class annotation, final Object instance, final Object... args)
Invokes all methods on the given instance that have been annotated with the given Annotation.
for (final Method method : instance.getClass().getMethods()) {
    if (method.isAnnotationPresent(annotation)) {
        final boolean isAccessible = method.isAccessible();
        method.setAccessible(true);
        try {
            final Class<?>[] argumentTypes = method.getParameterTypes();
            if (argumentTypes.length > args.length) {
                throw new IllegalArgumentException(String.format(
...
ObjectinvokeMethodWithArray2(Object target, Method method, Object[] args)
invoke Method With Array
try {
    return method.invoke(target, args);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
ObjectinvokeMethodWithKnownParamTypes(Object object, String methodName, Class[] methodParamTypes, Object... args)
invoke Method With Known Param Types
Throwable t = null;
try {
    Class clazz = object.getClass();
    Method setter = clazz.getMethod(methodName, methodParamTypes);
    return setter.invoke(object, args);
} catch (NoSuchMethodException e) {
    t = e;
    e.printStackTrace();
...
ObjectinvokeMethodWithNoArgs(Object o, Method method)
invoke Method With No Args
try {
    return method.invoke(o);
} catch (InvocationTargetException | IllegalAccessException e) {
    return null;
booleaninvokeMethodWithoutException(Object object, String methodName, Class[] parameterTypes, Object[] parameters)
Invoke a method.
try {
    invokeMethod(object, methodName, parameterTypes, parameters);
    return true;
} catch (SecurityException e) {
} catch (IllegalArgumentException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
...