Java Reflection Method Invoke invokeMethod(Object target, String methodName, Object[] arguments)

Here you can find the source of invokeMethod(Object target, String methodName, Object[] arguments)

Description

invoke Method

License

Open Source License

Declaration

public static Object invokeMethod(Object target, String methodName, Object[] arguments)
            throws InvocationTargetException, IllegalAccessException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not 

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

public class Main {
    public static Object invokeMethod(Object target, String methodName, Object[] arguments)
            throws InvocationTargetException, IllegalAccessException {
        Class targetClass = target.getClass();
        Class[] argumentTypes = new Class[arguments.length];
        for (int i = 0; i < argumentTypes.length; i++) {
            argumentTypes[i] = arguments[i].getClass();
        }/*from   www  .j  a  v a 2 s  . co  m*/

        try {
            Method method = targetClass.getMethod(methodName, argumentTypes);
            return method.invoke(target, arguments);
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        }

        return null;
    }
}

Related

  1. invokeMethod(Object target, Method method)
  2. invokeMethod(Object target, Method method, Object... args)
  3. invokeMethod(Object target, Method method, Object[] args)
  4. invokeMethod(Object target, String method)
  5. invokeMethod(Object target, String methodName, Object... parameters)
  6. invokeMethod(Object target, String name, Class... parameterTypes)
  7. invokeMethod(Object target, String name, Object[] args, Class[] argTypes)
  8. invokeMethod(Object target, String signature, Object... args)
  9. invokeMethod(Object target, String thisMethod, Object value)