Java Method Call invoke(Object target, String methodName)

Here you can find the source of invoke(Object target, String methodName)

Description

invoke

License

Open Source License

Declaration

private static Object invoke(Object target, String methodName)
            throws Exception 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

import java.lang.reflect.Method;

import java.util.Hashtable;

import java.util.Map;

public class Main {
    /** The classloader we should use to load javahelp classes */
    private static ClassLoader classloader = null;
    private static final Object[] NO_ARGS = new Object[0];
    private static Map METHOD_ARGS = new Hashtable();
    private static Map METHODS = new Hashtable();

    private static Object invoke(Object target, String methodName)
            throws Exception {
        return invoke(target, methodName, NO_ARGS);
    }/*from   ww w  .ja  v a2 s  . c o m*/

    private static Object invoke(Object target, String methodName,
            Object arg1) throws Exception {
        return invoke(target, methodName, new Object[] { arg1 });
    }

    private static Object invoke(Object target, String methodName,
            Object arg1, Object arg2) throws Exception {
        return invoke(target, methodName, new Object[] { arg1, arg2 });
    }

    private static Object invoke(Object target, String methodName,
            Object arg1, Object arg2, Object arg3) throws Exception {
        return invoke(target, methodName, new Object[] { arg1, arg2, arg3 });
    }

    private static Object invoke(Object target, String methodName,
            Object[] args) throws Exception {
        try {
            Method m = getMethod(target, methodName);
            if (target instanceof String)
                return m.invoke(null, args);
            else
                return m.invoke(target, args);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private static Method getMethod(Object target, String methodName)
            throws Exception {
        Method result = (Method) METHODS.get(methodName);

        if (result == null) {
            Object[] argTypes = (Object[]) METHOD_ARGS.get(methodName);
            result = getMethod(target, methodName, argTypes);
            METHODS.put(methodName, result);
        }

        return result;
    }

    private static Method getMethod(Object target, String methodName,
            Object[] argTypes) throws Exception {
        Class c = resolveClass(target);

        Class[] paramTypes = new Class[argTypes.length];
        for (int i = 0; i < paramTypes.length; i++)
            paramTypes[i] = resolveClass(argTypes[i]);

        return c.getMethod(methodName, paramTypes);
    }

    private static Class resolveClass(Object obj) throws Exception {
        if (obj instanceof Class)
            return (Class) obj;
        else if (obj instanceof String)
            return classloader.loadClass((String) obj);
        else
            return obj.getClass();
    }
}

Related

  1. invoke(Object proxy, java.lang.reflect.Method method, Object[] args)
  2. invoke(Object source, String key)
  3. invoke(Object target, Class clazz, String methodName, Class[] parameterTypes, Object... args)
  4. invoke(Object target, Class clazz, String method)
  5. invoke(Object target, Method method)
  6. invoke(Object target, String methodName, Object... args)
  7. invoke(Object target, String methodName, Object[] args)
  8. invoke(Object target, String name, Class[] params, Object[] args)
  9. invoke(Object targetObject, String method, Class[] paramTypes, Object[] paramValues)