Java Method Call invoke(Object targetObject, String methodName, Object... params)

Here you can find the source of invoke(Object targetObject, String methodName, Object... params)

Description

invoke

License

Apache License

Declaration

public static Object invoke(Object targetObject, String methodName, Object... params)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<Class, Class> wrappers = new HashMap<Class, Class>();

    public static Object invoke(Object targetObject, String methodName, Object... params)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        return findMethod(targetObject.getClass(), methodName, params).invoke(targetObject, params);
    }/*w w w .j  ava  2 s  . com*/

    private static Method findMethod(Class<?> clazz, String methodName, Object... params)
            throws NoSuchMethodException {
        for (Method c : clazz.getMethods()) {
            if (c.getName().equals(methodName) && match(c.getParameterTypes(), params)) {
                return c;
            }
        }

        throw new NoSuchMethodException(clazz.getName() + "." + methodName + argumentTypesToString(params));
    }

    private static boolean match(Class<?>[] paramTypes, Object[] params) {
        if (params.length != paramTypes.length) {
            return false;
        }

        for (int i = 0; i < paramTypes.length; i++) {
            Class<?> type = paramTypes[i];

            if (type.isPrimitive()) {
                type = wrappers.get(type);
            }

            if (params[i] != null && !type.isInstance(params[i])) {
                return false;
            }
        }

        return true;
    }

    private static String argumentTypesToString(Object[] args) {
        StringBuilder buf = new StringBuilder();
        buf.append("(");

        if (args != null) {
            for (Object arg : args) {
                if (buf.length() > 1) {
                    buf.append(", ");
                }

                buf.append(arg == null ? "null" : arg.getClass().getName());
            }
        }

        buf.append(")");
        return buf.toString();
    }
}

Related

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