Here you can find the source of getMethod(Class> clazz, String methodName, Class>... params)
Parameter | Description |
---|---|
clazz | The class we are searching |
methodName | The name of the method |
params | Any parameters that the method has |
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params)
//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; } } }