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

ObjectinvokeMethod(Object object, String methodName, Object arg, Class argType)
invoke Method
return invokeMethod(object, methodName, new Object[] { arg }, new Class[] { argType });
TinvokeMethod(Object object, String methodName, Object... parameters)
Invokes the method with the given name of the specified object, with the specified parameters.
try {
    Class[] parameterTypes = new Class[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        Object arg = parameters[i];
        parameterTypes[i] = arg.getClass();
    Method method;
    if (object instanceof Class) {
...
ObjectinvokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)

Invoke a named method whose parameter type matches the object type.

if (parameterTypes == null) {
    parameterTypes = emptyClassArray;
if (args == null) {
    args = emptyObjectArray;
Method method = getMatchingAccessibleMethod(object.getClass(), methodName, parameterTypes);
if (method == null)
...
booleaninvokeMethod(Object object, String methodName, Object[] params, Object[] result)
Find and invoke the method on the object if it exists.
if (object == null)
    return false;
boolean retval = false;
Method method = null;
Class<?>[] paramTypes = params == null ? null : new Class<?>[params.length];
if (params != null) {
    for (int i = 0; i < params.length; ++i) {
        paramTypes[i] = params[i] == null ? null : params[i].getClass();
...
ObjectinvokeMethod(Object object, String name, Object... arguments)
invoke Method
Class<?> clazz = object.getClass();
Class<?> argumentClasses[] = new Class<?>[arguments.length];
for (int argumentId = 0; argumentId < arguments.length; argumentId++) {
    argumentClasses[argumentId] = arguments[argumentId].getClass();
Method field = clazz.getDeclaredMethod(name, argumentClasses);
boolean accessable = field.isAccessible();
if (!accessable)
...
ObjectinvokeMethod(Object object, String propertyName)
invoke Method
try {
    Method getterMethod = object.getClass().getMethod(propertyName);
    return getterMethod.invoke(object);
} catch (Exception e) {
    e.printStackTrace();
    return null;
ObjectinvokeMethod(Object objectInstance, String methodToInvoke, Class[] parameterTypes, Object[] instanceParameters)
invoke Method
Method method = objectInstance.getClass().getMethod(methodToInvoke, parameterTypes);
Object retVal = method.invoke(objectInstance, instanceParameters);
return retVal;
ObjectinvokeMethod(Object owner, String methodName)
invoke Method
return invokeMethod(owner, methodName, new Object[] {});
ObjectinvokeMethod(Object owner, String methodName, Object[] args)
Invoke method.
Class<?> ownerClass = owner.getClass();
Class<?>[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
    argsClass[i] = args[i].getClass();
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(owner, args);
voidinvokeMethod(Object parent, String method, Class[] paramTypes, Object[] args)
invoke Method
try {
    parent.getClass().getMethod(method, paramTypes).invoke(parent, args);
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
...