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

ObjectinvokeAnUnwrapException(final Method method, final Object[] args, final Object target)
invoke An Unwrap Exception
try {
    return method.invoke(target, args);
} catch (InvocationTargetException ite) {
    throw ite.getTargetException();
voidinvokeCallback(String callbackName, String callbackParam)
invoke Callback
int a = callbackName.lastIndexOf('/');
if (a < 0) {
    throw new IllegalStateException("bad callback format (should be ab/cd/className/methName)");
String className = METHOD_SEPARATOR.matcher(callbackName.substring(0, a)).replaceAll(".");
String methodName = callbackName.substring(a + 1);
try {
    Class<?> clazz = Class.forName(className);
...
voidinvokeCmd(String className, String[] args)
invoke Cmd
Class<?> cmd = null;
try {
    cmd = Class.forName(className);
} catch (ClassNotFoundException ex) {
    System.err.println("Class '" + className + "' not found");
    System.exit(1);
Method method = null;
...
ObjectinvokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
Invokes any method of a class, even private ones.
Method m = c.getDeclaredMethod(method, paramClasses);
m.setAccessible(true);
return m.invoke(obj, params);
ObjectinvokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
invoke Declared
Method m = c.getDeclaredMethod(method, paramClasses);
m.setAccessible(true);
return m.invoke(obj, params);
ObjectinvokeDeclaredMethod(Object object, String methodName, Object... parameters)
invoke Declared Method
try {
    Method method = object.getClass().getDeclaredMethod(methodName, convertToMethodParameters(parameters));
    method.setAccessible(true);
    return method.invoke(object, parameters);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
    e.printStackTrace();
    return null;
ObjectinvokeDeclaredMethod(String methodName, Object target, Class[] parameterTypes, Object[] parameters)
invoke Declared Method
Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
return invokeMethod(method, target, parameters);
voidinvokeDeclaredMethodWithAnnotation(Class annotationClass, Object target)
invoke Declared Method With Annotation
Method[] declaredMethods = target.getClass().getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
    Annotation annotation = declaredMethod.getAnnotation(annotationClass);
    if (annotation != null) {
        invokeMethod(declaredMethod, target, new Object[] {});
ObjectinvokeDefaultMethod(final Method method, final Object[] args, final Object proxy)
invoke Default Method
try {
    Class<?> MHclass = Class.forName("java.lang.invoke.MethodHandles");
    Object lookup = MHclass.getMethod("lookup", (Class<?>[]) null).invoke(null, (Object[]) null);
    Constructor<?> constructor = lookup.getClass().getDeclaredConstructor(Class.class);
    constructor.setAccessible(true);
    Object newLookupInstance = constructor.newInstance(method.getDeclaringClass());
    Object in = newLookupInstance.getClass().getMethod("in", new Class<?>[] { Class.class })
            .invoke(newLookupInstance, method.getDeclaringClass());
...
booleaninvokedMethod(java.lang.Object closure, java.lang.Class targetClass, java.lang.String targetMethod)
Test whether the given closure invokes the specified method in the specified class.
throw new RuntimeException();