Java Method Call invoke(Class clazz, Object obj, String methodName, Object... args)

Here you can find the source of invoke(Class clazz, Object obj, String methodName, Object... args)

Description

invoke

License

Open Source License

Declaration

public static Object invoke(Class<?> clazz, Object obj,
            String methodName, Object... args) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static Object invoke(Class<?> clazz, Object obj,
            String methodName, Object... args) {
        try {//from  w  w w. j  ava  2s.  c  o  m
            Method method = findMethod(clazz, methodName);
            method.setAccessible(true);
            Object result = method.invoke(obj, args);
            return result;
        } catch (Throwable e) {
            throw new RuntimeException("invoke", e);
        }
    }

    public static Method findMethod(Class<?> clazz, String methodName) {
        for (Method method : clazz.getDeclaredMethods())
            if (method.getName().equalsIgnoreCase(methodName))
                return method;
        return null;
    }

    public static Method findMethod(Class<?> clazz, String method,
            Class<?>... prmTypes) {
        try {
            return clazz.getMethod(method, prmTypes);
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. invoke(@Nonnull Object source, @Nonnull String name, Object... args)
  2. invoke(boolean makeAccessible, T object, Class methodClass, String methodName, Class[] paramTypes, Object... params)
  3. invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  4. invoke(Class targetClass, Object target, String methodName, Class paramType0, Object paramValue0)
  5. invoke(Class annotationClass, Object instance, Object[] parameters)
  6. invoke(Class clazz, String methodName, Class[] parameterTypes, Object instance, Object[] args, Class returnType)
  7. invoke(Class clazz, String methodName, Object instance, Class[] signature, Object... args)
  8. invoke(Class cls, String methodName, Object... parameter)
  9. invoke(Class returnType, Method m, Object obj, Object... args)