Java Reflection Method Name getMethod(Class clazz, String methodName, Class... params)

Here you can find the source of getMethod(Class clazz, String methodName, Class... params)

Description

Get a method from a class that has the specific paramaters

License

Open Source License

Parameter

Parameter Description
clazz The class we are searching
methodName The name of the method
params Any parameters that the method has

Return

The method with appropriate paramaters

Declaration

public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from w w w . j  av  a  2s . c  o  m*/
     * Cache of methods that we've found in particular classes
     */
    private static Map<Class<?>, Map<String, Method>> loadedMethods = new HashMap<Class<?>, Map<String, Method>>();

    /**
     * Get a method from a class that has the specific paramaters
     *
     * @param clazz      The class we are searching
     * @param methodName The name of the method
     * @param params     Any parameters that the method has
     * @return The method with appropriate paramaters
     */
    public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
        if (!loadedMethods.containsKey(clazz)) {
            loadedMethods.put(clazz, new HashMap<String, Method>());
        }

        Map<String, Method> methods = loadedMethods.get(clazz);

        if (methods.containsKey(methodName)) {
            return methods.get(methodName);
        }

        try {
            Method method = clazz.getMethod(methodName, params);
            methods.put(methodName, method);
            loadedMethods.put(clazz, methods);
            return method;
        } catch (Exception e) {
            e.printStackTrace();
            methods.put(methodName, null);
            loadedMethods.put(clazz, methods);
            return null;
        }
    }
}

Related

  1. getMethod(Class c, String name, Class[] args)
  2. getMethod(Class clazz, String methodName)
  3. getMethod(Class clazz, String methodName, Class fieldType)
  4. getMethod(Class clazz, String methodName, Class... arguments)
  5. getMethod(Class clazz, String methodName, Class... params)
  6. getMethod(Class clazz, String methodName, Class... params)
  7. getMethod(Class clazz, String methodName, Class... params)
  8. getMethod(Class clazz, String methodName, final Class[] classes)
  9. getMethod(Class clazz, String methodName, int numParams)