Java Method Call invoke(Object targetObject, String method, Class[] paramTypes, Object[] paramValues)

Here you can find the source of invoke(Object targetObject, String method, Class[] paramTypes, Object[] paramValues)

Description

invoke

License

Apache License

Declaration

public static Object invoke(Object targetObject, String method, Class<?>[] paramTypes, Object[] paramValues)
        throws Exception 

Method Source Code


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

import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static final ConcurrentHashMap<String, Method> METHOD_CACHE = new ConcurrentHashMap<String, Method>();

    public static Object invoke(Object targetObject, String method, Class<?>[] paramTypes, Object[] paramValues)
            throws Exception {

        String methodKey = getMethodKey(targetObject.getClass(), method, paramTypes);

        Method methodObject = METHOD_CACHE.get(methodKey);

        if (methodObject == null) {
            methodObject = targetObject.getClass().getMethod(method, paramTypes);

            if (methodObject != null) {
                METHOD_CACHE.put(methodKey, methodObject);
            }/*from  w  w w  .  ja v a2 s . c  om*/
        }
        return methodObject.invoke(targetObject, paramValues);
    }

    private static String getMethodKey(Class<?> clazz, String method, Class<?>[] paramTypes) {
        StringBuilder sb = new StringBuilder(clazz.getName()).append(".").append(method);
        if (paramTypes != null && paramTypes.length > 0) {
            for (Class<?> c : paramTypes) {
                sb.append("-").append(c.getName());
            }
        }
        return sb.toString();
    }
}

Related

  1. invoke(Object target, Method method)
  2. invoke(Object target, String methodName)
  3. invoke(Object target, String methodName, Object... args)
  4. invoke(Object target, String methodName, Object[] args)
  5. invoke(Object target, String name, Class[] params, Object[] args)
  6. invoke(Object targetObject, String methodName, Object... params)
  7. invoke(Object thisObject, Method method)
  8. invoke(ObjectName objectName, Object[] params, String[] signature, String operationName)
  9. invoke(ObjectName objectName, String attribute, Object[] params, String[] signatur)