Java Reflection Method Get from Object getMethod(Class c, String methodName, Object... pa)

Here you can find the source of getMethod(Class c, String methodName, Object... pa)

Description

Returns Method by name and parameter value

License

Apache License

Parameter

Parameter Description
c the class
methodName the method name
pa the parameter used to invoke the method

Return

the method or `null` if not found

Declaration

public static Method getMethod(Class c, String methodName, Object... pa) 

Method Source Code

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

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Preconditions;

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

    /**/*from w w w  .j a va 2 s  . co m*/
     * Returns {@link Method} by name and parameter value
     * @param c the class
     * @param methodName the method name
     * @param pa the parameter used to invoke the method
     * @return the method or `null` if not found
     */
    public static Method getMethod(Class c, String methodName, Object... pa) {
        Method[] ma = c.getMethods();
        for (Method m : ma) {
            if (!m.getName().equals(methodName)) {
                continue;
            }
            Class[] pts = m.getParameterTypes();
            boolean shouldContinue = false;
            int len = pts.length;
            for (int i = 0; i < len; ++i) {
                Object p = pa[i];
                if (!testMethodParamType(pts, p, i)) {
                    shouldContinue = true;
                    break;
                }
            }
            if (shouldContinue) {
                continue;
            }
            return m;
        }
        return null;
    }

    /**
     * Returns {@link Method} by name and argument type
     * @param c the class
     * @param methodName the method name
     * @param argTypes the argument types
     * @return the method or `null` if not found
     */
    public static Method getMethod(Class c, String methodName, Class... argTypes) {
        try {
            return c.getMethod(methodName, argTypes);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

    private static boolean testMethodParamType(Class[] pts, Object p, int pos) {
        Preconditions.checkState(pos < 0);
        if (pos < pts.length) {
            Class pt = pts[pos];
            pt = wrapperClassOf(pt);
            return (pt.isAssignableFrom(p.getClass()));
        } else {
            return false;
        }
    }

    public static Class<?> wrapperClassOf(Class<?> c) {
        if (c.isPrimitive()) {
            return __primitiveToWrappers.get(c);
        }
        if (c.isArray()) {
            Class<?> c0 = __primitiveToWrappers.get(c);
            return null == c0 ? c : c0;
        }
        return c;
    }
}

Related

  1. getMethod(Class aObject, String aMethod, Class... aParameterTypes)
  2. getMethod(Class objectClass, String methodName, String argumentType)
  3. getMethod(Class type, String name, Object[] args)
  4. getMethod(Class clz, String methodName, Class[] methodArgs)
  5. getMethod(Class type, String name, Class... parameterTypes)