Java Method Call invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues)

Here you can find the source of invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues)

Description

invoke

License

Apache License

Declaration

@SuppressWarnings("rawtypes")
    public static Object invoke(final Object object, final String methodName, final Class[] methodParamTypes,
            final Object[] methodParamValues) throws Exception 

Method Source Code

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

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

public class Main {
    @SuppressWarnings("rawtypes")
    public static Object invoke(final Object object, final String methodName, final Class[] methodParamTypes,
            final Object[] methodParamValues) throws Exception {
        Object result = null;/*from  w ww .  jav a2  s .  c o  m*/
        try {
            result = object.getClass().getMethod(methodName, methodParamTypes).invoke(object, methodParamValues);
        } catch (InvocationTargetException ite) {
            Throwable t = ite.getTargetException();
            if (t instanceof Exception) {
                throw (Exception) t;
            }
            throw ite;
        } catch (Exception t) {
            t.printStackTrace();
        }
        return result;
    }

    @SuppressWarnings("rawtypes")
    public static Object invoke(Object obj, String method, Object[] params, Class[] param_types)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = obj.getClass().getMethod(method, param_types);
        return m.invoke(obj, params);
    }

    @SuppressWarnings("rawtypes")
    public static Object invoke(Object obj, String method, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method[] all_methods = obj.getClass().getMethods();
        Method method2invoke = null;
        // find method with the same name and matching argument types
        for (int i = 0; i < all_methods.length; i++) {
            Method m = all_methods[i];
            if (m.getName().equals(method)) {
                Class[] pt = m.getParameterTypes();
                boolean match = true;
                int match_loops = pt.length;
                if (match_loops != params.length) {
                    continue;
                }
                for (int j = 0; j < match_loops; j++) {
                    if (pt[i].isInstance(params[i]) == false) {
                        match = false;
                        break;
                    }
                }
                if (match == true) {
                    method2invoke = m;
                }
            }
        }

        // throw an exception if no method to invoke
        if (method2invoke == null) {
            String t = "(";
            for (int i = 0; i < params.length; i++) {
                if (i != 0) {
                    t += ", ";
                }
                t += params[i].getClass().getName();
            }
            t += ")";
            throw new NoSuchMethodException(obj.getClass().getName() + "." + method + t);
        }

        // finally, invoke founded method
        return method2invoke.invoke(obj, params);
    }

    public static Method getMethod(Object o, String methodName) {
        if ((methodName == null) || (o == null)) {
            return null;
        }
        Method[] ms = o.getClass().getMethods();
        for (int i = 0; i < ms.length; i++) {
            Method m = ms[i];
            if (m.getName().equals(methodName)) {
                return m;
            }
        }
        return null;
    }
}

Related

  1. invoke(final Method m, final Object obj, final Object... args)
  2. invoke(final Method method, final Object obj, final Object... args)
  3. invoke(final Object component, final Method method, final Class type, final String value)
  4. invoke(final Object instance, final String method, final Object... parameters)
  5. invoke(final Object instance, final String methodName, final Class[] methodParams, final Object[] args)
  6. invoke(final Object target, final Method method, final Object... parameters)
  7. invoke(final Object target, final Method method, final Object[] params)
  8. invoke(Object _o, String _method, Object... _args)
  9. invoke(Object bean, Method method, Object value)