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

ObjectinvokeExactField(Object object, String fieldName)

Retrieve a field whose value is returned as an object.

Field field = object.getClass().getDeclaredField(fieldName);
if (field == null)
    throw new NoSuchFieldException(
            "No such accessible field: " + fieldName + " on object: " + object.getClass().getName());
return field.get(object);
ObjectinvokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)

Invoke a method whose parameter types match exactly the parameter types given.

if (args == null) {
    args = emptyObjectArray;
if (parameterTypes == null) {
    parameterTypes = emptyClassArray;
Method method = getAccessibleMethod(object.getClass(), methodName, parameterTypes);
if (method == null)
...
ObjectinvokeFunction(Object iFunction, String funcName, String param)
invoke Function
Method m = null;
try {
    m = iFunction.getClass().getMethod(funcName, new Class[] { String.class });
} catch (SecurityException e) {
    e.printStackTrace();
    return null;
} catch (NoSuchMethodException e) {
    e.printStackTrace();
...
ObjectinvokeFunctions(Object iFunction, String funcName)
invoke Functions
Method m = null;
try {
    m = iFunction.getClass().getMethod(funcName, new Class[] {});
} catch (SecurityException e) {
    e.printStackTrace();
} catch (NoSuchMethodException e) {
    e.printStackTrace();
Object retObj = null;
try {
    retObj = m.invoke(iFunction, new Object[] {});
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
return retObj;
ObjectinvokeGenericMethodOneArg(final Object obj, final String methodName, final Object arg)
Dirty method to call a declared method with a generic parameter type.
final Class<?> objClass = obj.getClass();
final Class<?> argClass = arg.getClass();
Method methodFound = null;
boolean denyObject = false;
for (final Method method : objClass.getDeclaredMethods()) {
    if (method.getName().equals(methodName)) {
        final Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 1) {
...
ObjectinvokeGet(Object o, String fieldName)
invoke Get
Method method = getGetMethod(o.getClass(), fieldName);
try {
    return method.invoke(o, new Object[0]);
} catch (Exception e) {
    e.printStackTrace();
return null;
StringinvokeGetClasspathMethodIfItExists(ClassLoader cl)
invoke Get Classpath Method If It Exists
Class<?> clazz = cl.getClass();
try {
    Method m = clazz.getMethod("getClasspath");
    Object returnValue = m.invoke(cl);
    if (returnValue instanceof String) {
        return (String) returnValue;
    } else {
        return null;
...
ObjectinvokeGetMethodWithSameName(Object object, Method method)
invoke Get Method With Same Name
String methodName = "get" + method.getName().substring(3);
Method getMethod = object.getClass().getMethod(methodName, new Class[0]);
return getMethod.invoke(object, new Object[0]);
MethodinvokeHeritedMethod(Object target, String method, Class klass)
invoke Herited Method
if (klass == null) {
    throw new NoSuchMethodException();
Method m;
try {
    m = klass.getDeclaredMethod(method);
} catch (NoSuchMethodException e) {
    return invokeHeritedMethod(target, method, klass.getSuperclass());
...
ObjectinvokeInstanceMethod(Object target, String methodName, Class type, Object arg)
Invoke method.
return invokeInstanceMethod(target, methodName, new Class[] { type }, new Object[] { arg });