Java Reflection Method Parameter getMethod(final Class target, final String name, final Class... parameters)

Here you can find the source of getMethod(final Class target, final String name, final Class... parameters)

Description

Gets the method defined on the target class.

License

Open Source License

Parameter

Parameter Description
target Target class that contains method.
name Method name.
parameters Method parameters.

Return

Method if found, otherwise null.

Declaration

public static Method getMethod(final Class<?> target, final String name, final Class<?>... parameters) 

Method Source Code


//package com.java2s;
/* See LICENSE for licensing and NOTICE for copyright. */

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

public class Main {
    /** Method cache. */
    private static final Map<String, Method> METHOD_CACHE = new HashMap<>();

    /**// w w w . j  a  v a 2  s  .  c o m
     * Gets the method defined on the target class. The method is cached to speed
     * up subsequent lookups.
     *
     * @param  target  Target class that contains method.
     * @param  name  Method name.
     * @param  parameters  Method parameters.
     *
     * @return  Method if found, otherwise null.
     */
    public static Method getMethod(final Class<?> target, final String name, final Class<?>... parameters) {
        final String key = target.getName() + '.' + name;
        Method method = METHOD_CACHE.get(key);
        if (method != null) {
            return method;
        }
        try {
            method = target.getMethod(name, parameters);
            METHOD_CACHE.put(key, method);
            return method;
        } catch (NoSuchMethodException e) {
            return null;
        }
    }
}

Related

  1. getMethod(final Class javaClass, final String methodName, final Class[] methodParameterTypes, final boolean shouldSetAccessible)
  2. getMethod(final Class aClass, final String methodName, Class[] parameterTypes)
  3. getMethod(final Class clazz, final String methodName, final Class... parameterTypes)
  4. getMethod(final Class clazz, final String name, final Class... parametertypes)
  5. getMethod(final Class clazz, final String name, final Class... parameterTypes)
  6. getMethod(final Class receiver, final String methodName, final Class... parameterTypes)
  7. getMethod(final Field visitee, final String methodName, final Class... parameterTypes)
  8. getMethod(String classFullName, String methodName, Class... parameterTypes)
  9. getMethod0(Class c, String name, Class... parameterTypes)