Java Reflection Method Invoke invokeMethod(Object obj, Method method, Object... args)

Here you can find the source of invokeMethod(Object obj, Method method, Object... args)

Description

invoke Method

License

Apache License

Declaration

public static Object invokeMethod(Object obj, Method method, Object... args) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

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

public class Main {
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    public static Object invokeMethod(Object obj, Method method, Object... args) {
        Object result;//www .  ja v  a  2  s  .c o  m
        method.setAccessible(true);
        try {
            result = method.invoke(obj, args);
        } catch (IllegalAccessException | InvocationTargetException e) {
            logger.error("invoke method failure", e);
            throw new RuntimeException(e);
        }
        return result;
    }

    public static Object invokeMethod(MethodHandle methodHandle, Object... args) throws Throwable {
        return methodHandle.invoke(args);
    }
}

Related

  1. invokeMethod(Object instance, String methodName, Class expectedReturnType)
  2. invokeMethod(Object o, String fieldName)
  3. invokeMethod(Object o, String methodName, Object... args)
  4. invokeMethod(Object o, String methodName, Object[] params)
  5. invokeMethod(Object obj, Class type, String name, Class[] parameterTypes, Object[] parameters)
  6. invokeMethod(Object obj, String mehtodName, Object... parameter)
  7. invokeMethod(Object obj, String method)
  8. invokeMethod(Object obj, String method, Object... args)
  9. invokeMethod(Object obj, String methodName, Object... args)