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

Objectinvoke(Object obj, String methodName, Object... params)
invoke
if (params.length % 2 != 0) {
    throw new RuntimeException("types and values mismatch");
try {
    int size = params.length / 2;
    Class<?>[] types = new Class<?>[size];
    Object[] args = new Object[size];
    for (int i = 0; i < types.length; i++) {
...
Objectinvoke(Object obj, String methodName, Object[] params)
invoke
try {
    Method[] methods = obj.getClass().getMethods();
    Method method = getMethod(methods, methodName);
    Object result = null;
    if (method != null) {
        result = method.invoke(obj, params);
    } else {
        throw new IllegalAccessException("Not exist the method. Method Name: [" + methodName + "]");
...
Objectinvoke(Object obj, String name, Object... args)
invoke
Method method = null;
Class<?> clazz = obj.getClass();
for (Method met : clazz.getMethods()) {
    if (name.equals(met.getName())) {
        method = met;
        break;
try {
    if (method != null) {
        if (args.length == 0) {
            return method.invoke(obj);
        } else {
            return method.invoke(obj, args);
} catch (InvocationTargetException e) {
    return e.getCause();
} catch (Throwable e) {
    return e;
return null;
Objectinvoke(Object object, Method method)
This simpy calls Method#invoke(Object,Object) .
try {
    return method.invoke(object);
} catch (RuntimeException e) {
    return null;
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
} catch (InvocationTargetException e) {
    return null;
...
Objectinvoke(Object object, Object[] args, String methodName)
invoke
Method method = object.getClass().getMethod(methodName, new Class[] {});
return method.invoke(object, args);
Objectinvoke(Object object, String function, Object... params)
Dynamically invokes the specified function on an object with the given parameters.
Class<? extends Object> type = object.getClass();
try {
    Map<Method, Integer> candidateDistances = getCompatibleMethods(type, function, params);
    if (candidateDistances.isEmpty())
        throw new IllegalArgumentException(
                String.format("no suitable method found in %s for name %s and parameters %s", type,
                        function, Arrays.toString(params)));
    if (candidateDistances.size() == 1)
...
Objectinvoke(Object object, String function, String parameter)
Invoke function of object by String.
System.out.println(function + "(" + (null == parameter ? "" : parameter) + ")");
Class<?>[] types = null;
Object[] values = null;
if (null == parameter) {
    types = new Class[] {};
    values = new Object[] {};
} else {
    String[] parameters = parameter.split(",");
...
Tinvoke(Object object, String method, Object... args)
invoke
try {
    Class clazz = object.getClass();
    Method m = clazz.getMethod(method, getClassTypes(args));
    return (T) m.invoke(object, args);
} catch (Exception e) {
    throw new RuntimeException(e);
Objectinvoke(Object object, String methodName)
invoke
Class<?> objectClass = object.getClass();
Method method;
try {
    method = objectClass.getMethod(methodName, new Class<?>[] {});
} catch (NoSuchMethodException e) {
    method = objectClass.getDeclaredMethod(methodName, new Class<?>[] {});
    method.setAccessible(true);
return method.invoke(object, new Object[] {});
Objectinvoke(Object object, String methodName, Class[] argTypes, Object... args)
invoke
try {
    Method onLayout = object.getClass().getDeclaredMethod(methodName, argTypes);
    onLayout.setAccessible(true);
    return onLayout.invoke(object, args);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
} catch (InvocationTargetException e) {
    throw new RuntimeException(e);
...