Java Reflection Method Invoke invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args)

Here you can find the source of invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args)

Description

Invokes the method

License

Open Source License

Parameter

Parameter Description
handle The handle to invoke it on
methodName The name of the method
parameterClasses The parameter types
args The arguments

Return

The resulting object or null if an error occurred / the method didn't return a thing

Declaration

@SuppressWarnings("unused")
@Nullable
public static Object invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.annotation.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;

public class Main {
    /**/*from ww  w .j av  a  2s.  c  om*/
     * Invokes the method
     *
     * @param handle           The handle to invoke it on
     * @param methodName       The name of the method
     * @param parameterClasses The parameter types
     * @param args             The arguments
     *
     * @return The resulting object or null if an error occurred / the method didn't return a thing
     */
    @SuppressWarnings("unused")
    @Nullable
    public static Object invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args) {
        return invokeMethod(handle.getClass(), handle, methodName, parameterClasses, args);
    }

    /**
     * Invokes the method
     *
     * @param clazz            The class to invoke it from
     * @param handle           The handle to invoke it on
     * @param methodName       The name of the method
     * @param parameterClasses The parameter types
     * @param args             The arguments
     *
     * @return The resulting object or null if an error occurred / the method didn't return a thing
     */
    @Nullable
    public static Object invokeMethod(Class<?> clazz, Object handle, String methodName, Class[] parameterClasses,
            Object... args) {
        Optional<Method> methodOptional = getMethod(clazz, methodName, parameterClasses);

        if (!methodOptional.isPresent()) {
            return null;
        }

        Method method = methodOptional.get();

        try {
            return method.invoke(handle, args);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Invokes the method
     *
     * @param method The method to invoke
     * @param handle The handle to invoke on
     * @param args   The arguments
     *
     * @return The resulting object or null if an error occurred / the method didn't return a thing
     */
    public static Object invokeMethod(Method method, Object handle, Object... args) {
        try {
            return method.invoke(handle, args);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Invokes the method
     *
     * @param rethrowMethodErrors If true, any exceptions by the method invoked will be propagated up
     * @param method              The method to invoke
     * @param handle              The handle to invoke on
     * @param args                The arguments
     *
     * @return The resulting object or null if an error occurred / the method didn't return a thing
     *
     * @throws InvocationTargetException if an exception is thrown by the method
     */
    public static Object invokeMethod(boolean rethrowMethodErrors, Method method, Object handle, Object... args)
            throws InvocationTargetException {
        try {
            return method.invoke(handle, args);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            if (rethrowMethodErrors) {
                throw e;
            } else {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * @param clazz  The class to get the method for
     * @param name   The name of the method
     * @param params The parameters of the method
     *
     * @return The method, if any
     */
    public static Optional<Method> getMethod(Class<?> clazz, String name, Class<?>... params) {
        try {
            return Optional.of(clazz.getMethod(name, params));
        } catch (NoSuchMethodException ignored) {
        }

        try {
            return Optional.of(clazz.getDeclaredMethod(name, params));
        } catch (NoSuchMethodException ignored) {
        }

        return Optional.empty();
    }
}

Related

  1. invokeMethod(Object caller, Object classInstance, String className, String methodName, String parameterClasses[], Object[] parameters)
  2. invokeMethod(Object caller, String methodName, Object[] params)
  3. invokeMethod(Object classInstance, Method method, Object... args)
  4. invokeMethod(Object cls, String methodName, Class paramClass, String paramValue)
  5. invokeMethod(Object destination, String methodName, Object argument)
  6. invokeMethod(Object handler, String strMethod, Class[] cls, Object... params)
  7. invokeMethod(Object instance, String methodName, Class expectedReturnType)
  8. invokeMethod(Object o, String fieldName)
  9. invokeMethod(Object o, String methodName, Object... args)