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, Method method, Object[] params)
invoke
return method.invoke(obj, params);
Objectinvoke(Object obj, String method, Class[] params, Object[] args)

Invokes method of a given obj and set of parameters

try {
    return invoke_ex(obj, method, params, args);
} catch (Exception e) {
    throw new RuntimeException(e);
Objectinvoke(Object obj, String method, Class[] params, Object[] args)

Invokes method of a given obj and set of parameters

try {
    Class<?> clazz = null;
    if (obj instanceof Class) {
        clazz = (Class) obj;
    } else {
        clazz = obj.getClass();
    Method methods = clazz.getMethod(method, params);
...
Objectinvoke(Object obj, String method, Object param, Object paramType)
Invoke a method if it can be found!
Object result = null;
Object[] params = null;
Class[] paramTypes = null;
if (param.getClass().isArray()) {
    params = (Object[]) param;
    paramTypes = (Class[]) paramType;
} else {
    params = new Object[1];
...
Objectinvoke(Object obj, String methodName)
invoke
Class clazz = obj.getClass();
Method method = null;
try {
    method = clazz.getMethod(methodName);
} catch (Exception e) {
    throw new RuntimeException("Can not get the method[" + methodName + "] from the class["
            + clazz.getName() + "]! caused by: " + e.getMessage(), e);
try {
    return method.invoke(obj);
} catch (Exception e) {
    throw new RuntimeException("Unexpected exception occurs while invoking the method[" + methodName
            + "] in the class[" + clazz.getName() + "]! caused by: " + e.getMessage(), e);
Objectinvoke(Object obj, String methodName, boolean newValue)
invoke
try {
    Method method = obj.getClass().getMethod(methodName, new Class[] { Boolean.TYPE });
    return method.invoke(obj, new Object[] { new Boolean(newValue) });
} catch (IllegalAccessException e) {
    throw new NoSuchMethodException(methodName + " is not accessible");
} catch (InvocationTargetException e) {
    throw new InternalError(e.getMessage());
Objectinvoke(Object obj, String methodName, Class argType, Object arg)
Call a method on the object with one argument.
try {
    Method method;
    method = obj.getClass().getMethod(methodName, new Class[] { argType });
    return method.invoke(obj, new Object[] { arg });
} catch (Exception t) {
    throwBuildException(t);
    return null; 
Objectinvoke(Object obj, String methodName, Class clazz, Object newValue)
Invokes the specified setter method if it exists.
try {
    Method method = obj.getClass().getMethod(methodName, new Class[] { clazz });
    return method.invoke(obj, new Object[] { newValue });
} catch (IllegalAccessException e) {
    throw new NoSuchMethodException(methodName + " is not accessible");
} catch (InvocationTargetException e) {
    throw new InternalError(e.getMessage());
Objectinvoke(Object obj, String methodName, Class[] parameterTypes, Object[] args)
invoke
if (parameterTypes == null)
    parameterTypes = EMPTY_CLASSES;
Method method = obj.getClass().getMethod(methodName, parameterTypes);
if (args == null)
    args = EMPTY_PARAMS;
return method.invoke(obj, args);
Objectinvoke(Object obj, String methodName, Object... args)
This metho can help invoke the private method of the object, but it needs to cast the type of the result.
Class[] types = new Class[args.length];
Class tmp = null;
for (int i = 0; i < types.length; i++) {
    tmp = args[i].getClass();
    if (Proxy.class.isAssignableFrom(tmp)) {
        if (tmp.getInterfaces() == null || tmp.getInterfaces().length == 0) {
            if (!Proxy.class.isAssignableFrom(tmp.getSuperclass())) {
                tmp = tmp.getSuperclass();
...