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

IteratorinvokeIteratorCallable(String callableClassName, ClassLoader cl)
invoke Iterator Callable
Class<?> klass = Class.forName(callableClassName, true, cl);
Constructor<?> ctor = klass.getDeclaredConstructor();
Object instance = ctor.newInstance();
Method callMethod = klass.getMethod("call");
return (Iterator<?>) callMethod.invoke(instance);
ObjectinvokeMXMethod(String methodName)
invoke MX Method
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
Method m = operatingSystemMXBean.getClass().getDeclaredMethod(methodName);
m.setAccessible(true);
return m.invoke(operatingSystemMXBean);
TinvokeNoArgs(Object instance, Method method, Class returnType)
Invokes the method, using the provided instance as 'this', and casts the return value to the provided return type.
try {
    if (!method.isAccessible()) {
        method.setAccessible(true);
    final Object returnValue = method.invoke(instance, NO_ARGS);
    return returnType.cast(returnValue);
} catch (Exception e) {
    final String message = String.format("Error invoking no-args method: class=%s, method=%s",
...
ObjectinvokeNoArgsMethod(Object object, String methodName)
Invoke no args method from the given instance.
Method declaredMethod = null;
try {
    declaredMethod = object.getClass().getDeclaredMethod(methodName, (Class<?>[]) null);
    declaredMethod.setAccessible(true);
    return declaredMethod.invoke(object, (Object[]) null);
} catch (Exception e) {
    throw new IllegalStateException("Failed to invoke method: " + methodName, e);
ObjectinvokeNonAccessibleMethod(Class clazz, String methodName, Object instance, Object... params)
invoke Non Accessible Method
List<Class<?>> targetParamTypes = new ArrayList<Class<?>>();
if (params != null) {
    for (Object param : params) {
        targetParamTypes.add(param.getClass());
Object retVal = null;
try {
...
ObjectinvokeNonArgMethod(Object object, String methodName)
invoke Non Arg Method
Class<?> clazz = object.getClass();
try {
    return clazz.getMethod(methodName).invoke(object);
} catch (NoSuchMethodException ex) {
    throw new IllegalArgumentException(String.format("Class should declare method %s()", methodName));
} catch (InvocationTargetException | IllegalAccessException ex) {
    throw new IllegalStateException(ex);
ObjectinvokeObjectMethod(Object object, String name, Class paramTypes[], Object args[])
invoke Object Method
Method method = object.getClass().getMethod(name, paramTypes);
if (!method.isAccessible()) {
    method.setAccessible(true);
try {
    return method.invoke(object, args);
} catch (InvocationTargetException e) {
    throw e.getTargetException();
...
ObjectinvokeOrBailOut(Object invokee, Method method, Object[] params)
Utility method that invokes a method and does the error handling around the invocation.
try {
    return method.invoke(invokee, params);
} catch (IllegalArgumentException e) {
    throw new Error(createMessage(invokee, method, params), e);
} catch (IllegalAccessException e) {
    throw new Error(createMessage(invokee, method, params), e);
} catch (InvocationTargetException e) {
    throw new Error(createMessage(invokee, method, params), e);
...
ObjectinvokeParameterlessMethod(T theObject, String methodName)
invoke Parameterless Method
Method method = theObject.getClass().getDeclaredMethod(methodName, null);
method.setAccessible(true);
return method.invoke(theObject, null);
voidinvokePrepare(Object instance)
Try to invoke the prepare() method on the provided object instance.
if (instance == null) {
    return;
try {
    instance.getClass().getMethod("prepare").invoke(instance);
} catch (NoSuchMethodException | SecurityException ex) {
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {