Java Reflection Method Invoke invokeMethod(Class clazz, Object classObject, String methodName, Class[] paramTypes, Object... args)

Here you can find the source of invokeMethod(Class clazz, Object classObject, String methodName, Class[] paramTypes, Object... args)

Description

invoke Method

License

LGPL

Declaration

public static Object invokeMethod(Class clazz, Object classObject, String methodName, Class[] paramTypes,
            Object... args) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static Object invokeMethod(Class clazz, Object classObject, String methodName, Class[] paramTypes,
            Object... args) {// w ww.  j  ava2s.c  om
        try {
            if (paramTypes == null) {
                paramTypes = new Class[args.length];
                for (int i = 0; i < args.length; i++) {
                    if (args[i] instanceof Integer) {
                        paramTypes[i] = int.class;
                    } else {
                        paramTypes[i] = args[i].getClass();
                    }
                }
            }
            Method m = clazz.getDeclaredMethod(methodName, paramTypes);
            m.setAccessible(true);
            return m.invoke(classObject, args);
        } catch (Exception ex) {
            System.out.println(ex.getCause() + ", ");
            ex.printStackTrace();
            return null;
        }
    }
}

Related

  1. invoke(Method method, Object target, Object... arguments)
  2. invoke(Method method, T instance, Object... params)
  3. invoke(MethodHandle methodHandle, Object... params)
  4. invokeJdbcMethod(Method method, Object target)
  5. invokeJdbcMethod(Method method, Object target)
  6. invokeMethod(Class clazz, Object obj, String methodName, Class[] parametersTypes, Object[] parameters)
  7. invokeMethod(Class clazz, String methodName, Object[] args)
  8. invokeMethod(Class targetClass, Object obj, String methodName, Object arg)
  9. invokeMethod(Class clazz, E instance, String[] names, Object... args)