Java Reflection Method Get from Object getMethod(Class clz, String methodName, Class[] methodArgs)

Here you can find the source of getMethod(Class clz, String methodName, Class[] methodArgs)

Description

Gets method from class clz or any of its superclasses.

License

Open Source License

Parameter

Parameter Description
clz - declaring class of the method
methodName - name of the method
methodArgs - method arguments

Exception

Parameter Description
NoSuchMethodException an exception

Return

requested method

Declaration

public static Method getMethod(Class<? extends Object> clz, String methodName, Class[] methodArgs)
        throws NoSuchMethodException 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    /**//from   w w  w.  j  a  va 2 s . c  om
     * Gets method from class <code>clz</code> or any of its superclasses. If no method can be found 
     * <code>NoSuchMethodException</code> is raised.
     * 
     * @param clz - declaring class of the method
     * @param methodName - name of the method
     * @param methodArgs - method arguments
     * @return requested method
     * @throws NoSuchMethodException
     */
    public static Method getMethod(Class<? extends Object> clz, String methodName, Class[] methodArgs)
            throws NoSuchMethodException {

        if (clz == null)
            throw new NoSuchMethodException(methodName + "(" + methodArgs + ") method does not exist ");

        try {

            return clz.getDeclaredMethod(methodName, methodArgs);

        } catch (NoSuchMethodException e) {

            return getMethod(clz.getSuperclass(), methodName, methodArgs);

        }
    }
}

Related

  1. getMethod(Class aObject, String aMethod, Class... aParameterTypes)
  2. getMethod(Class c, String methodName, Object... pa)
  3. getMethod(Class objectClass, String methodName, String argumentType)
  4. getMethod(Class type, String name, Object[] args)
  5. getMethod(Class type, String name, Class... parameterTypes)
  6. getMethod(Class cls, String method, Object[] params)
  7. getMethod(Class clz, String methodName, Object... params)
  8. getMethod(Class klass, String methodName, Object... params)