Java Reflection Method Name getMethod(Class targetClass, String name, Class paramClass)

Here you can find the source of getMethod(Class targetClass, String name, Class paramClass)

Description

Get the method with the given name and with a single argument of the type specified by paramClass.

License

Open Source License

Parameter

Parameter Description
targetClass The target class
name The method name
paramClass The single parameter class (may be null)

Exception

Parameter Description
SecurityException an exception
NoSuchMethodException an exception

Return

The Method

Declaration


public static Method getMethod(Class targetClass, String name, Class paramClass)
        throws NoSuchMethodException, SecurityException 

Method Source Code


//package com.java2s;
import java.lang.reflect.Method;

public class Main {
    private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];

    /** Get the method with the given name and with a single argument of the
    type specified by paramClass.  The paramClass may be null in which case
    the method should have no arguments.
        /*from   w  w  w  . jav a  2 s  . c  o  m*/
    @param targetClass The target class
    @param name The method name
    @param paramClass The single parameter class (may be null)
    @return The Method
    @throws SecurityException
    @throws NoSuchMethodException
    */

    public static Method getMethod(Class targetClass, String name, Class paramClass)
            throws NoSuchMethodException, SecurityException {
        if (paramClass == null) {
            return getMethod(targetClass, name, EMPTY_CLASS_ARRAY);
        } else {
            Class[] paramClasses = new Class[1];
            paramClasses[0] = paramClass;
            return getMethod(targetClass, name, paramClasses);
        }
    }

    /** Get the method with the given name and with arguments of the
    types specified by paramClasses.  If the method is not found
    then a method which accepts superclasses of the current arguments
    will be returned, if possible.
        
    @param targetClass The target class
    @param name The method name
    @param paramClasses An array of parameter classes
    @return The Method
    @throws SecurityException
    @throws NoSuchMethodException
    */

    public static Method getMethod(Class targetClass, String name, Class[] paramClasses)
            throws NoSuchMethodException, SecurityException {
        Method method = null;
        try {
            method = targetClass.getMethod(name, paramClasses);
        } catch (NoSuchMethodException nsme) {
            Method[] methods = targetClass.getMethods();

            OUTER: for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().equalsIgnoreCase(name)
                        && methods[i].getParameterTypes().length == paramClasses.length) {
                    Class[] params = methods[i].getParameterTypes();
                    for (int j = 0; j < params.length; j++) {
                        if (!params[j].isAssignableFrom(paramClasses[j])) {
                            continue OUTER;
                        }
                    }
                    method = methods[i];
                    break;
                }
            }
            if (method == null) {
                throw nsme;
            }
        }
        return method;
    }
}

Related

  1. getMethod(Class clz, String methodName, Class expectedTypes[])
  2. getMethod(Class klazz, String[] methodNames, int argCount)
  3. getMethod(Class objClass, String methodName, Class argClass)
  4. getMethod(Class serviceClass, String methodName, Class... mapClass)
  5. getMethod(Class targetClass, String methodName, Class[] paramTypes)
  6. getMethod(Class targetClass, String targetMethodName)
  7. getMethod(Class theClass, String propertyName)
  8. getMethod(Class type, String name, Class[] paramTypes)
  9. getMethod(Class beanClass, String methodName, Class[] types)