Java Method Call invoke(Object host, String method, Object[] args)

Here you can find the source of invoke(Object host, String method, Object[] args)

Description

invoke

License

Apache License

Declaration

public static Object invoke(Object host, String method, Object[] args)
            throws Exception 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static Object invoke(Object host, String method, Object[] args)
            throws Exception {
        Class clz = host.getClass();
        for (Method m : clz.getMethods()) {
            if (m.getName().equals(method)
                    && m.getParameterCount() == args.length) {
                Class[] paramTypes = m.getParameterTypes();
                for (int i = 0; i < paramTypes.length; ++i)
                    args[i] = rectifyValue(paramTypes[i], args[i]);
                return m.invoke(host, args);
            }//www .j  av a 2s  .  co  m
        }
        return null;
    }

    public static Object rectifyValue(Class type, Object value) {
        if (type == byte.class || type == Byte.class)
            return new Byte(((Number) value).byteValue());
        if (type == char.class || type == Character.class)
            return new Character(value.toString().charAt(0));
        if (type == short.class || type == Short.class)
            return new Short(((Number) value).shortValue());
        if (type == int.class || type == Integer.class)
            return new Integer(((Number) value).intValue());
        if (type == long.class || type == Long.class)
            return new Long(((Number) value).longValue());
        if (type == float.class || type == Float.class)
            return new Float(((Number) value).floatValue());
        return value;
    }
}

Related

  1. invoke(Object bean, Method method, Object value)
  2. invoke(Object bean, String methodName, Object args, Class parameterType)
  3. invoke(Object buffer, String methodName, Class[] paramTypes, Object... args)
  4. invoke(Object clazzInstance, String method, Class[] paramClasses, Object[] params)
  5. invoke(Object context, String methodName, Object parameter)
  6. invoke(Object instance, java.lang.reflect.Method method, Object... args)
  7. invoke(Object instance, Method method, Object... params)
  8. invoke(Object instance, String method, Class[] paramTypes, Object... parameters)
  9. invoke(Object invoker, String cmd, Map params)