Java Reflection Method Invoke invokeMethod(Class clazz, E instance, String[] names, Object... args)

Here you can find the source of invokeMethod(Class clazz, E instance, String[] names, Object... args)

Description

invoke Method

License

Open Source License

Declaration

public static <T, E> Object invokeMethod(Class<? extends E> clazz,
            E instance, String[] names, Object... args) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static <T, E> Object invokeMethod(Class<? extends E> clazz,
            E instance, String[] names, Object... args) {
        try {/*ww w . j a v a  2s.com*/
            Method method = getMethod(clazz, names);
            if (method != null)
                return method.invoke(instance, args);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Method getMethod(Class<?> clazz, String... names) {
        try {
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                for (String methodName : names) {
                    if (method.getName().equalsIgnoreCase(methodName)) {
                        method.setAccessible(true);
                        return method;
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return null;
    }
}

Related

  1. invokeJdbcMethod(Method method, Object target)
  2. invokeMethod(Class clazz, Object classObject, String methodName, Class[] paramTypes, Object... args)
  3. invokeMethod(Class clazz, Object obj, String methodName, Class[] parametersTypes, Object[] parameters)
  4. invokeMethod(Class clazz, String methodName, Object[] args)
  5. invokeMethod(Class targetClass, Object obj, String methodName, Object arg)
  6. invokeMethod(Class clazz, Object object, String methodName, Class[] parameterTypes, Object[] parameters)
  7. invokeMethod(Class clazz, String method, Class[] args, Object object, Object[] objects)
  8. invokeMethod(Class clz, String methodName, Object... params)
  9. invokeMethod(Class returnClass, String methodName, Object ivokeObject, Object... objects)