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

TinvokeMethod(Object target, String name, Class... parameterTypes)
Safely invoke a method with the given signature (name + parameter types) on the given target object.
Method method = findMethod(target.getClass(), name, parameterTypes);
try {
    return (T) method.invoke(target, parameterTypes);
} catch (Exception e) {
    return null;
ObjectinvokeMethod(Object target, String name, Object[] args, Class[] argTypes)
invoke Method
Method m = findMethod(target.getClass(), name, argTypes);
if (m != null) {
    try {
        Object result = invokeMethod(m, target, castArguments(args, argTypes, m.getParameterTypes()));
        return result;
    } catch (InvocationTargetException ex) {
        throw ex.getCause();
} else {
    throw new NoSuchMethodException(name);
TinvokeMethod(Object target, String signature, Object... args)
Invokes method declared in target, may be private.
Assert.isNotNull(target);
Assert.isNotNull(signature);
Method method = getMethod(target, signature);
if (method == null) {
    Class<?> targetClass = getTargetClass(target);
    throw new IllegalArgumentException(signature + " in " + targetClass);
try {
...
booleaninvokeMethod(Object target, String thisMethod, Object value)
Invokes the specified method
try {
    if (value != null) {
        Class[] argTypes = new Class[] { value.getClass() };
        Method method = target.getClass().getMethod(thisMethod, argTypes);
        method.invoke(target, new Object[] { value });
} catch (Exception e) {
    if (System.getProperty("DEBUG") != null) {
...
ObjectinvokeMethod(Object theObject, String methodName, Object... parametersObject)
reflectively invokes the method on the object and catches all exceptions and wraps them in RuntimeExceptions
try {
    Class<?>[] parameters = new Class<?>[parametersObject.length];
    for (int i = 0; i < parameters.length; i++) {
        parameters[i] = parametersObject[i].getClass();
    Method method = theObject.getClass().getMethod(methodName, parameters);
    Object returnValue = method.invoke(theObject, parametersObject);
    return returnValue;
...
ObjectinvokeMethod(String className, String method, Class[] paramTypes, Object obj, Object[] args)
invoke Method
Object result = null;
try {
    Class cls = Class.forName(className);
    Method m = cls.getMethod(method, paramTypes);
    result = m.invoke(obj, args);
} catch (Exception e) {
    throw new Exception("Method invoke failed: Class: " + className + " Method: " + method);
return result;
ObjectinvokeMethod(String methodName, Object gameCommand)
invoke Method
Method method;
Object returnObj = null;
try {
    method = gameCommand.getClass().getDeclaredMethod(methodName);
    method.setAccessible(true);
    returnObj = method.invoke(gameCommand);
} catch (NoSuchMethodException e) {
    e.printStackTrace();
...
TinvokeMethod(String name, Object target)
invoke Method
Method method = findMethod(target.getClass(), name);
if (method == null) {
    return null;
return invokeMethod(method, target);
ObjectinvokeMethod2(Class cls, Object obj, String methodName, Object[] args)
invoke Method
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
    if (method.getName().equals(methodName)) {
        method.setAccessible(true);
        return method.invoke(obj, args);
return null;
...
ObjectinvokeMethodAndGet(Object object, String methodName, Object... args)
Invokes and returns result of method with given name and arguments of target object's class.
Class[] params = new Class[args.length];
for (int i = 0; i < args.length; ++i)
    params[i] = args[i].getClass();
Method m = object.getClass().getDeclaredMethod(methodName, params);
m.setAccessible(true);
try {
    return m.invoke(object, args);
} finally {
...