Java Utililty Methods Method Call

List of utility methods to do Method Call

Description

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

Method

Tinvoke(@Nonnull Object source, @Nonnull String name, Object... args)
invoke
Class<?>[] argTypes = new Class<?>[args.length];
for (int i = 0; i < args.length; i++) {
    argTypes[i] = args[i] == null ? Object.class : args[i].getClass();
final Method method = findMethod(source.getClass(), name, argTypes);
if (method == null) {
    throw new IllegalArgumentException(String.format("Unable to find '%s' with '%s' on '%s'!", name,
            Arrays.toString(argTypes), source.getClass()));
...
RTinvoke(boolean makeAccessible, T object, Class methodClass, String methodName, Class[] paramTypes, Object... params)
Invokes a method by means of reflection.
try {
    if (object == null)
        throw new NullPointerException("Cannot invoke the method '" + methodName + "' over a null object");
    Method setPropM = methodClass.getDeclaredMethod(methodName, paramTypes);
    if (makeAccessible)
        setPropM.setAccessible(true);
    return (RT) setPropM.invoke(object, params);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
...
Objectinvoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
Invokes accessible method of an object.
Method m = c.getMethod(method, paramClasses);
return m.invoke(obj, params);
Objectinvoke(Class targetClass, Object target, String methodName, Class paramType0, Object paramValue0)
call method by reflection this method allows to call protected and private methods; use it with care...
Class[] paramTypes = new Class[1];
Object[] paramValues = new Object[1];
paramTypes[0] = paramType0;
paramValues[0] = paramValue0;
return invoke(targetClass, target, methodName, paramTypes, paramValues);
voidinvoke(Class annotationClass, Object instance, Object[] parameters)
Invokes the method of the object instance annotated with the specified annotation.
Class<?> instanceClass = instance.getClass();
Method[] methods = instanceClass.getMethods();
for (Method method : methods) {
    if (method.isAnnotationPresent(annotationClass)) {
        method.setAccessible(true);
        try {
            method.invoke(instance, parameters);
        } catch (Exception e) {
...
Objectinvoke(Class clazz, Object obj, String methodName, Object... args)
invoke
try {
    Method method = findMethod(clazz, methodName);
    method.setAccessible(true);
    Object result = method.invoke(obj, args);
    return result;
} catch (Throwable e) {
    throw new RuntimeException("invoke", e);
Tinvoke(Class clazz, String methodName, Class[] parameterTypes, Object instance, Object[] args, Class returnType)
invoke
try {
    Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
    method.setAccessible(true);
    return returnType.cast(method.invoke(instance, args));
} catch (Exception e) {
    throw new IllegalStateException(e);
Objectinvoke(Class clazz, String methodName, Object instance, Class[] signature, Object... args)
Reflectively invokes a method
boolean mod = false;
Method m = null;
try {
    try {
        m = clazz.getDeclaredMethod(methodName, signature == null ? NO_ARG_SIG : signature);
    } catch (NoSuchMethodException e) {
        m = clazz.getMethod(methodName, signature == null ? NO_ARG_SIG : signature);
    if (!m.isAccessible()) {
        m.setAccessible(true);
        mod = true;
    return m.invoke(instance, args);
} catch (Exception ex) {
    throw new RuntimeException(ex);
} finally {
    if (mod && m != null) {
        m.setAccessible(false);
Objectinvoke(Class cls, String methodName, Object... parameter)
invoke
Class<?>[] parameterTypes = new Class<?>[parameter.length];
for (int i = 0; i < parameterTypes.length; i++) {
    parameterTypes[i] = parameter[i].getClass();
try {
    return getMethod(cls, methodName, parameterTypes).invoke(null, parameter);
} catch (Exception e) {
    e.printStackTrace();
...
Tinvoke(Class returnType, Method m, Object obj, Object... args)
Invokes the given method on the given object with the given arguments.
try {
    return (T) m.invoke(obj, args);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);